Zookeeper Leader Election: How is Leader Elected?
In our last Apache ZooKeeper tutorial, we discussed ZooKeeper Workflow. Today, we will see the ZooKeeper Leader Election. Basically, an ensemble of servers chooses a leader to order client requests that change the ZooKeeper state.
Though there is a complete process for it. So, in this Zookeeper article, we will learn the whole process of Leader election in ZooKeeper in detail. Moreover, we will learn how is leader elected in ZooKeeper.
So, let’s start ZooKeeper Leader Election.
What is Zookeeper Leader Election?
A server that has been chosen by an ensemble of servers, is what we call a Leader. Also, that leader continues to have support from that ensemble. Basically, to order client requests that change the ZooKeeper state is the main purpose of Leader.
Zookeeper State includes create, setData, and delete. Also, it transforms each request into a transaction. Furthermore, it proposes to the followers that the ensemble accepts and applies them in the order issued by the leader.
However, it enters the ELECTION state, when a process starts. And, the process tries to elect a new leader or become a leader, in this state.Â
Hence, the process moves to the FOLLOWING state and begins to follow the leader when it finds an elected leader. So, we can say the processes in the FOLLOWING state are followers.
Moreover, the process moves to the LEADING state and becomes the leader, if the process is the leader after the election.Â
How is Leader elected in Zookeeper?
While creating znodes that represent “proposals” of clients, a simple way of doing leader election with ZooKeeper is to use the SEQUENCE|EPHEMERAL flags. Its basic concept is to have a znode, say “/election”, such that each znode creates a child znode “/election/n_” with both flags SEQUENCE|EPHEMERAL.
Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!
A sequence number which is higher than anyone previously appended to a child of “/election”, that is automatically appended by Zookeeper, with the sequence flag. In other words, Leader is a process which created the znode with the smallest appended sequence number.
Although make sure it is must watch for failures of the leader. Because if in the case the current leader fails, that helps a new client arises as the new leader.
There is one other solution for that. It is that to have all application processes keeping an eye upon the current smallest Znode, and also keep checking that when the smallest Znode goes away if they are the new leader. Although this results in a herd effect.
That is all other processes receive a notification and execute getChildren on “/election” to obtain the current list of children of “/election”, upon of failure of the current leader. Also, it causes a spike in the number of operations that ZooKeeper servers have to process, if the number of clients is large.Â
The pseudo code for ZooKeeper Leader Election is:
Let’s suppose ELECTION be a path of choice of the application. So, in order to volunteer to be a leader:
with path "ELECTION/n_", Create Znode z with both SEQUENCE and EPHEMERAL flags; Then let's suppose i be the sequence number of z and C be the children of "ELECTION"; Also, where j is the largest sequence number such that j < i and n_j is a Znode in C, watch for changes on "ELECTION/n_j";
Further, upon receiving a notification of Znode deletions:
Let’s suppose C be the new set of children of ELECTION;
Then execute leader procedure, if z is the smallest node in C;
Else, where j is the largest sequence number such that j < i and n_j is a Znode in C, watch for changes on “ELECTION/n_j”;
However, make sure that the Znode having no preceding znode on the list of children. Though it says it does not imply that the creator of this znode is aware that it is the current leader.
And, to acknowledge that the leader has executed the leader procedure, applications may consider creating a separate Znode.
So, this was all in Apache ZooKeeper leader election. Hope you like our explanation.
Conclusion
Hence, in this ZooKeeper leader election tutorial, we have seen the whole Leader election in Apache ZooKeeper in detail. Moreover, we discussed how is leader elected in ZooKeeper. Still, if any doubt occurs regarding the process of ZooKeeper election, feel free to ask in the comment section.
Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google