← All articles

Understanding Raft · Part 2

Understanding Raft, Part 2: Leader Election

How a Raft cluster elects exactly one leader and recovers when it dies. Terms as a logical clock, RequestVote, and randomized timeouts.

5 min read

One leader at a time

In Part 1 consensus came down to one goal, an identical and ordered log on every server. Raft gives that log to a single leader. Clients send commands only to the leader, the leader appends them and copies them to the others, and the followers mostly wait.

So everything depends on one property. At any moment there is at most one leader that counts. If two servers act as leader at the same time and both accept writes, the logs split. The election has to rule that out through crashes, partitions and late messages.

Three states

Every server is in exactly one of three states.

Raft server states: a server starts as a Follower, becomes a Candidate on election timeout, becomes Leader on winning a majority, and any server steps down to Follower when it sees a higher term.

A follower that stops hearing from the leader becomes a candidate. A candidate with a majority becomes leader. Any server that sees a higher term goes back to follower. (Compare Ongaro and Ousterhout 2014, Figure 4.)

A follower answers the leader and candidates and does nothing on its own. If it hears nothing from a leader for long enough, it assumes the leader is gone. A candidate is a follower that decided to run for leader and asks everyone for votes. The leader won an election, serves clients, and sends regular heartbeats so the followers don’t start elections of their own.

Terms

Raft doesn’t trust wall clocks, because clocks on different machines drift. It numbers time instead. Time is split into terms, and each term starts with an election. If someone wins, they lead for the rest of the term. If the vote splits and nobody wins, the term ends without a leader and the next term starts with a new election.

Raft time divided into numbered terms: each begins with an election; some terms elect a leader and run normally, while a term with a split vote ends with no leader and advances to the next term.

The term is a counter that only goes up, a logical clock. Servers compare term numbers to decide whose information is newer. (Compare Ongaro and Ousterhout 2014, Figure 5.)

Every RPC carries the sender’s term, and two rules follow from that.

A server that sees a higher term than its own takes that term and becomes a follower.

A server that sees a lower term than its own rejects the message.

The first rule does a lot of quiet work. Say the leader gets cut off from the network, the others elect a new leader in a higher term, and the old leader reconnects later. The first message it exchanges with anyone carries the higher term, so it steps down by itself. Raft needs no special “you have been replaced” message. An old leader can’t do damage because its first contact with the rest of the cluster demotes it.

The election

A follower starts an election when its election timeout fires. It adds one to its term, becomes a candidate, votes for itself, and sends RequestVote to every other server.

Each server gives at most one vote per term, to whoever asks first. There is also a check on how up to date the candidate’s log is, which Part 3 covers. One vote per term is what keeps the election safe. A candidate needs a majority, every server votes once, so two candidates can’t both collect a majority in the same term. It is the same overlap argument from Part 1, applied to votes.

Then one of three things happens. The candidate gets a majority, becomes leader and starts sending heartbeats at once. Or a message arrives from another server that claims leadership for the same term or a higher one, and the candidate accepts it and goes back to follower. Or the votes split, nobody wins, and the timeout fires again for a new election in a new term.

Split votes

The third case is the dangerous one. Say several followers time out at almost the same moment. They all become candidates in the same term and all vote for themselves, so nobody reaches a majority. If they time out together again, they split again, and the cluster can keep holding elections forever without ever serving a client.

Raft’s fix costs almost nothing. Each server picks its election timeout at random from a range, for example 150 to 300 ms. Now the followers rarely fire together. One of them times out first and collects the votes before the others have even started. When a split does happen, everyone picks a new random timeout for the next round, so a second collision is even less likely.

That’s all of it. No priorities, no coordinator. The paper says the authors tried more elaborate ranking schemes first and went with randomness because it was the easiest to reason about. I think it’s the nicest idea in the whole algorithm, and Crystal uses it exactly as described.

Choosing the timeout range

Randomness only helps if the range fits the network. The paper gives a rule I find easy to remember.

broadcastTime  ≪  electionTimeout  ≪  MTBF

broadcastTime is how long an RPC round trip to the cluster takes, often well under a millisecond inside one datacenter. electionTimeout is the random range. MTBF is the mean time between failures of one server, usually months.

If the timeout is close to the broadcast time, followers start elections before the heartbeats can reach them and the cluster never settles. If it is close to the MTBF, the cluster spends a real share of its uptime without a leader after every crash. The safe window is several orders of magnitude wide, but an implementation that picks outside it behaves badly even though every line of it is correct.

What elections don’t solve

The cluster can now pick one leader and replace it when it dies. But an election only decides who is in charge. It says nothing about what is in the log. A new leader might be missing entries a majority already accepted, or hold entries that were never committed.

Making sure only the right server can win, and that the logs converge safely afterwards, is Part 3. That’s where most of Raft’s subtlety lives.


Reference: Diego Ongaro and John Ousterhout, “In Search of an Understandable Consensus Algorithm,” USENIX ATC 2014. The diagrams are mine. Where one follows a figure from the paper, the caption says so. The code is in Crystal.