Try
↑ ↓ move↵ openesc close
In this lesson · topic 2 of 7
  1. 1. Agreeing when nodes fail
  2. 2. Elections, terms and the log
  3. 3. Crash the leader and cut the cluster
  4. 4. The leader nobody follows
  5. 5. Walking a follower's log back to the leader's
  6. 6. Adding two nodes without ever having two majorities
  7. 7. Check votes, logs and quorums

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
n0term 1n1term 1n2term 1orange: leader · dashed: candidate · grey: followerarrows are messages, dim ones are earlier
term
1
timers
n0 410 ms · n1 320 ms · n2 560 ms
  1. 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.

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
n0n1leader t2n2log emptylog emptylog emptyclientsolid cell: committed · dashed: in the log, not committedcommitted once a majority has it and the leader says so
leader
n1, term 2
logs
all empty
  1. 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 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.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms