In Search of an Understandable Consensus Algorithm
2014 · USENIX ATC · ~12 min read
Read the original paperA consensus algorithm that keeps a replicated log identical across crashes, designed to be understood. Read it before you operate etcd, Consul or any database that replicates with a leader.
Raft is an algorithm for keeping a replicated log identical on a group of machines while some of them crash and restart. The paper’s contribution is not a new capability; Paxos already provided one. It is a design that engineers can understand well enough to implement correctly, and an argument for why that should count as a goal of the research.
Consider the job Raft is used for. A configuration store runs on three machines so that losing one does not take it down. A client writes leader=svc-7. The machine that received the write crashes a moment later. Is the write durable? Will the new leader of the group have it? Could another client read the old value after being told the write succeeded? A replicated store has to answer these questions exactly and the same way every time, and it has to do so with no reliable clocks and messages that can be delayed or lost.
Why Paxos was not enough
By 2013 the field had a proven answer in Paxos. Lamport’s algorithm guarantees that a group agrees on a single value as long as a majority are working. Diego Ongaro and John Ousterhout’s complaint, set out in Section 3 of the paper, was twofold. Paxos was hard to understand, even for researchers who taught it. And its basic form agrees on one value; a real system needs agreement on a sequence of values, and the literature’s description of that extension, Multi-Paxos, left many decisions unspecified. Every production system, the paper notes, began with Paxos and ended with something different and unproven, with Google’s Chubby team saying as much in their own experience paper.
So Raft starts from a different priority. Correctness and efficiency comparable to Multi-Paxos were required. Beyond that, whenever there was a design choice, the authors picked the option that was easier to explain, and they used two techniques throughout: divide the problem into parts that can be understood separately (leader election, log replication, safety), and reduce the number of states the system can be in. They then tested the claim with a user study of 43 students at Stanford and Berkeley who learned both algorithms; most did better on the Raft quiz.
One leader per term, and how it is elected
Raft puts all decisions in the hands of a single leader. Clients send commands to the leader; the leader appends them to its log and copies them to the others; log entries only flow from leader to followers. That is a much stronger leader than in basic Paxos, and it is the main simplification. The problem it creates is choosing the leader safely.
Time is divided into terms, numbered with consecutive integers. Each term begins with an election and has at most one leader. Every server stores the latest term it has seen, and every message carries the sender’s term. A server that sees a higher term updates its own and becomes a follower at once; a request carrying a stale term is rejected. Terms play the role Lamport’s logical clocks play: they let servers detect stale information without trusting time.
A leader sends periodic heartbeats. A follower that hears nothing for its election timeout increments its term, votes for itself, becomes a candidate and sends RequestVote to everyone. Each server votes for at most one candidate per term, first come first served. A candidate that collects votes from a majority becomes leader and starts sending heartbeats.
If two followers time out at the same moment, they can split the votes and neither wins. Both time out again and start a new term, and this could repeat. Raft’s answer is randomisation: each server picks its timeout at random from a range, the paper’s example being 150 to 300 milliseconds. In a five-node cluster one follower will usually time out tens of milliseconds before the others, win the election and send heartbeats before anyone else wakes up. The authors had first considered a ranking scheme for breaking ties and found it produced subtle availability problems; randomness was easier to reason about.
The paper states the timing requirement as an inequality. The time to send a message to all servers and get replies must be much less than the election timeout, which must be much less than the average time between failures of a single server. Broadcast time on a local network is typically well under the low tens of milliseconds, so a timeout of a few hundred milliseconds leaves room. Run Raft across continents and the left side of the inequality grows, which is why geographically spread deployments raise their timeouts.
Following one command into the log
Take a five-node cluster, S1 to S5, with S1 as leader in term 2. A client sends set x=1. S1 appends it to its log at index 4 with term 2 and sends AppendEntries to the four followers. Each AppendEntries also names the index and term of the entry just before the new ones, here index 3 and whatever term that entry had.
A follower accepts only if its own log has an entry at index 3 with that term. This consistency check, applied inductively, gives the log matching property: if two logs have an entry with the same index and term, the logs are identical up to that point. When a follower’s log differs, say S4 missed entry 3 while it was down, it rejects the request. The leader keeps a next-index for each follower, decrements it, and retries until it reaches a point where the logs agree. Then it sends everything from there, overwriting anything the follower had after that point. Repair is always the leader’s log winning.
With numbers: S4 holds entries 1 and 2. S1 sends index 4 with “previous is index 3, term 2”. S4 has no index 3, so it rejects. S1 lowers S4’s next-index to 3 and sends entries 3 and 4 with “previous is index 2, term 1”. That matches, so S4 appends both. The paper notes this one-step-at-a-time walk back can be optimised, for instance by having the follower report the term of the conflicting entry so the leader skips a whole term at once, and argues that in practice failures are rare enough that the simple version is fine.
One detail in Figure 2 is easy to miss and causes real bugs. Each server must write its current term, the candidate it voted for, and its log to stable storage before replying to any request. If a server votes for S2 in term 3, crashes, restarts with no memory of the vote, and then votes for S5 in term 3, two leaders can be elected in the same term. Raft’s safety argument assumes votes survive restarts.
Once S1 learns that index 4 is stored on three of five servers, itself and two followers, the entry is committed. S1 applies it to its state machine, answers the client, and tells followers the new commit index in later messages so they apply it too. The simulation below lets you run elections, append entries, crash the leader and see which entries survive.
Fire a timeout to elect a leader, send writes and watch them commit when a majority holds them, then crash the leader and see who may replace it.
Everything Raft promises follows from one rule: an entry counts as committed only when a majority holds it, and only a node whose log is at least as complete as a majority can become leader. Two majorities always overlap, so a committed entry can never be lost by an election.
Why a committed entry survives any crash
Suppose S1 crashes right after answering the client. The client was told set x=1 succeeded, so the next leader must have it. Nothing so far guarantees that. Raft adds one rule to elections: a server refuses to vote for a candidate whose log is less up to date than its own. “Up to date” compares the term of the last entry first, and the length of the log only if those terms are equal. RequestVote carries the candidate’s last index and term so voters can apply the check.
In the five-node cluster, index 4 is on S1, S2 and S3 and is committed. S1 crashes. S5 missed the entry and times out first. Can S5 become leader?
Show the worked answer
Not without the entry. S5 needs three votes including its own, so it needs two of S2, S3 and S4. S2 and S3 both have index 4 from term 2, so their logs are more up to date than S5’s, and they refuse. S4 alone is not enough. S5’s election fails, and a later candidate from S2 or S3 wins. Any majority that can elect a leader overlaps the majority that stored the entry, and the overlapping server enforces the rule.
The case that catches implementers: entries from an older term
The rule above is the obvious half. The paper’s Figure 8 shows a less obvious half, and it is worth walking through slowly because implementations get it wrong. Five servers again.
In term 2, S1 is leader and replicates an entry at index 2 to S2 only, then crashes. S5 is elected leader for term 3 with votes from S3, S4 and itself (their logs end at index 1, as does S5’s, so S5 is up to date enough). S5 accepts a different client command at index 2 with term 3, then crashes before replicating it. S1 restarts, wins term 4, and continues replicating its old term-2 entry. It reaches S3, so the term-2 entry now sits on S1, S2 and S3: a majority.
Is the term-2 entry committed? It is on a majority, so the naive rule says yes. Now let S1 crash again. S5 can win term 5 with votes from S2, S3 and S4, because the last term in its log is 3, which beats the 2 at the end of theirs. As leader, S5 overwrites index 2 on every follower with its own term-3 entry. An entry that was stored on a majority, and might have been reported committed, has vanished.
Raft’s fix is a restriction on the commit rule. A leader only counts replicas to commit entries from its own current term. Older entries become committed indirectly, when an entry from the current term after them is committed. In the walk-through, S1 in term 4 must get a term-4 entry onto a majority before anything at index 2 counts as committed. Once that happens, S5’s log is no longer up to date enough to win an election, and the overwrite cannot occur. The cost is a little extra latency in the rare case where a new leader inherits uncommitted entries; implementations commonly append an empty entry at the start of each term to settle them quickly.
What the paper leaves to you, and what came later
Much of what made Paxos hard to build lies outside the core algorithm, and Raft’s paper covers these parts too. Section 6 handles changing the set of servers with joint consensus: for a transition period, decisions need separate majorities of both the old and new configurations, so two leaders cannot be elected in different majorities. Section 7 covers snapshots for compacting the log, and Section 8 covers clients, including giving each command a serial number so a retried command is not applied twice, and requiring a leader to confirm it is still leader before serving a read.
| problem | what the paper does | what it costs | what later work changed |
|---|---|---|---|
| split votes | randomised election timeouts | a few hundred ms of unavailability after a leader dies | tuned timeouts per deployment |
| changing membership | joint consensus over old and new sets | two-phase transition, more states | Ongaro’s thesis adds one-server-at-a-time changes, with a bug found and fixed later |
| unbounded log | snapshot the state machine, discard the prefix | snapshot transfer to slow followers | incremental snapshots in production stores |
| stale reads from a deposed leader | leader confirms with a heartbeat round before reads | a network round trip per read | leases that trade a clock assumption for speed |
| a flaky server disrupting the cluster | not covered in the paper | repeated elections with ever higher terms | pre-vote and check-quorum, from Ongaro’s thesis |
That last row is where real deployments hurt. A server that can send but not receive, or that is cut off from the leader but not from the others, keeps timing out and starting elections with higher terms, forcing a healthy leader to step down. Pre-vote has a candidate first ask whether it could win before incrementing its term; check-quorum has a leader step down if it cannot hear from a majority. Cloudflare’s 2020 write-up of a partially failed network switch, which left an etcd cluster electing leaders over and over, is a good account of this class of failure.
Is Raft really different from Paxos? Heidi Howard and Richard Mortier’s 2020 paper compares them in a common framework and concludes the two are very close, with the main difference in how a leader is chosen: Raft only allows a server with an up-to-date log to win, while Paxos lets any server win and then has it catch up. Raft’s advantage is in presentation and completeness of specification more than in mechanism. That was the paper’s claim, so it is not a criticism.
When should you not reach for Raft? When the data can tolerate merging, because a majority requirement means a minority partition cannot write at all. When write latency must stay low across distant regions, because every commit waits for a majority round trip. And when a single Raft group would hold too much data or traffic; CockroachDB, TiKV and YugabyteDB run thousands of small Raft groups, one per range of keys, for that reason. Within those limits, Raft became the default. etcd uses it and holds the state of every Kubernetes cluster, Consul uses it, and Kafka replaced ZooKeeper with its own Raft-based controller quorum.
How to read the original
Read the extended version linked here, not the shorter conference paper. Sections 1 to 4 are motivation and take ten minutes. Then go to Figure 2, the one-page summary of the whole algorithm, and keep it open while you read Section 5. Read 5.2 to 5.4 closely; Section 5.4.2 and Figure 8 are where the subtle case above comes from, and they are worth reading twice. Section 5.6 has the timing inequality. Read Section 6 on membership if you will operate a cluster, and skim 7 and 8 otherwise. In Section 9, the user study is interesting as methodology; the performance section shows how election time depends on the timeout range, which is the evidence for randomisation. Afterwards, try writing the state transitions from memory. If you can, the paper achieved its goal.
Raft orders writes within a group by having one leader number them. Spanner needs an order that holds across thousands of such groups spread over continents, consistent with real time, and it gets it by returning to the problem Lamport set aside: physical clocks.
The original. Read it after this page; the explainer above is a map, not a substitute.
An animated walkthrough of elections and log replication; the best ten minutes you can spend on this paper.
The author presenting the design and the reasoning behind each choice.
The paper, the visualisation, and a list of the many implementations in production today.