Topic 132 / Advanced
Consensus: Raft and Paxos
Chapter 8.3 · Consensus and Coordination
What and why
Quorums (→ 130) let individual reads and writes tolerate disagreement, but some decisions can't be left eventually consistent at all — which node is the leader, or what the next entry in a replicated log is — because acting on two different answers is a correctness failure, not staleness. Consensus is the problem of getting a set of nodes to agree on a single value, or a single totally ordered log, despite node failures and an unreliable network. Raft is a consensus algorithm built around a single elected leader that replicates a log to followers and reports an entry committed only once a majority have stored it. Paxos is an earlier consensus algorithm reaching the same guarantee through a different majority-voting protocol; it is widely considered harder to reason about, which is why Raft was designed explicitly for understandability. Builds on: → 130 (quorums — Raft's majority-commit rule is quorum logic applied to a log).
Diagram
election timeout, no leader heartbeat
Follower ───────────────────────────────────────► Candidate
▲ │
│ sees a leader at ≥ current term │ receives votes from a majority
│ ▼
└───────────────────────────────────────────── Leader
discovers a higher term in a message
How it works
- Time is divided into terms, each with at most one leader; every message carries its term number, and a node always defers to the higher term it has seen.
- A follower hearing nothing from a leader within its election timeout becomes a candidate, increments the term, and requests votes.
- A candidate receiving votes from a majority of nodes becomes leader for that term; each node votes for at most one candidate per term, which is what prevents two leaders winning the same term.
- The leader accepts writes, appends them to its log, and replicates each entry to followers; an entry is committed once a majority have stored it — at that point it is safe to apply and safe to never be lost, even if the leader then crashes.
- A new leader must have the previous leader's committed entries in its own log before it can win an election, which is what lets committed entries survive leader changes.
- At awareness level: with purely asynchronous messaging and no bound on delay, no deterministic algorithm can guarantee consensus if even one node may fail (the FLP impossibility result). Practical systems don't violate this; they use timeouts and randomized election delays to make progress likely, accepting that safety (never two different committed values) and liveness (eventually agreeing on something) aren't both provably guaranteed on every single run under real asynchrony.
Practical example
(illustrative). A 5-node Raft cluster needs votes or acknowledgments from a majority of 3 to elect a leader or commit an entry; it keeps making progress with up to 2 nodes down or partitioned away, because the 3 remaining nodes still form a majority — the same arithmetic as a quorum (→ 130) with N=5, majority=3.
When to use it
- Use when: a small set of decisions must have exactly one cluster-wide answer — who's leader, what's committed next in a replicated log — typically as the engine inside a coordination service (→ 136) rather than hand-rolled per application.
- Avoid when: implementing Raft or Paxos from scratch for ordinary application data — the election-safety and log-matching rules above are easy to get subtly wrong in ways that only show up under rare failure timing.
- Failure scenarios: a minority partition that still believes it can elect a leader and accept writes — it can't, because it can never reach a majority, which is exactly the safety property; a cluster sized so that losing one more node than planned removes the possibility of a majority.
- Simpler alternative: a single leader with manual, human-driven failover, when brief unavailability during that failover is acceptable and automated consensus's operational complexity isn't justified.
Practice
(Mechanism kind — trace by hand; 25–30 min, no lab services). Draw a 5-node cluster, term 1, node A as leader. A crashes; node C's election timeout fires first, C becomes a candidate at term 2 and gets votes from itself, D, and E (3 of 5 — a majority). Write out which node is leader at each term, then decide: an entry A had replicated only to itself and B (not a majority) before crashing — is it committed? Done when you can state, with the majority arithmetic shown, that it is not (it never reached a majority), and that it does not survive the leader change.
Key takeaway
Consensus gets a cluster to agree on one leader or one log despite failures by requiring every decision to clear a majority; Raft does this with an elected leader and a replicated log, Paxos with a different but equivalent majority-voting protocol. Done when you can: trace a Raft election and a commit round by hand and state the majority size for a given cluster size. Used next in → 133, 136.
References
Ongaro and Ousterhout, "In Search of an Understandable Consensus
Algorithm (Extended Version)" — see references.md.