Kafka consumer disconnect - Disconnected (after 684744ms in state UP)
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 distributed event streaming platform that has the capability to handle high volumes of data efficiently. A critical component of its architecture is the Kafka consumer, which reads data from Kafka topics. However, in real-world applications, issues such as consumer disconnects can occur. An example message you might encounter is: "Disconnected (after 684744ms in state UP)." This message indicates that the consumer was previously connected and processing messages but was disconnected after about 684.7 seconds. Let's delve into what this means, the possible causes, and how to address them.
Understanding Kafka Consumer States
The Kafka consumer has several states during its lifecycle, including:
UP: The consumer is connected properly to the broker and actively consuming messages.DOWN: The consumer is not connected to the broker.REBALANCING: The consumer is in the process of joining or leaving a consumer group, a crucial part for load balancing and fault tolerance.
When a consumer shifts from the UP state to being Disconnected, it signals an issue in maintaining a steady connection with the Kafka broker.
Causes of Disconnection
There are multiple reasons why a Kafka consumer might disconnect after a substantial period in the UP state:
- Network Issues: Temporary network failures or poor network conditions can disrupt the connection between the consumer and Kafka brokers.
- Broker Failures: Issues on the Kafka broker side, such as crashes or restarts, can lead to consumer disconnections.
- Consumer Overload: If the consumer cannot process messages as fast as they are produced, it may become overwhelmed, leading to disconnections.
- Session Timeout Exceeded: If the consumer fails to send heartbeats within the session timeout period, the broker will consider it dead and cause a disconnection.
- Configuration Issues: Incorrect consumer configuration might lead to performance bottlenecks and connectivity issues.
Technical Insight: Session and Heartbeat
Kafka consumers are managed within consumer groups to ensure distributed consumption of partitions. The session.timeout.ms and heartbeat.interval.ms are significant configurations:
session.timeout.ms: This setting controls the maximum time a consumer can take without sending heartbeats to the broker before being considered dead.heartbeat.interval.ms: This setting defines the interval between heartbeats sent by the consumer to indicate it’s alive.
If the consumer fails to send heartbeats within the session timeout, it leads to a disconnection.
Strategies to Mitigate Disconnection Issues
- Optimizing Network Configuration: Ensure reliable and robust network connections. Consider using quality of service (QoS) settings if available.
- Adjusting Session Timeout and Heartbeat Interval: Fine-tuning these settings can help prevent unexpected disconnections due to minor delays in processing or network issues.
- Monitoring and Logging: Implement efficient monitoring to preemptively spot and rectify issues leading to disconnections.
- Load Balancing: Proper load distribution among multiple consumers in a group can prevent any single consumer from being overwhelmed.
Diagnostics and Monitoring Tools
Tools like Apache Kafka’s JMX metrics, Confluent Control Center, or third-party solutions like Datadog and Prometheus can be highly beneficial. These tools can monitor consumer metrics such as lag, throughput, and error rates to predict and mitigate disconnections.
Summary Table
| Issue | Cause | Resolution |
| Consumer gets disconnected | Network issues, Broker failures, Overload, Session timeout, Configuration | Optimize network, adjust timeouts, monitoring |
Final Thoughts
Disconnects in Kafka consumers can stem from a variety of issues, but careful planning, consistent monitoring, and strategic configurations can substantially mitigate such challenges. Understanding the nuances of Kafka’s operation, such as consumer states and heartbeating mechanism, is crucial for maintaining a robust Kafka ecosystem.
Related reading
- Kafka consumer does not start from latest message
- Kafka consumer doesn't consume messages from existing topic
- Kafka consumer doesn't receive messages until restarted
- Kafka Consumer Error
- Kafka Consumer Error - xxxx nodename nor servname provided, or not known
- Kafka consumer Error ERROR Unknown error when running consumer (kafka.tools.ConsoleConsumer)
- Kafka consumer error Cancelled in-flight API_VERSIONS request with correlation id 1 due to node -1 being disconnected
- Kafka Consumer error Marking coordinator dead

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.