Kafka producer send blocks indefinitely when kafka servers are down
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
When designing systems that interact with distributed streaming platforms like Apache Kafka, one common challenge developers might face is handling scenarios when Kafka servers (brokers) are temporarily unavailable. In these contexts, understanding the behavior of Kafka producers is crucial, particularly concerning how they manage their send operations during outages.
Kafka Producer Basics
Apache Kafka producers are responsible for sending records (messages) to Kafka brokers. The basic workflow involves creating a ProducerRecord, which specifies the target topic and the message (key-value pair), and then using the send() method of KafkaProducer to send the record to a Kafka broker.
The success or failure of these send operations highly depends on the Kafka cluster's state and the producer's configuration. Producers are configured with various settings that dictate their behavior in failure scenarios, including metadata.fetch.timeout.ms, delivery.timeout.ms, retries, and max.block.ms.
Behavior Under Server Outages
By default, a Kafka producer will attempt to send messages even when the associated Kafka brokers are down. The specific behavior under these conditions is guided by the aforementioned configurations:
- retries: This configuration specifies the number of times the producer will retry sending a message before giving up.
- delivery.timeout.ms: This setting defines the duration after which the producer will stop retrying and consider the send operation failed.
- max.block.ms: Controls the time the producer will block when calling
send()or when using methods likepartitionsFor(). If this time elapses without being able to send metadata or allocate memory for the record (because the buffer is full), a TimeoutException is thrown.
When Kafka brokers are down, the producer will continue to retry sending messages according to the retries and retry.backoff.ms settings. However, if the brokers remain unavailable for longer than max.block.ms while attempting to send a message or metadata.fetch.timeout.ms while fetching metadata, then the producer blocks, potentially indefinitely if the settings allow. This situation can lead to severe application stalls if not properly handled.
Technical Example
Consider a Java example where a Kafka producer tries to send messages while Kafka is down. Below is a simplified configuration and sending logic:
In this example, if Kafka is down, after 5 seconds (max.block.ms), a TimeoutException would be thrown, assuming that the broker does not recover within retry attempts and timeout durations.
Key Points Summary
| Configuration | Default Value | Description |
retries | INT_MAX | Number of retry attempts when sending messages fails. |
retry.backoff.ms | 100 | Time to wait between retries. |
delivery.timeout.ms | 120000 (2 min) | Maximum time to attempt message delivery before failing. |
max.block.ms | 60000 (1 min) | Maximum time to block on buffer full or metadata fetch. |
metadata.fetch.timeout.ms | 60000 (1 min) | Timeout for fetching metadata from the broker. |
Conclusion
By understanding these configurations and behaviors, developers can better design Kafka clients that are robust against Kafka broker outages. Proper handling and awareness of potential indefinite blocking can prevent Kafka applications from stalling and ensure more resilient data flows within systems. Adjustments in these settings should reflect the criticality of message delivery timelines and system tolerance for delays.
Related reading
- Kafka producer send message expiring duo to 30003 ms has passed since last append
- Kafka Producer terminating with 1 message (881 bytes) still in queue or transit
- kafka producer throw EOFException during running
- Kafka producer throws an error Invalid transition attempted from state IN_TRANSACTION to state IN_TRANSACTION
- Kafka producer throws Received unknown topic or partition error when sending to topic created via AdminClient createTopics method
- Kafka Producer TimeOutException
- Kafka producer TimeoutException Expiring 1 record(s)
- Kafka producer to read data files

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.