In this lesson · topic 2 of 7
Topic 2
Elections, terms and the log
8 min read
Roles and terms
Every node is a follower, a candidate or a leader. Time is divided into terms, numbered upward. Each term begins with an election and has at most one leader. Every message carries the sender’s term, and a node that sees a higher term than its own adopts it and drops to follower. That one rule is how an old leader learns it has been replaced, and how stale messages are ignored.
An election
A follower that hears nothing from a leader for an election timeout runs for office. Step through one election below.
One election, three nodes
Step 1 / 6- term
- 1
- timers
- n0 410 ms · n1 320 ms · n2 560 ms
Three followers, no leader
Every node starts as a follower in term 1 with a random election timer somewhere between one and two timeouts. Nobody is sending heartbeats, so the timers run down.
The shortest timer fires
n1 hears nothing before its timer runs out. It increments its term to 2, becomes a candidate, votes for itself and restarts its timer.
Ask for votes
n1 sends RequestVote to the others with its term and the index and term of its last log entry, so each voter can check that its log is at least as complete as their own.
Each voter checks and grants
n0 and n2 see a higher term, adopt it, and have not voted in term 2. The candidate’s log is as complete as theirs. Each grants its vote, remembers who it voted for, and resets its own timer so it does not run too.
A majority: n1 is leader
With its own vote plus one more, n1 holds 2 of 3. That is a majority, so it becomes the leader of term 2. One vote per node per term is what makes a second leader in term 2 impossible.
Heartbeats hold the position
The leader at once sends empty AppendEntries messages and keeps sending them every heartbeat interval. Each one resets the followers’ timers. As long as they keep arriving, nobody else runs.
- Three followers, no leader. Every node starts as a follower in term 1 with a random election timer somewhere between one and two timeouts. Nobody is sending heartbeats, so the timers run down. term: 1; timers: n0 410 ms · n1 320 ms · n2 560 ms.
- The shortest timer fires. n1 hears nothing before its timer runs out. It increments its term to 2, becomes a candidate, votes for itself and restarts its timer. n1 role: candidate; n1 votes: 1 of 3 (itself).
- Ask for votes. n1 sends RequestVote to the others with its term and the index and term of its last log entry, so each voter can check that its log is at least as complete as their own. message: term 2, last index 0, last term 0; in flight: 2.
- Each voter checks and grants. n0 and n2 see a higher term, adopt it, and have not voted in term 2. The candidate’s log is as complete as theirs. Each grants its vote, remembers who it voted for, and resets its own timer so it does not run too. n0 voted for: n1; n2 voted for: n1; rule: one vote per node per term.
- A majority: n1 is leader. With its own vote plus one more, n1 holds 2 of 3. That is a majority, so it becomes the leader of term 2. One vote per node per term is what makes a second leader in term 2 impossible. votes: 2 of 3; leader: n1, term 2.
- Heartbeats hold the position. The leader at once sends empty AppendEntries messages and keeps sending them every heartbeat interval. Each one resets the followers’ timers. As long as they keep arriving, nobody else runs. heartbeat: every 100 ms; election timers: reset on every heartbeat.
Two details decide whether elections work. The timeout is random, drawn fresh from a range each time it is reset, so that one node usually times out first and wins before the others wake. And each node grants one vote per term, which is what makes two leaders in one term impossible: two majorities would need a shared voter who voted twice.
When two candidates do run for the same term and split the vote, neither reaches a majority. Both time out again, with new random waits, and one of them starts a higher term. The lab counts these split votes.
A replicated write
Once there is a leader, every client write goes through it. Step through one.
One replicated write
Step 1 / 6- leader
- n1, term 2
- logs
- all empty
A client sends a write
The client sends x=5 to the leader. Only the leader takes writes. A follower that receives one points the client at the leader instead.
The leader appends, uncommitted
n1 appends the entry to its own log at index 1 with its current term, 2. The entry is not committed and the client gets no answer yet.
Replicate to the followers
n1 sends AppendEntries to n0 and n2 with the new entry, plus the index and term of the entry before it (0 and 0) and its commit index (0). The previous index and term are the consistency check.
One follower acknowledges
n0 checks that its log matches at the previous index, appends the entry, and replies success with match index 1. The message to n2 is slow and still in flight.
A majority has it: commit
n1 counts itself and n0, which is 2 of 3. The entry is from the current term, so the commit rule applies. n1 sets its commit index to 1, applies the write and answers the client. From now on this entry cannot be lost.
Followers learn the commit
The next heartbeat carries leaderCommit 1, so n0 commits and applies the entry too. n2’s slow message finally arrives; it appends and replies, and learns the commit from a later heartbeat. Followers never decide commits themselves.
- A client sends a write. The client sends x=5 to the leader. Only the leader takes writes. A follower that receives one points the client at the leader instead. leader: n1, term 2; logs: all empty.
- The leader appends, uncommitted. n1 appends the entry to its own log at index 1 with its current term, 2. The entry is not committed and the client gets no answer yet. n1 log: index 1, term 2; commit index: 0.
- Replicate to the followers. n1 sends AppendEntries to n0 and n2 with the new entry, plus the index and term of the entry before it (0 and 0) and its commit index (0). The previous index and term are the consistency check. message: prev 0/0, entries [1], leaderCommit 0; in flight: 2.
- One follower acknowledges. n0 checks that its log matches at the previous index, appends the entry, and replies success with match index 1. The message to n2 is slow and still in flight. n0 log: index 1, term 2; n1 knows: n0 has index 1.
- A majority has it: commit. n1 counts itself and n0, which is 2 of 3. The entry is from the current term, so the commit rule applies. n1 sets its commit index to 1, applies the write and answers the client. From now on this entry cannot be lost. replicas: 2 of 3; n1 commit index: 1; client: write acknowledged.
- Followers learn the commit. The next heartbeat carries leaderCommit 1, so n0 commits and applies the entry too. n2’s slow message finally arrives; it appends and replies, and learns the commit from a later heartbeat. Followers never decide commits themselves. n0 commit index: 1; n2: has the entry, commit still 0.
The leader appends the entry to its own log, sends it to every follower, and commits it once a majority has it. Committed means durable: the leader applies it, answers the client, and tells followers the new commit index in its next messages. Followers never decide commits. They apply what the leader says is committed.
The three safety rules
Log matching. Every AppendEntries message carries the index and term of the entry just before the new ones. A follower accepts the message only if its own log has that entry. If it does not, the leader steps back one index and tries again until they agree, then overwrites everything after that point. The result is that if two logs share an entry at an index and term, they are identical up to there.
Election restriction. A voter refuses any candidate whose log is less complete than its own, judged by the last entry’s term and then its length. Since a committed entry is on a majority and a candidate needs a majority, at least one voter has it and will refuse a candidate without it. So a new leader always has every committed entry.
Commit rule. A leader counts replicas only for entries from its own term. An entry from an earlier term can be on a majority and still be overwritten by a later leader, so counting it would be unsafe. Earlier entries get committed indirectly, when an entry from the current term after them commits.
What the lab adds
The lab runs this protocol at the message level with seeded delays, so every election, split vote and repair you see is the real algorithm reacting to the network, not an animation of it. Tests in the repository check the three rules above after every tick across random crashes, restarts, partitions and heals. If the simulator ever broke one, the tests would fail.