Raft Leader Election

Initiate an Election

  • When servers start up, they begin as followers.

  • A server remains in follower state as long as it receives valid RPCs from a leader or candidate.

  • Leaders send periodic heartbeats (AppendEntries RPCs that carry no log entries) to all followers in order to maintain their authority.

  • If a follower receives no communication over a period of time called the election timeout, then it assumes there is no viable leader and begins an election to choose a new leader.

  • To begin an election, a follower increments its current term and transitions to candidate state.

  • It then votes for itself and issues RequestVote RPCs in parallel to each of the other servers in the cluster.

  • A candidate continues to be a candidate until one of three things happens:

    • it wins the election
      • receives votes from a majority of the servers in the full cluster for the same term.
    • another server establishes itself as leader
      • When waiting for vote, some may have already become leader (or previous leader came back to live)
      • restriction: If the leader’s term (included in its RPC) is at least as large as the candidate’s current term
    • a period of time goes by with no winner.
      • timeout and start new RPC
      • uses randomized election timeouts

Vote Process

  • Guarantees:

    • all the committed entries from previous terms are present on each new leader from the moment of its election
      • log entries only flow in one direction, from leaders to followers, and leaders never overwrite existing entries in their logs.
  • A candidate must contact a majority of the cluster in order to be elected

  • Each server will vote for at most one candidate in a given term, on a first-come-first-served basis

  • up to date: compare the index and term of the last entries in the logs.

    • diff terms: log with later term is more up to date
    • same term, longer log is up-to-date