In this lesson · topic 1 of 7
Topic 1
Agreeing when nodes fail
5 min read
One log, many copies
A single server keeps a log of writes and applies them in order. To survive the server dying, you copy the log to several servers. Now the copies must agree on the contents and the order, or they stop being copies. That agreement, reached by machines that can crash and lose contact with each other, is consensus.
The easy design is a primary that takes writes and tells the others. The hard part is what happens when the primary stops answering. The previous lesson showed that nobody can tell whether it died. If the others pick a new primary and the old one is only slow, two primaries take writes at once and the copies diverge.
What has to be true
A consensus protocol promises a short list of things, whatever the network does:
- At most one leader per term, so two nodes never take writes for the same slot in time.
- A committed entry is never lost or changed. Once the system has told a client its write is durable, every future leader has it.
- All copies apply the same entries in the same order.
What it does not promise is that it will always make progress. Fischer, Lynch and Paterson showed in 1985 that no deterministic protocol can guarantee termination in an asynchronous system where even one node may crash. Real protocols accept that. They keep the promises above at all times and make progress when the network behaves, which is most of the time.
Raft
This lesson uses Raft, from Ongaro and Ousterhout, because it was designed to be understood. It splits the problem into leader election, log replication and a handful of safety rules, and the lab implements all three at the message level. Elections are driven by a timeout on missing heartbeats, the same kind of detector as in the previous lesson but with a randomised wait, so a crash costs roughly one election timeout of leaderless time.
Predict first
What the numbers look like
In the lab’s default five-node cluster, averaged over eight seeds, the first leader is elected 416 msmeasured after start. After the leader crashes, a new one takes over in 635 msmeasured. In 14%measured of terms with a candidate, nobody wins and the cluster tries again. The next topic walks through why those numbers come out as they do.