Part II — Replication & Consensus · Chapter 7

State machine replication

Hook

Raft and Paxos both end with the same output: an agreed-upon, ordered list of commands. Neither one says anything about what those commands do. So how does "everyone agrees on a log" turn into "everyone agrees on the state of a running service"?

Intuition
Leader (applied all 5)101.0Follower (applied 0)0.0
Command 1 of 5 next: SET x = 10

Step the Follower forward one command at a time. It started behind the Leader, applying nothing — but each click applies the exact next command from the same log. Watch it converge to the Leader's exact value once it's caught up.

Formalize

State machine replication is consensus plus one extra ingredient: every replica runs the same deterministic state machine over the same ordered log.

  1. Consensus produces the log

    Raft, Paxos, or PBFT agree on the order of commands — not on what they mean or do.

  2. Every replica applies the same function

    Each replica feeds that log, entry by entry, into an applyCommand(state, cmd) function that is a pure, deterministic function of its two inputs — no clocks, no randomness, no reading anything outside the log.

  3. Same log + same function = same state

    Because the function is deterministic, any two replicas that have applied the same prefix of the log — regardless of when — are guaranteed to be in exactly the same state.

  • log — the ordered list of commands consensus agreed on.
  • applyCommand(state, cmd) — a pure function: same inputs always produce the same output.
  • state — the replica's local copy of whatever's being replicated, built entirely from the log.
Play
Canonical order101.0Reordered (same commands)11.0
After 5 commands: canonical x = 101, reordered x = 11 — same commands, different order, different state

Slide the same number of commands into two different orderings of the identical five-command set. Determinism guarantees agreement given the same order — it says nothing about two different orders of the same commands, and the diverging bars show exactly why the order itself has to be agreed on, not just the set of commands.

Worked example

A single-key register, starting empty. The agreed log:

  1. Apply each command in order
    1. SET x=10 → x=10
    2. ADD x+=5 → x=15
    3. ADD x+=-3 → x=12
    4. SET x=100 → x=100
    5. ADD x+=1 → x=101
  2. A replica lagging at command 3

    Suppose a replica only applied the first three commands: it's sitting at x=12 — exactly what appliedPrefix(LOG, 3) returns.

  3. It catches up, not restarts

    Applying the remaining two commands (SET x=100, ADD x+=1) from x=12 lands on x=101 — identical to a replica that applied all five commands from the start. Determinism doesn't care how you got to a prefix, only that you apply the same suffix afterward.

  4. Now reorder the same five commands

    Swap the first and fourth entries: SET x=100 now runs before SET x=10. Tracing the new order:

    1. SET x=100 → x=100
    2. ADD x+=5 → x=105
    3. ADD x+=-3 → x=102
    4. SET x=10 → x=10
    5. ADD x+=1 → x=11

    Same five commands, same final ADD x+=1 — but set isn't commutative with add, so the final value is x=11, not x=101.

Checkpoint

Slide the number of applied log entries until the state machine reaches x = 12.

x after 0 commands0.0
x = 0
Slide n to try a value
Summary

Consensus's job ends at producing an agreed, ordered log. State machine replication is the other half: apply that log through a deterministic function, and agreement on the log becomes agreement on an entire service's state — no matter how many replicas, or how far behind any one of them started.