Kafka how to set producer retries to Infinity
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a widely used distributed event streaming platform capable of handling trillions of events a day. In scenarios where dependability and delivery assurance are crucial, such as financial transactions or critical monitoring systems, ensuring that messages are successfully produced is paramount. One of the settings available to Kafka producers to handle message delivery failures is the retries configuration.
Understanding Kafka Producer Retries
When producing messages to a Kafka topic, certain errors (like a temporary loss of connection to the Kafka broker or a leader election when the preferred leader is not available) may cause message production to fail. To mitigate this, Kafka includes a retry mechanism that can repeat the send operation a specified number of times.
The retries setting in Kafka's producer configuration defines how many times the producer retries a failed send operation before giving up. Setting this parameter to a higher value or even effectively infinite can be crucial in environments where data loss cannot be tolerated.
Setting Producer Retries to Infinity
To achieve near-infinite retries, we cannot literally set retries to infinity (∞), as the setting expects a numerical value. Instead, we set this to a very high integer value, such as Integer.MAX_VALUE (which is 2147483647 in Java).
Here is how you can set up the producer configuration for virtually infinite retries:
Key Configuration Settings
When setting retries to a high value, it's also important to adjust other related configurations to ensure that the retries work as expected:
delivery.timeout.ms: This setting should be high enough to allow sufficient time for retries. Setting this toInteger.MAX_VALUEeffectively removes timeout limitations, but this might be risky and needs careful handling.max.block.ms: The time the producer will wait for buffer space to become available. Increase this value appropriately to handle cases where retries might fill up the buffer.
Risks and Considerations
While setting retries to a very high number can ensure higher message delivery success rates under normal circumstances, it also introduces risks and issues:
- Performance Impact: High retries can lead to longer times to detect unresolved issues, affecting system performance and resource utilization.
- Deadlocks and Resource Starvation: In a worst-case scenario, excessive retrying could lead to resource starvation, affecting not just the producer but potentially other clients of the Kafka cluster as well.
Summary Table
| Configuration Key | Recommended Value | Description |
retries | Integer.MAX_VALUE | Set to a high value to enable near-infinite retries. |
delivery.timeout.ms | Integer.MAX_VALUE | Allows sufficient time for all retries to be attempted. |
max.block.ms | 300000 (or higher) | Blocks producer if buffer is full, increase if retries are high. |
In conclusion, configuring Kafka producer retries to a near-infinite number is a viable strategy for critical applications where data loss is unacceptable. However, it requires a careful balancing act with other configuration settings and system resources to ensure that it does not backfire and degrade overall system performance or stability. When implementing such configurations, thorough testing and monitoring are recommended to gauge the impact under different scenarios.
Related reading
- Kafka Idempotent producer
- Kafka ignoring `transaction.timeout.ms` for producer
- Kafka in distributed system
- Kafka in Kubernetes - Marking the coordinator dead for group
- Kafka in Kubernetes Cluster- How to publish/consume messages from outside of Kubernetes Cluster
- Kafka incremental sticky rebalancing
- Kafka inaccessible once inside Kubernetes/Minikube
- Kafka input to logstash plugin

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.