- W-001
- 1 min read
- distributed systems
Notes on Raft, from scratch
These are working notes from implementing Raft with only the paper open. They’re mostly the places where I got it wrong first.
Terms are the clock
Every message carries the sender’s term. Any node that sees a higher term steps down to follower and adopts it. Almost every bug I hit came from handling a message before checking its term.
Election safety needs the log check
A candidate wins only with votes from a majority, and a node grants its vote only if the candidate’s log is at least as up to date as its own. Up to date means a higher last term, or the same last term and a log at least as long. Without that check, a node with a stale log can win and overwrite committed entries.
Commit only from your own term
A leader can mark an entry committed only once an entry from its current term is stored on a majority. Counting replicas of entries from older terms isn’t enough. Figure 8 in the paper shows why, and it was the last bug I fixed.