Raft Leader Election
Subtitle: How a cluster agrees on exactly one leader
THREE NODE STATES:
FOLLOWER - waits for heartbeats from the leader CANDIDATE - asks peers for votes LEADER - sends heartbeats, appends log
- ELECTION SEQUENCE:
- Heartbeat times out on a follower
- It becomes a Candidate, term + 1
- Votes for itself, sends RequestVote
- Peers grant one vote per term
- A majority of votes wins leadership
RULES THAT KEEP IT SAFE:
Randomized timeouts prevent tied votes One vote per node per term A majority is a quorum, not all nodes Higher term wins, old leader steps down
COMMON BEGINNER MISTAKE:
Assuming any node can just declare itself leader. Without a majority and a rising term, two nodes both believe they lead - and you get split brain.