21 Raft 2 Leader Re-Election

Leader Election

  • Leader sends AppendEntries when client submit operation
  • Leader also sends AppendEntries as a heartbeat "I'm still alive"
  • If leader unresponsive, after Election Timeout, server start election

Election Timeout: Must be randomized such that, most of the time, exactly 1 server starts an election at a time,

Election Process

During election, server will

  1. become CANDIDATE
  2. increase term number
  3. solicit votes
  4. if get majority vote, become leader, otherwise, wait & repeat process later

We must guarantee

  1. At Most One leader for a term
  2. The elected leader must have All Committed log entries

Each voter guarantees

  1. Only vote once: use votedForto track who it voted for incurrentTerm
    • So that no 2 leaders can both have the majority of vote
  2. Only vote candidate with all log it has (or more):
    • ensure elected leaders have all log entries present on majority of servers, thus all newly elected leaders have all committed entries!

Server will vote the first time it sees aRequestVotemsg for a new term, if it comes from an up-to-date server.

Pasted image 20251026011918.png

Up-to-date Server

Server will cast vote based on requester's log length and last term

  1. When requester has bigger term
  2. When they have same term but requester has longer log length

It is impossible for a server without all committed logs to be elected because the majority must have had all committed entries, so they will only vote for a leader with all committed entries.