Skip to content

Consensus and leader election

Replication has needed an answer to the same question in every lesson: which node is in charge now? Failover promotes a replica, and split brain happens when two nodes each decide for themselves. Consensus is the problem of getting a group of nodes to agree on one value, here the identity of the leader, even though nodes crash and messages are delayed or lost.

The Raft paper states what its algorithm promises. It is safe, meaning it never returns an incorrect result, under all non-Byzantine conditions, including network delays, partitions, packet loss, duplication and reordering. It is fully functional as long as any majority of the servers are operational and can communicate with each other and with clients. Servers are assumed to fail by stopping. Those are precise limits: no node lies, and a minority that is cut off or crashed does not stop the majority.

The majority is what makes agreement possible during a partition. Two disjoint groups cannot both contain more than half of the nodes, so at most one side of any split can decide. That is the same arithmetic as the write quorum of the last lesson, applied to the choice of a leader.

Few applications write this themselves. The etcd documentation lists Election RPCs among the coordination primitives it provides, so that an application can use an election instead of building one. The lesson on coordination services returns to this.

Mental model. A council elects a chair. The chair sends a note every so often to say it is still there. If a member hears nothing for long enough, it stands for election and asks the others to vote. A member votes once per term, and a candidate that collects votes from more than half the council becomes chair.

Raft divides time into terms, numbered with consecutive integers. Each term begins with an election, and if a candidate wins, it serves as leader for the rest of the term. Terms act as a logical clock: servers exchange them, a server that finds its own term out of date updates it, and a leader or candidate that finds its term out of date reverts to follower. A server that receives a request with a stale term number rejects it.

The trigger is silence. A leader sends periodic heartbeats. A follower that receives nothing over a period called the election timeout assumes there is no viable leader, increases its term, becomes a candidate and asks for votes. The lab runs this with three nodes, N1, N2 and N3, and one storage. Any message takes 10 ms. At 1,000 ms the leader, N1, pauses, for example in a long garbage collection, and sends no heartbeats. Leave Election timeout at 300 ms and Pause of the leader at 1,500 ms and read the log.

A leader pauses, a follower is elected, and the old leader wakes up and writes. Change the timeout and the pause, and switch the fencing check on and off.

300 ms
1,500 ms

A fencing token is the term number sent with every write. Storage refuses a write whose token is older than one it has already seen.

2,520 ms
  • N1FollowerTerm 2
  • N2LeaderTerm 2
  • N3FollowerTerm 2
  • SStorageHolds A, highest token 2

N1 learns that term 2 exists and steps down to follower.

Results of the run

Time without a working leader
320 ms
Term at the end
2
Time two nodes believed they led
1,200 ms
Late write from the old leader
Accepted
Value in storage at the end
A
State of each node at the event shown
NodeStateHolds
N1FollowerTerm 2
N2LeaderTerm 2
N3FollowerTerm 2
SStorageHolds A, highest token 2
Every event in words
Every event of the run, in time order
At secondEvent
0 msN1 is the leader of term 1. A follower starts an election after 300 ms without a heartbeat. Storage takes writes from whichever node sends them.
1,000 msN1 checks that it is still the leader, prepares a write to storage, and then pauses for 1,500 ms, for example in a long garbage collection. It sends no heartbeats.
1,300 msN2 hears no heartbeat for 300 ms. It becomes a candidate for term 2 and asks for votes.
1,320 msN3 votes for N2. N2 is the leader of term 2.
1,330 msN2 writes B to storage with fencing token 2.
2,500 msN1 wakes up. It still believes it is the leader, does not check again, and sends the write it prepared, with token 1.
2,510 msStorage accepts the write with token 1, because it does not check tokens. It overwrites B with A.
2,520 msN1 learns that term 2 exists and steps down to follower.

Illustrative values, not benchmarks of any real product.

Model notes

Assumptions

  • Three nodes and one storage. N1 leads term 1. Any message takes 10 ms, and a follower starts an election when no heartbeat arrives within the timeout.
  • N1 checks that it is the leader, prepares a write, and then pauses for the time you choose. It does not check again when it wakes up.
  • N2 times out first and wins with the vote of N3. The other timeouts are longer, so the vote is never split.

Simplifications

  • Real elections can split the vote and need another round. Here only the mechanism of the late write is shown.
  • The fencing token is the term number. A real system gets tokens from a lock service or from a log.

Parameter values are illustrative. They are not benchmarks of any real product.

N2 hears nothing for 300 ms, so at 1,300 ms it becomes a candidate for term 2 and asks for votes. N3 votes for N2, and 20 ms later, at 1,320 ms, N2 is the leader of term 2. The lab reports Time without a working leader as 320 ms: the timeout of 300 ms plus two message hops of 10 ms each, one to ask for the vote and one for the reply. The Raft paper says the same thing in general terms: when the leader crashes, the system is unavailable for roughly the election timeout.

The timeout is a choice, and the lab lets you move it. Set Election timeout to 150 ms and the time without a leader falls to 170 ms. Set it to 600 ms and it grows to 620 ms. A shorter timeout shortens the outage.

It also makes mistakes more likely. Keep Pause of the leader at 200 ms, a pause that ends before the 300 ms timeout. Nothing happens beyond the pause: no election, term 1 to the end and 200 ms without a working leader. Now set Election timeout to 150 ms with the same 200 ms pause. N2 times out before the leader returns, term 2 begins, and the leader that was only paused is replaced. The cluster paid for an election that a more patient timeout would have avoided.

The Raft paper frames the same trade. The election timeout should be an order of magnitude longer than the broadcast time, so that leaders can reliably send the heartbeats that keep followers from starting elections, and a few orders of magnitude shorter than the mean time between failures of a server, so that the system makes steady progress. The paper’s example interval is 150 to 300 ms, and it expects typical election timeouts to lie between 10 ms and 500 ms. None of this makes the choice universal: it depends on how long your network and your processes take to answer.

If two followers time out together, each can collect one vote and neither a majority. Raft ends such a term with no leader and begins a new one. To keep this rare, election timeouts are chosen at random from a fixed interval, so that usually one follower times out first and wins before the others start. The lab always lets N2 time out first and win with the vote of N3, so it never shows a split vote. That is a simplification of the mechanism, not a claim that splits do not happen.

  • Consensus gets nodes to agree on one value, here the leader, and a majority is the test: two disjoint groups cannot both have one.
  • A Raft-style election is triggered by silence. A follower that hears no heartbeat within the election timeout becomes a candidate for the next term and asks for votes.
  • In the lab, the leader pauses at 1,000 ms, N2 is elected at 1,320 ms, and the time without a leader is 320 ms: the 300 ms timeout plus two 10 ms hops.
  • A 150 ms timeout gives 170 ms without a leader, and 600 ms gives 620 ms. A short timeout also replaces a leader that was only paused for 200 ms.
  • Randomised timeouts make split votes rare, and a split vote costs another term. The lab does not model it.

Next: Leases and fencing tokens.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.