Elections in Redis and Zookeeper

Foreword

In a recent review of the preparation, thinking about the string of a string of previous knowledge, fragmented always easy to forget, or to knowledge into the system, which is an essential quality of Internet engineers, or else have a new day knowledge, until 35 but also how to fight and young people, only precipitate their own thing, in order not to be eliminated.

Now everywhere distributed, highly concurrent, we use a lot of things like databases and the like to the production environment will deploy the cluster, each machine is a node, a lot of scenes from the turn divided into the main node, such as write from the master read like ah, of course, at this point ultimately linked to the case of a node, the node linked to the general problem is not large, and now the failover variety of work has been done is in place, after all, to ensure high availability Well. Today to chatter about, after the node linked to a number of election-related work, the paper mainly Zookeeper and Redis an example to illustrate.

Redis Sentinel election lead

Sentinel is to ensure Redis achieve high availability . Sentinel system is to monitor the effect of Redis server cluster, it can be obtained redis cluster stop state, when hanging a master node, a failover operation will elect a new master node from the node, where the transfer is to be dominated by the fault Sentinel Finished.

Sentinel not to think too complicated, it is actually a special operating mode of the Redis server only, Redis is a cluster deployment, Sentinel here is also to deploy clusters, to a non-single-deployment, your Sentinel hung up, this time Redis cluster on the GG.

Then I said above, when the primary server node hanging, the system will select a Sentinel the lead Sentinel accomplished failover works. Election rules are as follows:

  • Monitoring the hanging of the master node to all Sentinel has been elected as the lead qualification

  • Once for each election, whether or not successful, era + 1 configuration , the configuration is a counter era

  • In each Sentinel has one and only one chance each configuration epoch election , once selected the master node of the node think, in this era, can not be changed

  • Each server is linked to the discovery of Sentinel will be configured era +1 and cast their vote, followed by a message asking other Sentinel set itself as the leader 1, each wants to be the leader of the Sentinel

  • Each request sent by Sentinel will be the first node is set to lead their lead node and send a reply, who should come to me whom to

  • When the source Sentinel received a reply, and configure era in the response and consistent and lead their own Sentinel Id is when Id, indicates that the target Sentinel has its own set lead

  • In a configuration era, when a Sentinel received more than half of the agreed reply, it is the leader of the

  • If, within a given time, have not been successful election Sentinel, then over time to initiate new elections

Election procedures and rules leading Sentinel's probably mentioned above, should be noted that only the nodes only need to hang the elected leader Sentinel, Sentinel is usually equal status of each cluster appears ~

The election here is actually an application raft algorithm, interested partners can read small papers under this algorithm

Zookeeper election

Zookeeper is a highly distributed data consistency solutions such dubbo the registry on the use of Zookeeper. Of course, this is a cluster deployment, but it does not use the traditional Master / Slave architecture, but the introduction of the Leader, Follwer and Observer. Leader and Follower like Master / Slave, the new Observer role is just to increase read performance of the cluster, it does not participate in Leader election.

Node status are the following:

  • LOOKING: the node is in a state primary election, does not provide services until the end of the primary election;

  • FOLLOWING: a node from the system, the master node receiving the update and writes the local log;

  • LEADING: as the system master node, receiving client updates, write and copy to the local log from the node

Zookeeper state synchronization protocol is based on the Zab, Zab protocol has two modes, which are the crash recovery (selected from the master) and the broadcast message (synchronous). When the service starts or after Leader collapse, Zab entered recovery mode, when the Leader is elected, and more than half completed and leader of machine state after synchronization, recovery mode is over.

Let's look at selected main focus is how to complete

First, clear a few concepts:

  • Sid : server id;

  • Zxid : the new server transaction id, data, larger zxid; zxid Epoch is a 32-bit high, the low 32-bit id is self-energizing in zpoch, starting from 0. Every time to elect a new Leader, epoch will increase, while the lower 32 zxid cleared.

Is selected from the main flow as a whole

  1. Status changes. When the server is started when the status of each server's Looking, if the leader is hung into the election, then the rest of the non-Observer's Server will change your server status Looking, then began to enter the state election of Leader;

  2. Poll. Each server will have a (sid, zxid) voting system initialization time zxid is 0, if it is during operation, each server is zxid may have different, depending on the data of the last update. The vote sent to all machines in the cluster;

  3. Receives and checks the voting. After the server receives a vote, will first check whether the current round of voting, if the server from looking state;

  4. Voting process. Their vote and vote received were PK:

Check zxid, greater priority for the leader; if zxid the same, sid large as leader; update their vote based on PK results, send their vote again ;

  1. Statistical vote. After each ballot, voting information server statistics, if there is more than half received the same voting machine, then the leader is generated , and if not, then the next round of voting conducted;

  2. Change the server status. Once the Leader, server will update their status Following or Leading. End of the election.

We want to ensure that after the election the main completed, the transaction has been submitted to the original primary node continues to complete the submission; the original primary node only proposed transaction and did not submit to be abandoned. This is why tend to choose zxid largest node from the master node, because it is on top of the latest transaction, the most consistent with the original master node.

to sum up

  1. Is selected from the main Redis Sentinel relatively more simple, because the consistency of the transaction does not involve state

  2. Sentinel master election protocol is based on the raft, Zookeeper protocol based on Zab

  3. Both are to receive half the votes cast on election success

  4. Sentinel is the main content of a message vote Sentinel id and configure era, Zookeeper is zxid and sid

  5. Sentinel vote who should come to him who he would vote, Zookeeper is in comparison to some careful examination, including examination epoch and node status, check before you start pk with their vote, and then to see or need to update their the vote, if necessary, their vote should be broadcasted

    Original Public No.

Guess you like

Origin juejin.im/post/5df60245f265da33f34b362f