How does zookeeper internally achieve data consistency among leader and follower when leader fail
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache ZooKeeper is a service for coordinating distributed applications, widely used for maintaining configuration information, naming, providing distributed synchronization, and facilitating group services. One of the core aspects of ZooKeeper is ensuring data consistency across all nodes in its cluster, which comprises a single leader and multiple followers. This article delves into the mechanisms ZooKeeper employs to maintain consistency, particularly when the leader node fails.
Data Consistency Model
At its heart, ZooKeeper follows a sequential consistency model where updates from a client will be applied in the order they were sent. The whole system is based on an atomic messaging system that guarantees that a message is either delivered once or not at all, avoiding partial results.
How ZooKeeper Handles Writes and Reads
- Writes: All write requests are directed to the leader node. The leader then broadcasts these requests to the follower nodes. A write request is considered successful only when a majority of nodes (a quorum) have acknowledged receival and applied the change. This is known as Zab (ZooKeeper Atomic Broadcast) protocol.
- Reads: Read requests can be handled by any node. However, to ensure that a read returns the most recent write, ZooKeeper may direct a read to the leader or ensure that local data is the latest.
The Role of Zab Protocol
The Zab protocol is central to ZooKeeper's ability to manage data replication among cluster nodes. During normal operation, the leader processes write requests, turning them into a transaction proposal to be sent to all followers. Each follower processes the proposal and responds with an acknowledgment. Upon receiving a quorum of acknowledgments, the leader commits the transaction and informs the followers, which then also commit the transaction.
Handling Leader Failures
Leader failure is a critical event in ZooKeeper's lifecycle. Here's how ZooKeeper handles it:
- Leader Detection: Each node frequently sends heartbeat messages. If followers don't receive a heartbeat from the leader within a specified time, they initiate a "leader election”.
- Leader Election:
- ZooKeeper employs an election algorithm to choose a new leader. This might be based on node IDs or the last known transaction ID each node was aware of, which ensures confluence post-election.
- Once a new leader is elected, it proposes a new epoch (a unique, increasing identifier) to all other servers. This step is crucial to prevent split-brain scenarios and ensure that all following transactions are newer than any that occurred before the failure.
- Data Synchronization:
- Post-election, the new leader takes up the task of synchronizing state with all followers.
- This involves sending a snapshot of its state to any followers that are out of date, ensuring that all operational nodes are consistent with the new leader’s logs before processing continues.
- Resuming Operations: Once state synchronization is complete and acknowledged by a quorum, the cluster resumes normal operations under the leadership of the newly elected leader.
This mechanism ensures that the system remains robust and consistent in the face of network partitions or node failures, crucial for critical applications reliant on ZooKeeper’s services.
Summary Table
| Aspect | Details |
| Write Operations Handling | Directed to and processed by the leader, requires quorum acknowledgment |
| Read Operations Handling | Can be served by any node, with safeguards to ensure data freshness |
| Zab Protocol | Used for consensus on write transactions, involves proposal, ACK from quorum, commit, and then broadcast of commit |
| Leader Failure Detection | Triggered by missing heartbeats, provokes leader election |
| Leader Election | Based on either node IDs or the last acknowledged transaction ID, introduces new epoch |
| Data Synchronization Post-Failure | New leader sends state snapshots to out-of-date followers |
| Cluster Resumption | Operations resume once all nodes are consistent with leader's log |
Understanding these mechanisms is essential for developers and administrators managing services relying on ZooKeeper for distributed coordination and consistency.
Related reading
- How does Zookeeper/Kafka retain offset for a consumer?
- How is ordering guaranteed during failures in Kafka Async Producer?
- How is spark.streaming.kafka.maxRatePerPartition related to spark.streaming.backpressure.enabled incase of spark streaming with Kafka?
- How kafka balances partitions load?
- How is Cassandra designed to avoid the need for load balancers?
- How is ETCD a highly available system, even though it uses Raft which is a CP algorithm?
- How Kafka broadcast to many Consumer Groups
- How Kafka distributes the topic partitions among the brokers

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.