Consensus Algorithms: Paxos and Raft

How a group of servers agrees on one value, or one ordered log, even when some of them crash: the problem, how Paxos and Raft solve it, and what it costs.

Overview

The problem

Consensus means getting several servers to agree on one value and never change their minds, even when some servers crash or messages are slow. That sounds small, but many hard problems reduce to it. Picking a single leader is agreeing on who leads. Committing a distributed transaction is agreeing on commit or abort. Keeping replicas in step is agreeing on the order of every write.

A correct consensus algorithm gives three promises. Agreement: no two servers decide different values. Validity: the decided value was actually proposed by someone. Termination: every server that does not crash eventually decides. The first two are about safety (nothing bad happens); the third is about liveness (something good eventually happens).

Four everyday problems are consensus in disguise, and each one names a different thing to agree on.

  • Leader election: the servers agree on which server is the leader for this term.
  • Atomic commit: every participant reaches the same answer, commit or abort.
  • Replicated log (total order broadcast): the servers agree on which entry goes in each log slot, in the same order everywhere.
  • Lock or lease service: the servers agree on which client holds the lock right now.

In practice, systems do not run consensus once. They run it for every slot of a replicated log. Each server applies the log, in order, to its own copy of a state machine (a key-value map, for example). Same starting state plus same log in the same order gives the same result on every server.

The rest of this chapter is about how the servers fill each slot of that log safely, starting with the one trick every algorithm shares.

Key concepts

  • Consensus
  • Quorum
  • Replicated log
  • Term (Raft)
  • Election timeout
  • Committed entry
  • Proposal number (Paxos)
  • FLP result

Interview questions

  • Walk through what happens in Raft when the leader crashes.
  • Why must a consensus cluster have a majority to make progress? What happens in a network split?
  • How is Paxos different from Raft?
  • What does the FLP impossibility result mean in practice?
  • Why is total order broadcast the same problem as consensus?

Sign up free to read the full topic

More DDIA topics

Start free on Cariara · Coding problems · System design studio