Raft lets a group of computers agree on one ordered log using a leader, majorities and random timeouts, and was designed to be easy to understand.
Many systems keep copies of the same data on several computers, so that one broken computer does not lose anything. The hard part is keeping every copy identical when computers crash and messages get lost. This problem is called consensus: a group of computers agreeing on one ordered list of commands. The widely used older solution, Paxos, was hard to understand and to build from. This paper designs a new algorithm, Raft, with understandability as its main goal.
In Search of an Understandable Consensus Algorithm. Diego Ongaro and John Ousterhout, Stanford University. USENIX Annual Technical Conference (ATC) 2014. Link: raft.github.io/raft.pdf (the extended version).
Elect one leader, let only the leader decide the order of commands, and count a command as final once more than half of the servers have stored it.
Raft splits the problem into three smaller parts: choosing a leader, copying the leader's list to the others, and rules that keep the list safe when leaders change.
Terms used below. A server is one computer in the group. A log is a server's ordered list of commands. A majority (also called a quorum) is more than half of the servers.
Waiting for all servers is no good, since one crashed server blocks everything. So Raft waits for a majority.
Any two majorities of the same group share at least one server. With 5 servers a majority is 3, and two groups of 3 must overlap in at least 3 + 3 - 5 = 1 server. So anything a majority has stored is known to at least one server in any later majority. Both election and commit use this overlap.
Servers A, B, C, D, E. A, B, C store an entry. Later, a group B, D, E tries to decide something. B is in both groups, so B can tell the new group about the entry. Try any other group of 3 and you will always find A, B or C in it.
Every server is in one of three states. A follower only answers other servers. A candidate is asking for votes. A leader accepts commands from clients and sends them to the followers. The leader sends regular small messages, called heartbeats, so followers know it is alive.
Time is cut into terms, numbered 1, 2, 3 and so on. A term starts with an election. There is at most one leader per term. Every message carries the sender's term. A server that sees a higher term than its own adopts it. A leader or candidate that sees a higher term steps down at once. A server rejects any message with an older term.
A follower that hears no heartbeat before its election timeout assumes the leader is gone. It adds 1 to its term, votes for itself and asks the others for votes. Each server gives one vote per term, to the first valid request it sees. A candidate that gets a majority becomes the leader and sends heartbeats.
If two servers become candidates together, the votes can split and nobody wins. Raft prevents repeats by giving each server a random election timeout, chosen from a range such as 150 to 300 ms. Usually one server times out first and wins before the others start.
The leader of term 4 crashes. The random timeouts are S1 = 240 ms, S2 = 170 ms, S3 = 290 ms, S4 = 210 ms and S5 = 260 ms. S2 has the shortest timeout. A majority is 3 of 5.
Term 4 leader crashes. All five are followers with different random timeouts
The paper tested this. With a fixed timeout, electing a leader took more than 10 seconds in its tests because of repeated split votes. With 5 ms of randomness the median downtime was 287 ms. With 50 ms of randomness the worst of 1000 trials took 513 ms.
Each log entry holds a command, the term when it was created, and an index (its position). A client sends a command to the leader. The leader appends it to its log and sends it to the followers with an AppendEntries message.
That message also names the index and term of the entry just before the new ones. A follower accepts only if its own log has that same entry there. Otherwise it refuses, and the leader tries again one position earlier, until it finds a point where the logs agree. Then the follower deletes its own entries after that point and copies the leader's.
This one check gives the Log Matching Property: if two logs have an entry with the same index and term, then the two logs are identical up to that entry.
Entries are written as index:term. The leader's log is 1:1, 2:1, 3:2. A follower's log is 1:1, 2:1, 3:1, where its entry 3 is a leftover from an old leader. The leader now appends 4:3.
The leader sends entry 4:3 and says: the entry before it is 3:2. The follower has 3:1, not 3:2
An entry is committed once the leader has stored it on a majority of servers. A committed entry will not be lost. The leader tells the followers which index is committed, and each applies the committed commands, in order, to its own copy of the data.
Suppose an entry is committed and then the leader crashes. If a server missing that entry became leader, it would overwrite the entry. Raft prevents this with the election restriction. A server refuses to vote for a candidate whose log is less up to date than its own.
"More up to date" is defined exactly. Compare the terms of the last entries. The log with the later last term wins. If the last terms are equal, the longer log wins.
A committed entry is on a majority. A winning candidate needs votes from a majority. These two majorities overlap, so at least one voter has the entry, and that voter refuses any candidate that lacks it.
A voter has the log 1:1, 2:1, 3:2, 4:3 (last term 3, length 4). Candidate X has 1:1, 2:1, 3:2, 4:2, 5:2. Its last term is 2, lower than 3, so the voter refuses even though X's log is longer. Candidate Y has 1:1, 2:1, 3:2, 4:3, 5:3. Its last term is 3, equal, and it is longer, so the voter votes for Y.
There is one more rule. A leader never commits an entry from an older term just by counting copies. It commits an entry from its own term. That also commits all earlier entries through the Log Matching Property. The paper's Figure 8 shows a case where counting old entries would lose a committed entry.
To change the set of servers, Raft first uses a joint consensus period. Decisions then need a majority of the old set and a majority of the new set. Switching directly could briefly let two separate majorities exist. To stop the log from growing forever, servers save snapshots of their data and drop the log entries behind them.
Raft was tested for understandability and for election speed. The paper also argues safety in a proof, and a TLA+ proof of one key property relies on invariants that were not mechanically checked.
| Test | Result |
|---|---|
| User study, 43 students at two universities, quizzes on Raft and Paxos | 33 scored higher on Raft. Mean score 25.7 on Raft and 20.8 on Paxos, out of 60 |
| Election with 12 to 24 ms timeouts | 35 ms on average to elect a leader, longest trial 152 ms |
The study shows Raft was easier to learn in that sample. The authors say the setup favored Paxos in two ways: 15 of 43 participants had prior Paxos experience, and the Paxos video was 14% longer. They also say self-reported opinions may be biased by knowing the hypothesis. Raft was designed to match multi-Paxos in function and in efficiency, and it is not claimed to be faster than Paxos.