← All articles

Understanding Raft · Part 1

Understanding Raft, Part 1: Why Consensus Is Hard

Getting a few unreliable machines to agree on one sequence of events. Part 1 of the Raft series behind Crystal, my key-value store in Go.

5 min read

One machine is easy

You build a service and it stores some state. One machine is fine until the disk dies or the process crashes, so you run three machines instead of one. And now you have a much harder problem. Three machines with three copies of the data have to agree on what the data is, every time, even when messages get lost or arrive late and even when one machine disappears in the middle of a write and comes back an hour later sure that it was right.

That problem is called consensus. etcd, ZooKeeper, CockroachDB and the control plane of Kubernetes all sit on top of it. It is also the problem I wanted to understand properly, so I built Crystal, a key-value store in Go that implements the Raft consensus algorithm from the paper up.

This series explains Raft the way I would have liked to learn it. Every mechanism comes from a concrete failure it has to survive. You need to be able to program. You don’t need a PhD.

Replicated state machines

Every consensus system starts from the same picture. A state machine takes commands and applies them in order. A key-value store is one: SET x=3, then SET y=1, then DELETE x. If two copies start in the same state and apply the same commands in the same order, they end in the same state.

So keeping replicas consistent comes down to one rule. Every server applies the same commands in the same order.

Replicated state machine: clients send commands to a consensus module, which replicates an identical log to every server; each server’s state machine executes that log in order to reach the same state.

The consensus module keeps the log identical on every server. The key-value store above it only has to apply that log. (Compare Ongaro and Ousterhout 2014, Figure 1.)

This changes the question. The servers don’t agree on the value of x. They agree on the log, the ordered list of commands, and the values follow from it.

Where it gets hard

If machines never failed and networks never dropped anything, you would broadcast each command and be done. All the difficulty sits in the failures.

A server can take part in a decision, crash before it writes that decision to disk, and come back with no memory of it. Messages get lost or arrive out of order, and from the outside a slow machine looks exactly like a dead one. Worst of all, the network can split. Two servers can talk to each other but not to the other three, and if both groups keep accepting writes, the data splits with them and never comes back together.

A correct algorithm has to keep working whenever a majority of the servers can talk to each other, and it can never make two conflicting decisions, not even for a second during a partition. One divergence is silent data corruption, so “correct most of the time” is worth nothing here.

Majorities

The idea that makes this possible is the majority quorum. In a cluster of five servers, any group of three shares at least one server with any other group of three. If every decision needs a majority, two conflicting decisions would need two majorities, and the server they share would have refused to vote for both. The math rules the conflict out.

This also explains why clusters are odd-sized. Five servers survive two failures, because three are still a majority. Three servers survive one. Four servers also survive only one, because a majority of four is three, so going from three to four buys you nothing.

Paxos, and why Raft exists

For a long time the standard answer to consensus was Paxos, by Leslie Lamport. It is correct and it is famously hard to follow. The paper describes how to agree on a single value, and turning that into a system that agrees on a whole sequence of values (Multi-Paxos) is mostly left to the reader. Every team that builds it ends up with a slightly different version.

Diego Ongaro and John Ousterhout made understandability a design goal in their 2014 paper, In Search of an Understandable Consensus Algorithm. Raft is what came out of it. It splits consensus into three parts that you can study one at a time.

  1. Leader election. How the cluster picks exactly one server to be in charge, and what happens when that server dies. (Part 2.)
  2. Log replication. How the leader takes commands from clients and copies its log to the other servers. (Part 3.)
  3. Safety. The rules that make sure leaders and logs can never diverge. (Parts 3 and 4.)

Raft also uses strong leadership. One server is the leader, clients only talk to the leader, and the log only flows from the leader to the followers. That is stricter than Paxos on purpose. A whole family of “what if two servers both think they are in charge” cases goes away, because who leads is itself something the cluster agrees on.

The rest of the series

Each part ties the theory to a decision I made in Crystal.

  • Part 2, Leader election. Terms as a logical clock, the RequestVote RPC, and randomized timeouts, the small trick that keeps elections from going in circles.
  • Part 3, Log replication and safety. AppendEntries, the Log Matching Property, what “committed” means, and the commit rule almost every first implementation gets wrong.
  • Part 4, Building Crystal. How the paper became Go code. A crash-safe write-ahead log, snapshots, and the single-writer loop that keeps the concurrency simple.

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.