Raft's Leader Election Protocol

Learn about Raft's leader election protocol.

Leader election

All servers start as followers in Raft. A server keeps this state as long as it receives valid heartbeat RPCs (AppendEntries RPCs with no log entries) from the leader, which the leader uses to ascertain its authority. As soon as a follower stops receiving these heartbeat RPCs for a specific time duration, known as election timeout, it assumes that there is no valid leader currently. It changes its state to a candidate, starting a new election.

To begin an election, a server goes through the following three stages:

  1. A follower increments its current term number and transitions into a candidate state.
  2. A candidate votes for itself and sends RequestVotes RPCs parallelly to other servers.
  3. This election’s outcome determines the candidate’s next state. It can be either one of the following three cases: election won, election lost, or election drawn.

Case I (election won)

A candidate wins the election and establishes itself as the leader.

A candidate can emerge as a winner if it obtains votes from a majority of the servers in the complete cluster during the same term. In each term, a server can vote for only one candidate based on the order in which they arrive. The rule of majority guarantees that only one candidate can win the election for a specific term. The winning candidate becomes the leader and sends heartbeat messages to all other servers to assert its authority and stop further elections.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.