Replicated state machines under FLP

When several servers must agree on the same answer despite crashes and unreliable networks, the network itself forbids any deterministic program from being both always-correct AND always-making-progress — so Raft, like every real protocol, treats correctness as non-negotiable and treats progress as something the network has to earn.

Scene 01

Replicated state machines under FLP

  1. Watch
  2. Try it
  3. Predict
  4. Capture
STATE MACHINESDELIVERY CHANNELSHARED COMMAND LOGSET x=1SET x=2INC x3/3 SHOWNSM_A✓ LIVEAPPLIED LOG12(no commands applied yet)STATE(empty)SM_B✓ LIVEAPPLIED LOG12(no commands applied yet)STATE(empty)SM_C✓ LIVEAPPLIED LOG12(no commands applied yet)STATE(empty)✓✓✓Async network: OFF·Partial synchrony: OFFThree servers, one shared to-do list of commands. Each server runs the same commands in the same order, so each server ends up in…
What to watch for

Suppose you run a service that has to stay up even if one or two of its servers crash. You can't trust a single machine — machines die, disks fail, networks drop packets — so you keep several copies (servers) and you need all the surviving copies to give the client the same answer. The tricky part is that the network between them is not reliable: messages get delayed, reordered, or lost, and a server that has gone silent could be either crashed or just slow. The systems-design name for the picture you're about to watch is a replicated state machine — three identical copies of a deterministic program (one whose output depends only on its inputs, never on a clock or a random number), each fed the SAME list of commands in the SAME order. That list is called a log in the systems sense: an append-only, totally ordered sequence of commands. When the same deterministic program processes the same log, every copy must end up in the same state. Watch the three commands [SET x=1, SET x=2, INC x] apply on all three servers — they converge to x = 3.

Continue unlocks when the animation finishes.
Implementation

Highlighted lines are the ones running in the diagram right now.

Replica.applyLoop() — the replicated state machine model
every replica runs THIS loop; determinism + same log ⇒ same state
# Each replica holds:
# log — append-only, totally ordered commands
# stateMachine — DETERMINISTIC: (state, cmd) -> state'
# lastApplied — index of the last command fed to stateMachine
def applyLoop():
while True:
# block until log[lastApplied + 1] is committed
entry = waitForCommitted(log, lastApplied + 1)
stateMachine.apply(entry.command)
lastApplied += 1
# invariant: same log prefix, same lastApplied,
# same stateMachine state — on every replica.
Replica.onTimeout() — why FLP forbids a deterministic decision
a slow replica is indistinguishable from a crashed one
# FLP (Fischer, Lynch, Paterson 1985):
# no deterministic protocol can guarantee BOTH safety AND
# liveness in a fully asynchronous network, even with one
# crash failure. Below is the shape of WHY.
on receive heartbeat from leader:
reset election_timeout
on election_timeout fires:
# The protocol cannot distinguish:
# leader is SLOW (timeout was too aggressive — wait)
# leader is CRASHED (timeout was correct — elect new)
# Any deterministic resolution in finite time can be
# made WRONG by an adversarial scheduler.
start_election()

Where this sits in Build Raft — consensus you can defend

Scene 01 of 12. Consensus on a replicated log lets N deterministic state machines converge — but FLP forbids any deterministic protocol from being both safe and live in a fully asynchronous network. Raft fixes safety as a theorem, concedes liveness to partial synchrony.

Up next. Now that you know correctness (safety) is the part Raft must guarantee no matter what the network does, the next scene asks: how can the servers even agree on the ORDER of commands when nobody trusts wall-clock time?

All 12 scenes in Build Raft — consensus you can defend · Every curriculum

Built with Arqly
Every scene in Build Raft — consensus you can defend builds on the one before it.All 12 Build Raft — consensus you can defend scenes