PAXOS

Consensus in two phases

L320 MINDISTRIBUTED SYSTEMS
PREDICT

Paxos runs in two phases: prepare(n) asks acceptors to promise ballot n, then accept(n, v) asks them to accept value v at that ballot. Three acceptors; X is already chosen: A1 and A2 accepted (1, X). P2, which wants Y, sends prepare(2) to A2 and A3 and gets both promises. What does P2 send?

Paxos lets a set of acceptors agree on one value even when proposers race and messages are lost. A proposer picks a ballot n and sends prepare(n). Each acceptor promises to ignore lower ballots and reports any value it already accepted. With a majority of promises the proposer sends accept(n, v). A value is chosen once a majority accepts the same ballot.

The rule people drop: v is not the proposer’s choice. If any promise reports an accepted value, v must be the one with the highest ballot. That value may already be chosen. Any two majorities overlap, so the next proposer always hears from an acceptor that holds it, and the highest-ballot value it hears is that chosen value.

▶ SIMULATION
PROPOSER P1 · wants X
ballot — · idle
promises: —
sending: — · rejected 0×
ACCEPTORS (majority = 2)
A1: promised — · accepted —
A2: promised — · accepted —
A3: promised — · accepted —
chosen: nothing yet
PROPOSER P2 · wants Y
ballot — · idle
promises: —
sending: — · rejected 0×
prepare rounds0 · highest ballot —
last eventP1 wants X, P2 wants Y. Nothing promised, nothing accepted.
✓ once a value is chosen, no different value is ever chosen
SCHEDULE
01[P1]P1: prepare(1) → acceptors
02[A1]A1 ← prepare from P1
03[A2]A2 ← prepare from P1
04[A3]A3 ← prepare from P1
05[P1]P1: with a majority, accept(n, highest reported value or X)
06[A1]A1 ← accept from P1
07[A2]A2 ← accept from P1
08[A3]A3 ← accept from P1
09[SYS]report
Start of single proposer scenario. 9 steps.
Single-decree Paxos

PHASE 1  proposer picks a ballot n, unique and higher than any it has used
  -> prepare(n) to the acceptors
  acceptor: if n > promised:
              promised = n
              reply promise(n, accepted)   // accepted = (n', v') or none
            else reject

PHASE 2  with promises from a majority:
  v = value of the highest-numbered accepted (n', v') among the promises
      or, if none reported one, the proposer's own value
  -> accept(n, v)
  acceptor: if n >= promised:
              promised = n; accepted = (n, v); reply accepted
            else reject

CHOSEN   a majority accepted the same (n, v)

Safety   any two majorities share an acceptor, so a later proposer
         always hears about a chosen value and must re-propose it.
Liveness not guaranteed: duelling proposers can pre-empt each other
         forever. Multi-Paxos elects a stable leader to stop the duel.

Watch the “chosen” line in the ACCEPTORS box. BROKEN: IGNORE PROMISES ends with X and Y both chosen. FIX: ADOPT HIGHEST runs the same messages and ballot 2 re-chooses X. DUELLING PROPOSERS never breaks safety. Ballots climb 1, 2, 3 (P1 takes odd numbers, P2 even, so P1’s next is 5). No accept reaches a majority. A1’s lone acceptance of (1, X) is why the stable leader’s ballot 5 re-proposes X.

BREAK IT NOT YET

P2 runs the buggy proposer: it sends its own value Y no matter what the promises report. Every message is a step. Order them so that both X and Y end up chosen.

GOAL: X and Y are both chosen

FIX IT NOT YET

Return { onPrepare, onAccept, chooseValue }. An acceptor is { promised, accepted } with accepted = { n, value } or null. onPrepare(acc, n) promises if n is higher than anything promised and returns { ok, accepted }. onAccept(acc, n, value) accepts unless it promised a higher ballot, and returns true or false. chooseValue(promises, myValue) gets a majority of promises and returns the value to propose. The grader replays two proposers against your rules.

Mental model:

a ballot is a lease on the right to propose, and a promise is the acceptor’s memory handed to the next leaseholder. Safety comes from overlapping majorities plus one rule: re-propose what you hear. Liveness is a separate problem: competing proposers can pre-empt each other forever, which is why every real system puts a stable leader in front.

IN PRODUCTION

Google Chubby and Spanner (Multi-Paxos), Apache ZooKeeper (Zab, a close relative), Cassandra lightweight transactions (Paxos per partition), and every Raft cluster, which is Multi-Paxos with the leader election made explicit. Bugs live in the recovery path: a new leader that does not adopt in-flight values from the old one.

IN INTERVIEWS

“Walk me through Paxos. Why does it need two phases?” Explain prepare and promise, accept and chosen, why the proposer must adopt the highest-numbered accepted value, why two majorities guarantee it hears about a chosen value, and why duelling proposers threaten liveness but not safety. Compare to Raft's leader election and log matching.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: CAP THEOREM →