Zookeeper
Data Consistency
Leader-Follower Model
System Failure
Distributed Systems

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.

Practice system design

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:

  1. 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”.
  2. 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.
  3. 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.
  4. 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

AspectDetails
Write Operations HandlingDirected to and processed by the leader, requires quorum acknowledgment
Read Operations HandlingCan be served by any node, with safeguards to ensure data freshness
Zab ProtocolUsed for consensus on write transactions, involves proposal, ACK from quorum, commit, and then broadcast of commit
Leader Failure DetectionTriggered by missing heartbeats, provokes leader election
Leader ElectionBased on either node IDs or the last acknowledged transaction ID, introduces new epoch
Data Synchronization Post-FailureNew leader sends state snapshots to out-of-date followers
Cluster ResumptionOperations 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.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
System Design practice on Codemia

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

Practice system design