Leader Election Restriction for Safety
What you'll learn: How Raft prevents incomplete or stale logs from winning elections, ensuring committed entries are never lost.
The Safety Problem
In Raft, once a log entry is committed (replicated to a majority), it must never be lost. But what if a candidate with an outdated log wins an election? It could overwrite committed entries on other servers, violating safety guarantees.
Imagine a committee where the newest member—who missed the last three meetings—suddenly becomes chairperson and declares all prior decisions void. That would be chaos! Raft prevents this scenario in leader election.
The Voting Restriction
During the RequestVote RPC, candidates include their last log entry's index and term. Followers use this information to decide whether to grant their vote.
A follower only votes for a candidate if the candidate's log is at least as up-to-date as its own log.
Log Comparison Rules
Raft compares logs using a two-step check:
- Compare the term of the last entry: Higher term wins (more recent)
- If terms are equal, compare log length: Longer log wins (more complete)
This ensures that any elected leader has all committed entries. Since commitment requires a majority, and the new leader must also win a majority vote, the two majorities must overlap—guaranteeing the new leader has seen all committed entries.
Why This Works
Because committed entries exist on a majority of servers, and a candidate needs votes from a majority (which must overlap), at least one voter will have those committed entries. If the candidate's log is incomplete, that voter will reject the vote request.
Key Takeaway:
Raft's voting restriction—requiring candidates to have logs at least as complete as a majority of servers—ensures that only candidates possessing all committed entries can become leader, preserving safety.