Kafka Consumer
Reconnection
Disconnection Issues
Troubleshooting
Connectivity Management

Kafka consumer reconnection after getting disconnected

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka is a popular distributed event streaming platform used by many organizations to handle real-time data feeds. Kafka’s ability to handle large volumes of data makes it a significant tool in the analytics and data processing landscape. One of the critical components of Kafka is its Consumer API, which allows applications to read (or consume) data from Kafka clusters. However, during operation, a Kafka consumer might get disconnected due to network issues, broker failures, or other unforeseen problems. Ensuring robust reconnection logic is vital for maintaining data consistency and application resilience. Here's an in-depth look at how Kafka consumers can manage disconnections and reconnect effectively.

Understanding Kafka Consumer Architecture

A Kafka consumer subscribes to one or more topics and reads data in the form of records from the brokers. Consumers maintain a connection to the broker to pull new messages and keep track of their progress using offsets in the consumed topics.

Reasons for Consumer Disconnections

Consumers can get disconnected from a broker for several reasons:

  • Network Issues: Temporary network failures can disrupt the connection between the consumer and the Kafka cluster.
  • Broker Failures: Issues like broker crashes can lead to a sudden loss of connection.
  • Configuration Changes: Updates in the Kafka configuration or network settings might require reconnections.
  • Load Balancers: In cloud environments, load balancers might drop connections that appear inactive.

Reconnecting Kafka Consumers

Here’s how Kafka Consumers usually handle reconnections:

Automatic Reconnection

Kafka clients, including consumers, are designed to handle transient failures by automatically retrying connections to the broker. The Consumer API in the Kafka client automatically tries to reconnect to the broker if the session times out or a connection is lost. This behavior is controlled by various configuration parameters:

  • reconnect.backoff.ms: This setting specifies the amount of time the client will wait before attempting to reconnect to a given host. This helps to avoid continuous connection attempts.
  • reconnect.backoff.max.ms: The maximum amount of time in milliseconds to backoff/wait when reconnecting to a broker that has repeatedly failed to connect.

Manual Handling

While automatic reconnection works for most scenarios, there might be cases where manual intervention is necessary, especially if the reconnection needs to ensure certain conditions are met (like rebalancing partitions among consumers).

java
1Properties props = new Properties();
2props.put("bootstrap.servers", "localhost:9092");
3props.put("group.id", "test-group");
4props.put("enable.auto.commit", "true");
5props.put("auto.commit.interval.ms", "1000");
6props.put("session.timeout.ms", "30000");
7props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
8props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
9
10KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
11try {
12    consumer.subscribe(Arrays.asList("my-topic"));
13    while (true) {
14        ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
15        for (ConsumerRecord<String, String> record : records) {
16            System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
17        }
18    }
19} catch (WakeupException e) {
20    // Ignore for shutdown logic
21} finally {
22    consumer.close();
23}

Handling Longer Disconnections

For disconnections that last longer than the session.timeout.ms or if the consumer stops responding for some reason (like JVM pauses), the broker might consider the consumer dead and trigger a rebalance. To manage this, you should:

  • Set appropriate values for session.timeout.ms and heartbeat.interval.ms.
  • Handle ConsumerRebalanceListener to manage consumer’s state and offset commits when partitions are reassigned.

Key Configuration Parameters

ParameterDescriptionDefault Value
reconnect.backoff.msInitial wait before retrying a connection to the broker50 ms
reconnect.backoff.max.msMaximum wait time for retries1000 ms
session.timeout.msTime after which a missing consumer is considered dead10000 ms
heartbeat.interval.msFrequency of heartbeats to the broker to indicate liveness3000 ms

Conclusion

Handling reconnections in Kafka consumers is critical for building reliable and fault-tolerant streaming applications. By understanding and configuring consumer properties appropriately, you can ensure that your Kafka consumers are robust against network and broker issues. Use both automatic and manual handling strategies depending on the situation to maintain smooth and efficient data consumption from Kafka topics.


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.