The full name of the ZAB protocol is Zookeeper Atomic Broadcast Protocol. Function: The ZAB protocol can be used to synchronize data between active and standby nodes in a cluster to ensure data consistency. Before explaining the ZAB protocol, we must understand the role of each Zookeeper node. The role of each Zookeeper nodeLeader:
Follower:
Observer:
How to choose a Leader during the startup phase?When Zookeeper is first started, multiple nodes need to find a leader. How do they find one? By voting. For example, there are two nodes in the cluster, A and B, and the schematic diagram is as follows:
At this time, the voting information of node A is (2, 0), as shown in the following figure:
What should I do if the Leader crashes during operation?During the operation of Zookeeper, the Leader will remain in the LEADING state until the Leader crashes. At this time, the Leader must be re-elected, and the election process is basically the same as the election process in the startup phase. Points to note:
How to synchronize data between nodes?Different clients can connect to the primary node or the backup node separately. When the client sends a read or write request, it does not know whether it is connected to the Leader or the Follower. If the client is connected to the master node and sends a write request, the Leader will execute 2PC (two-phase commit protocol) to synchronize with other Followers and Observers. However, if the client is connected to a Follower and sends a write request, the Follower will forward the write request to the Leader, and then the Leader will perform 2PC to synchronize data to the Follower. Two-phase commit protocol:
Let's take a look at the process of Leader synchronizing data:
Next, let's see how the Follower handles the proposal transaction request sent by the Leader:
Now the data of the Leader and Follower are all in the memory and are consistent. The data read by the client from the Leader and Follower are consistent. How does ZAB achieve sequential consistency?When the Leader sends a proposal, it actually creates a queue for each Follower and sends the proposal to their respective queues. The following figure shows the message broadcast process of Zookeeper: The client sent three write transaction requests, and the corresponding proposals are: proposal01 : zxid1 After receiving the request, the Leader puts it into the queue one by one, and then the Follower gets the request from the queue one by one, thus ensuring the order of the data. Is Zookeeper strongly consistent?Official definition: sequential consistency. Strong consistency is not guaranteed, why? Because after the Leader sends the commit message to all Followers and Observers, they do not complete the commit at the same time. For example, due to network reasons, different nodes receive commits later, so the submission time is also later, resulting in inconsistent data among multiple nodes. However, after a short period of time, when all nodes commit, the data will be synchronized. In addition, Zookeeper supports strong consistency, which means manually calling the sync method to ensure that all nodes are committed for success. Here is a question: If a node fails to commit, will the leader retry? How to ensure data consistency? Welcome to discuss. Leader downtime data loss issueFirst case: Assume that the Leader has written the message to the local disk but has not yet sent a proposal to the Follower. At this time, the Leader crashes. Then you need to select a new leader. When the new leader sends a proposal, the zxid auto-increment rule included in it will change:
When the old Leader recovers, it will become a Follower. When the Leader sends the latest proposal to it, it finds that the high 32 bits of the zxid of the proposal on the local disk are smaller than the proposal sent by the new Leader, so it discards its own proposal. Second case: If the Leader successfully sends a commit message to the Follower, but all or some of the Followers have not had time to commit the proposal, that is, load the proposal from the disk into the memory, then the Leader crashes. Then you need to select the Follower with the largest zxid in the disk log. If the zxids are the same, compare the node IDs and use the one with the larger node ID as the Leader. This article tries to use plain language + drawings to explain, hoping to inspire everyone. |
There is no doubt that 5G is the new technology o...
80VPS, a long-established Chinese hosting company...
All Internet services rely on the TCP/IP protocol...
As 2022 is coming to an end, the Chinese business...
ping command The ping command is used to test the...
Infovista welcomes TM Forum’s new industry survey...
The traditional Chinese New Year is approaching, ...
In the past two days, Linode released a blog post...
Recently, the State Council issued the "Guid...
Author: Wang Yingyue, Unit: China Mobile Smart Ho...
The Internet of Things is the application that is...
The tribe once shared information about MoonVM in...
More than a year after its official commercial la...
At the beginning of 2022, we received New Year pr...
When you use WiFi at home to play games, you alwa...