Kafka Producer config retry strategy
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Apache Kafka is a distributed event streaming platform capable of handling vast amounts of data while providing robust messaging capabilities. One of the critical aspects of Kafka is its producer client which is responsible for sending messages (records) to Kafka topics. An important feature of the Kafka producer is its ability to handle message delivery failures through a retry mechanism. This mechanism is crucial for ensuring data integrity and robustness in a production environment.
Understanding Kafka Producer Retry Configuration
Kafka producer retries are controlled by various configuration settings that dictate how and when the producer should retry a failed send. The primary configurations related to retry strategies include:
retries: This configuration specifies the number of times the producer will retry sending a record before giving up. The default value is2147483647, implying an almost infinite number of retries.retry.backoff.ms: This setting determines the time in milliseconds the producer will wait between retry attempts. The default value is100ms.delivery.timeout.ms: This controls the total time the producer will wait for a record to be acknowledged by the topic's leader before considering the delivery a failure, including all retry attempts. The default is120,000ms(2 minutes).
These configurations can significantly impact the behavior and performance of Kafka producers, especially in error scenarios.
How Retries Work in Kafka Producer
When a Kafka producer sends a record, it waits for an acknowledgment from the leader of the topic’s partition. If the acknowledgment is not received within the specified time (request.timeout.ms), or if an error occurs indicating a potentially transient failure, the producer may retry sending the record based on the configured retry policies.
Here’s how the retry mechanism typically functions:
- A record is sent to the broker.
- If the broker does not acknowledge the record within
request.timeout.ms, or returns a retriable error (like a network error or a leader change), the producer schedules the record for a retry. - The producer waits for
retry.backoff.msbefore attempting to resend the record. - If this attempt fails, steps 2 and 3 are repeated until the number of attempts reaches the configuration specified by
retries. - If the
delivery.timeout.msis reached across all retries, the producer aborts and can optionally execute a user-defined callback to handle the failed delivery.
Practical Example
Consider a scenario where you are sending a critical notification that must be reliably delivered:
Key Points Summary Table
| Configuration Key | Default Value | Description |
retries | 2147483647 | Maximum number of retry attempts. |
retry.backoff.ms | 100 | Time between retries in milliseconds. |
delivery.timeout.ms | 120000 | Maximum time to attempt delivery (includes retries). |
Enhancing Reliability with Idempotence and Transactional Producers
Beyond simple retry strategies, Kafka also supports more advanced features like idempotent and transactional message delivery. Enabling enable.idempotence ensures that messages are not duplicated in the event of a retry. For scenarios that require all-or-nothing writes across multiple partitions, transactional producers (transactional.id configuration) can be used.
Conclusion
The Kafka Producer's retry mechanism is a powerful feature designed to ensure message delivery even in the face of errors and network issues. However, it must be configured diligently to balance system performance with data integrity. Understanding and appropriately setting configurations like retries, retry.backoff.ms, and delivery.timeout.ms are crucial in building robust Kafka-based applications.

