Kafka Producer
Server Downtime
Troubleshooting
Data Streaming
System Errors

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.

Practice system design

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 like partitionsFor(). 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:

java
1Properties props = new Properties();
2props.put("bootstrap.servers", "localhost:9092");
3props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
4props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
5props.put("retries", 10);
6props.put("delivery.timeout.ms", 30000);
7props.put("max.block.ms", 5000);
8
9Producer<String, String> producer = new KafkaProducer<>(props);
10try {
11    producer.send(new ProducerRecord<>("test-topic", "key", "value")).get();
12} catch (TimeoutException e) {
13    System.err.println("Timeout while sending message: " + e.getMessage());
14} catch (InterruptedException | ExecutionException e) {
15    e.printStackTrace();
16} finally {
17    producer.close();
18}

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

ConfigurationDefault ValueDescription
retriesINT_MAXNumber of retry attempts when sending messages fails.
retry.backoff.ms100Time to wait between retries.
delivery.timeout.ms120000 (2 min)Maximum time to attempt message delivery before failing.
max.block.ms60000 (1 min)Maximum time to block on buffer full or metadata fetch.
metadata.fetch.timeout.ms60000 (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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.