Election Restriction: Up-to-Date Check
What You'll Learn
You'll learn how Raft prevents candidates with incomplete or outdated logs from becoming leaders, ensuring elected leaders always have all committed entries.
The Problem
In a distributed system running Raft, imagine a server was offline when several log entries were committed. If it comes back and immediately wins an election, it could become leader with an incomplete log—potentially losing committed data. This would violate Raft's Leader Completeness Property (which guarantees that if an entry is committed, all future leaders must have it).
The Up-to-Date Check
Raft solves this with a strict election restriction. When a candidate sends a RequestVote RPC, it includes:
- The term of its last log entry
- The index (length) of its log
Voters compare this information with their own logs before granting a vote. A voter will reject the candidate if the voter's log is "more up-to-date":
Comparison Rules:
- Higher last term wins: If the logs end with different terms, the one with the higher term is more up-to-date
- Longer log wins (if terms equal): If last terms are equal, the longer log is more up-to-date
Analogy
Think of it like a library: before electing a new head librarian, you check their catalog. Someone with the latest edition (higher term) or more books from the same edition (longer log at same term) is better qualified.
Why This Works
Since committed entries require majority replication, at least one server in any majority must have all committed entries. By requiring majority votes AND this up-to-date check, only candidates with complete logs can win.
Key Takeaway: Raft's election restriction ensures only candidates with sufficiently up-to-date logs can become leaders, preventing committed data loss by comparing log term and length during voting.