Kafka Topic InvalidRetentionMsException encountered when setting topic configurations.

The retention period for the Kafka topic is set to an invalid value.

Understanding Apache Kafka

Apache Kafka is a distributed event streaming platform used by thousands of companies for high-performance data pipelines, streaming analytics, data integration, and mission-critical applications. Kafka is designed to handle real-time data feeds and is often used for building real-time streaming data pipelines and applications that adapt to the data streams.

Identifying the Symptom: InvalidRetentionMsException

When working with Kafka topics, you might encounter the InvalidRetentionMsException. This exception typically surfaces when there is an issue with the retention period configuration of a Kafka topic. The retention period dictates how long Kafka retains messages in a topic before they are eligible for deletion.

What You Observe

Developers may notice that Kafka is not retaining messages for the expected duration, or an error message is logged indicating an InvalidRetentionMsException. This can disrupt data processing workflows that rely on specific retention periods.

Details About the InvalidRetentionMsException

The InvalidRetentionMsException is thrown when the retention period set for a Kafka topic is not valid. This could be due to setting a negative value, a value that exceeds the maximum allowed, or a non-numeric value. Kafka requires the retention period to be specified in milliseconds, and it must be a positive integer.

Common Causes

  • Setting a negative retention period.
  • Using a non-numeric value for the retention period.
  • Exceeding the maximum allowable retention period.

Steps to Fix the InvalidRetentionMsException

To resolve the InvalidRetentionMsException, you need to ensure that the retention period is set correctly. Follow these steps:

Step 1: Verify Current Retention Settings

First, check the current retention settings for the topic. You can do this using the Kafka command-line tool:

bin/kafka-configs.sh --bootstrap-server localhost:9092 --entity-type topics --entity-name your_topic_name --describe

This command will display the current configuration, including the retention period.

Step 2: Set a Valid Retention Period

To set a valid retention period, use the following command, replacing your_topic_name with your actual topic name and retention_ms with the desired retention period in milliseconds:

bin/kafka-configs.sh --bootstrap-server localhost:9092 --entity-type topics --entity-name your_topic_name --alter --add-config retention.ms=604800000

In this example, 604800000 milliseconds corresponds to 7 days.

Step 3: Validate the Changes

After updating the retention period, validate the changes by describing the topic configuration again:

bin/kafka-configs.sh --bootstrap-server localhost:9092 --entity-type topics --entity-name your_topic_name --describe

Ensure that the retention.ms value reflects your intended configuration.

Additional Resources

For more information on Kafka topic configurations, refer to the official Kafka documentation. Additionally, consider exploring the Kafka Quickstart Guide for a comprehensive overview of setting up and managing Kafka topics.

Never debug

Kafka Topic

manually again

Let Dr. Droid create custom investigation plans for your infrastructure.

Start Free POC (15-min setup) →
Automate Debugging for
Kafka Topic
See how Dr. Droid creates investigation plans for your infrastructure.

MORE ISSUES

Made with ❤️ in Bangalore & San Francisco 🏢

Doctor Droid