What are the Zookeeper Leader Election?
A server selected from a group of servers is what we call a leader. In addition, the leader continues to be supported by this ensemble. Basically, the main purpose of the Leader is to sequence client requests that change the state of ZooKeeper.
How does leader election in ZooKeeper work?
Leader's choice. A simple way to perform leader election with ZooKeeper is to use the SEQUENCE|EPHEMERAL flag when creating a Znode representing a "suggestion" from a client. The idea is to have a znode say "/election" so that each znode creates a child znode "/election/guid-n_" with two flags SEQUENCE|EPHEMERAL.
Let us analyze how a leader node may be elected in a ZooKeeper ensemble. take into account there are N number of nodes in an exceedingly cluster. the process of leader election is as follows −
All the nodes produce a ordered, transient znode with a similar path, /app/leader_election/guid_.
ZooKeeper ensemble can append the 10-digit sequence range to the trail and also the znode created are going to be /app/leader_election/guid_0000000001, /app/leader_election/guid_0000000002, etc.
For a given instance, the node that creates the littlest range within the znode becomes the leader and every one the opposite nodes are followers.
Each follower node watches the znode having future smallest number. as an example, the node that creates znode /app/leader_election/guid_0000000008 can watch the znode /app/leader_election/guid_0000000007 and also the node that creates the znode /app/leader_election/guid_0000000007 can watch the znode /app/leader_election/guid_0000000006.
If the leader goes down, then its corresponding znode /app/leader_electionN gets deleted.
The next in line follower node can get the notification through watcher about the leader removal.
The next in line follower node can check if there are different znodes with the smallest range. If none, then it'll assume the role of the leader. Otherwise, it finds the node that created the znode with the smallest range as leader.
Similarly, all different follower nodes elect the node that created the znode with the smallest range as leader.