Why should Zookeeper cluster nodes be deployed into odd numbers

Why should Zookeeper cluster nodes be deployed into odd numbers

Why must the number of Zookeeper cluster nodes be odd? When we first came into contact with this issue, we might think that Zookeeper's election mechanism requires the majority to obey the minority, and we must be odd, otherwise normal elections cannot be conducted. So is the answer really like this?


In fact, after understanding the Zookeeper cluster consistency protocol-ZAB algorithm , we found that this is not the case at all. We will find that in Zookeeper's ZAB protocol, when the Leader is elected in crash recovery, there will be a processing logic when voting:Compare ZXID first, then compare myidAccording to the example of deploying Zookeeper cluster in Zookeeper cluster deployment , we found that even if the ZXID is the same, we can also elect the Leader node by comparing myid (myid will definitely not be the same).


In addition, we can also understand the consistency protocol-Paxos algorithm , which only requires the majority of votes to pass, so we can deploy even number of units, not even nodes may cause the same number of votes. , That is, it will not cause the same number of votes because of even nodes, because not half of them fail.


We learned above all in Paxos algorithm and ZAB algorithm, theoretically come to the conclusion, then our actual examples of what can be verified, and now we have to follow before Zookeeper cluster deployment steps described to deploy a two node Zookeeper cluster, let's test whether it can succeed
Insert picture description here
Insert picture description here

Then we can start the two machines, as follows:
Insert picture description here
Insert picture description here
Insert picture description here

It is found that the number of nodes in our Zookeeper cluster is 2, that is, even numbers can be successfully deployed.




So why should the Zookeeper cluster nodes be deployed in odd numbers? If the election is not possible because of even numbers, what is the reason?
Because ofZookeeper cluster has such a feature: as long as more than half of the machines in the cluster are working normally, the entire cluster is available to the outside world.


That is to say, if there are 3 Zookeeper nodes, if 1 Zookeeper node is dead, there are 2 normal nodes left. With more than half of the surviving nodes, the Zookeeper service can still be used. However, if two Zookeeper nodes are dead and only one normal node is left, and the number of surviving nodes is not more than half, then the Zookeeper service cannot be used.

In the same way, if there are 4 Zookeeper nodes, if 1 Zookeeper node is dead, there are 3 normal nodes left. With more than half of the surviving nodes, the Zookeeper service can still be used. However, if two Zookeeper nodes are dead and only two normal nodes are left, and the number of surviving nodes is not more than half, then the Zookeeper service cannot be used.

We can send 3 Zookeeper nodes and 4 Zookeeper nodes from the same disaster tolerance, the same can be found that the disaster tolerance of 5 and 6 Zookeeper nodes, 6 and 7 Zookeeper nodes, etc. are the same, That is to say, the disaster tolerance of 2n and 2n-1 is the same, so in order to be more efficient, why not add that unnecessary zookeeper. So, based on the above, it can be concluded:From the perspective of resource saving, it is best to deploy an odd number of nodes in the Zookeeper cluster

<br/



Why are more than half of the machines in the Zookeeper cluster required to work normally, and the entire cluster is available to the outside world

But then we may have new questions, why does Zookeeper cluster have such a feature?As long as more than half of the machines in the cluster are working properly, the entire cluster is available to the outside world. This feature is actually very easy to understand, because in the Paxos algorithm and the ZAB algorithm, when we vote for elections, if the majority obey the minority, if you want to pass a proposal, you must obtain the consent of most nodes, so if the cluster More than half of the machines must survive before they can be elected by voting.

( Note: In the ZAB algorithm, it is not simple to compare ZXID first, then compare myid, and directly elect a new leader. After the comparison, each node will compare the results, re-vote, and then still follow the majority Principle of minority).




The role of more than half of the mechanism

Understand the above reasons, we can ask questions again, why both Paxos algorithm and ZAB algorithm require more than half of the mechanism? Because more than half of the mechanism can solve our --CAP BASE theory and the theory of distributed systems to bring too common problems of distributed systems, such as: 通信异常, , 网络分区, 三态, 节点故障here we take a look if the mechanism to avoid network by more than half Division, and split-brain problem.


First, let's take a closer look at the split brain problem. Now if there are 5 machines deployed a Zookeeper cluster, but they are distributed in two places A and B, of which 3 are in area A and 2 are in area B, as shown below:
Insert picture description here

At this time, a network problem suddenly occurred, and the AB area of ​​the Zookeeper cluster cannot communicate with each other because of network problems, but the areas can communicate with each other.


Now suppose there is no limit of more than half of the mechanism , because there is a Leader node in the A area, there is no impact, but there is a problem in the B area, and now it is unable to communicate with the A area, so now the B area will conduct the election of the Leader, because it is assumed that there is More than half of the restrictions of the mechanism, so the election can be successful, as follows:
Insert picture description here

This is equivalent to the original one cluster, which was divided into two clusters and two Leaders appeared. This is the so-called "brain split" phenomenon. In fact, it can also be seen from this situation that a cluster that originally provided services externally has now become two clusters providing services externally at the same time.


Assuming that the network has suddenly recovered and the areas AB can communicate with each other again, then problems will arise at this time. The two clusters have just provided external services, how to merge data, how to resolve data conflicts, and so on.One of the prerequisites when the above scenario occurs is that no more than half of the mechanism is considered, so in fact, the split brain problem will not easily occur in the Zookeeper cluster, because the half of the mechanism is the reason.


Because there is more than half of the mechanism, it is impossible to re-elect a new leader in area B, and area B will not provide services to the outside. After the network is restored, area B will synchronize data from area A to ensure its consistency.


In fact, more than half of the mechanism can not only avoid the problem of split brain, it can also help us to elect quickly, because more than half of the mechanism, compared with 2PC / 3PC, we don't need to wait for all nodes to vote for the same node to elect one Leader, this is faster, so it is called the fast leader election algorithm .




So the reason why our Zookeeper cluster nodes are deployed into odd numbers is the above? In fact, it is not only the above-mentioned perspective of saving resources, but also has the purpose of,Deploying odd numbers can improve the disaster tolerance of Zookeeper servicesAs mentioned above, if there is a network problem, there is always one of the areas A and B that can provide services to the outside. Because of the deployment of odd numbers, no matter how the areas A and B are divided, a leader (or the original leader) can be elected to manage the service.


However, if an even number of units are deployed, the situation in areas A and B is as follows. Because of the over half mechanism, it is impossible to provide services from the outside, so the entire Zookeeper cluster will collapse.
Insert picture description here

286 original articles published · Liked12 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/newbie0107/article/details/105024891