How Does ZooKeeper Keep Data Consistent? The Paxos Algorithm Explained
ZooKeeper achieves strong data consistency across its server cluster by implementing the Paxos consensus algorithm, where clients propose changes, servers vote on proposals with monotonically increasing IDs, and a leader coordinates writes to ensure only one operation is committed at a time.
ZooKeeper is a cluster of multiple servers, each of which can be connected by many clients that can read and modify the data stored on the servers.
ZooKeeper guarantees that the data on every server is exactly the same by using the Paxos algorithm, the foundational distributed consensus algorithm.
Paxos Basic Idea
Imagine a club with two roles: members and councilors (the decision‑making group). Members submit proposals to councilors to change club rules.
Councilors gather, vote on each proposal, and a proposal passes if it receives a majority of votes. Each proposal has a monotonically increasing ID.
Each councilor keeps a notebook containing the current rules and the highest proposal ID it has processed (initially 0). A proposal is accepted only if its ID is greater than the ID in the notebook; the councilor then updates its notebook with the new ID.
When a majority of councilors approve a proposal, it becomes effective and all councilors update their notebooks accordingly. Subsequent queries can be answered directly from the notebook.
In the case of concurrent proposals, only the one that is processed first will have an ID greater than the current notebook ID; the other proposal will be rejected and must be retried.
This mechanism maps to ZooKeeper as follows:
Member – client
Councilor – server
Councilor’s notebook – server’s data
Proposal – data‑change request
Proposal ID – zxid (ZooKeeper Transaction ID)
Proposal execution – applying the data change
ZooKeeper also introduces a leader role, which centralizes the right to initiate proposals, preventing multiple servers from issuing competing proposals simultaneously.
Through voting, a global numbering scheme, and leader coordination, Paxos ensures that at any moment only one write operation is approved, while competing writes must retry after losing the vote.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
