In a distributed cluster, a master node is required to coordinate and manage the other nodes. The existence of the primary node ensures the orderly operation of other nodes and the consistency of data written in the database cluster on each node.

Several common distributed election algorithms are introduced here.

Leant algorithm

In Bully algorithm, there are two roles of nodes: common node and primary node. Its election principle is “elder”, that is, among all the living nodes, the node with the largest ID is selected as the primary node.

Bully algorithm needs the following three messages in the election process:

  • Election message, used to initiate an Election;
  • An Alive message is an answer to an Election message.
  • Victory message: a message that the successful primary node sends to other nodes to pledge its sovereignty.

The assumption is that each node in the cluster knows the IDS of other nodes.

The election process is as follows:

  1. Each node in the cluster determines whether its ID is the largest among the existing nodes. If so, it directly sends Victory message to other nodes to swear its sovereignty.
  2. If you are not the node with the largest ID currently alive, send the Election message to all nodes with the largest ID and wait for the other nodes to reply.
  3. If the node does not receive the Alive message from other nodes within a given time range, it considers itself as the primary node and sends Victory message to other nodes to swear itself as the primary node.
  4. If it receives the Alive message from the node whose ID is larger than its own, it waits for other nodes to send Victory message.
  5. If the node receives an Election message from a node whose ID is smaller than its own, it replies with an Alive message telling the other nodes that I am older than you and that the Election is reheld.

Advantages:

  • Fast elections
  • Low algorithm complexity
  • Easy to implement

Disadvantages:

  • Extra information is stored more
  • Nodes with ids larger than the primary node frequently enter or exit the cluster, which can trigger multiple elections.

Raft algorithm

In Raft algorithm, the node with the most votes becomes the main node.

Role:

  • The Leader node has only one Leader at a time to coordinate and manage other nodes.
  • Candidate: Each node can be a Candidate. Only in this role can a node be elected as the new Leader.
  • Followers of the Follower Leader are not allowed to initiate elections.