Kafka Producer/Consumer reconnect after kafka node failure
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 capable of handling trillions of events a day. It has become a popular tool for managing large streams of data efficiently and is notably resilient due to its distributed nature. However, node failures can and do occur, necessitating a robust mechanism for handling reconnections by Kafka producers and consumers. This article explores the process, challenges, and solutions for reconnecting Kafka producers and consumers after a Kafka node failure.
Understanding Kafka's Architecture
Before delving into reconnections, it’s crucial to understand the basic architecture of Kafka. Kafka clusters consist of several servers (nodes), each of which can handle reads and writes of data records. In Kafka terminology:
- Producers publish data to topics.
- Consumers subscribe to topics and read data.
- Brokers are Kafka servers that store data and serve clients.
- Topics are categories for messages, which are split into partitions for scalability and redundancy.
Kafka Producer Reconnection Mechanism
Kafka producers are designed to automatically handle transient failures while sending records to a Kafka broker. Each producer maintains a list of brokers from the cluster metadata and connects to the broker that is the leader for the partition to which data is being published. If the connected node fails, here’s what happens:
- Detection of Failure: The producer detects a node failure typically through a timeout. This occurs if the producer cannot receive an acknowledgment from the broker within a configured
request.timeout.ms. - Update Metadata: On detection of a failure, the producer will refresh its metadata to get the latest view of the cluster, identifying which nodes are alive and which partitions they are leading.
- Reconnection Attempt: The producer will then attempt to reconnect to the new leader of the partition it was previously sending data to. If the new leader has not yet been elected, the producer retries the metadata refresh after a backoff specified by
retry.backoff.ms.
Here is a simple example with Kafka's Java client:
Kafka Consumer Reconnection Mechanism
Kafka consumers utilize a similar mechanism to handle node failures:
- Failure Detection: Like producers, consumers detect failures when they cannot poll data from the broker.
- Consumer Group Rebalance: Consumers in a group coordinate with each other to handle failures. If a consumer can’t poll data due to a broker failure, it triggers a group rebalance. During this, consumers stop consuming, refresh their metadata, and then resume.
- Reconnecting to New Leaders: Once the new leader is elected and the group rebalance is complete, consumers start fetching data from the new brokers assigned to them.
Here’s a quick example using Kafka's Java client:
Challenges in Handling Reconnections
Despite Kafka's robust mechanisms, several challenges may arise:
- Missed Data: If producers have
acksset to0or1, there’s a risk of losing data if a node fails before all copies of data are stored. - Reconnection Loops: Continuous node failures can lead to repeated reconnection attempts, increasing latency and reducing throughput.
Summary
| Aspect | Description |
| Failure Detection | Timeout and error handling in clients. |
| Reconnection Strategy | Metadata refresh, backoff strategies. |
| Configuration | Timeout settings, error handlers, retry policies. |
Conclusion
Node failures in a Kafka cluster are not uncommon and handling them efficiently is key to maintaining data integrity and service availability. Both Kafka producers and consumers are equipped with mechanisms that allow them to recover from such failures by reestablishing connections to new leaders. Proper configuration and understanding of Kafka's client libraries are vital to leverage these mechanisms effectively.
Related reading
- kafka producers are very slow
- Kafka produce.send never sends the message
- Kafka python consumer reading all the messages when started
- Kafka python graceful shutdown of consumer
- Kafka Quorum-based approach to elect the new leader?
- kafka readiness probes failing
- Kafka QuickStart, advertised.host.name gives kafka.common.LeaderNotAvailableException
- Kafka Rebalancing and listeners pitfalls

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.