DETERMINISTIC SIMULATION

Run the distributed system under a scheduler you can replay

L320 MINTESTING
PREDICT

A test passes on your laptop 5,000 times in a row and fails once a week in CI. The failure needs two messages to arrive in the opposite order. You re-run the failing build with the same code. What is the best bet for the next run?

Two nodes, A and B. A takes writes and sends B a replication message for each. B applies messages in the order they arrive. On a quiet network they arrive in the order they left, so the bug stays hidden: if two messages swap, B keeps the older value and the nodes disagree for good.

Deterministic simulation removes the sources of chance. The simulator holds a queue of pending events, and a seeded generator picks which one runs next. Nothing else is allowed to decide: no wall clock, no real network, no Math.random. The seed is the whole run. The FoundationDB docs say it runs “a deterministic simulation of an entire FoundationDB cluster within a single-threaded process” and that “determinism is crucial in that it allows perfect repeatability of a simulated run”. A real simulator also injects delays and crashes through the same generator. Compare it with the flaky test you fixed by finding the interleaving: here the simulator picks the interleaving for you, and keeps a record of it.

▶ SIMULATION
NODE A (PRIMARY)
x = 0
last seq applied: 0
SCHEDULER
mode: arrival order
pending: w1 w2
NODE B (FOLLOWER)
x = 0
last seq applied: 0
trace—
✓ when the queue is empty, follower B holds the same value as primary A
SCHEDULE
01[SIM]a quiet network: messages arrive in order
02[SCHED]scheduler: pick event 1 of 4 from the queue
03[SCHED]scheduler: pick event 2 of 4 from the queue
04[SCHED]scheduler: pick event 3 of 4 from the queue
05[SCHED]scheduler: pick event 4 of 4 from the queue
Start of laptop: fifo scenario. 5 steps.
// Follower: apply replication messages in arrival order
function onRepl(msg) {
  this.x = msg.v;        // an older message arriving late overwrites a newer one
}

// Test scheduler: the real network and the OS pick the order
// (so the bad order may appear once in thousands of runs)

Watch the invariant strip: converge fails only when the follower ends up behind the primary. LAPTOP never fails because arrival order is the only order it tries. In SEEDED the same four events run in a different order for each seed, and some seeds break the invariant. The last two scenarios use a fixed seed, so the failing run is the same each time you press play; the fix passes under that very seed.

BREAK IT NOT YET

The simulator runs the same four events under a seeded scheduler. Move the SEED until the follower ends up with a different value from the primary.

GOAL: the run ends with A and B disagreeing

FIX IT NOT YET

Write the scheduler step. pick(queue, rng) receives the pending events (an array; do not change it) and a seeded generator: rng() returns a number in [0, 1) and rng.int(n) an integer in [0, n). Return the index of the event to run next. The same seed must give the same trace, different seeds must explore different orders, and some seed must reach the bad order. No Math.random and no Date: the grader throws if you touch them.

Mental model:

A bug you cannot repeat is a bug you cannot fix or prove fixed. Route every source of nondeterminism (order, delay, crashes, time, randomness) through one seeded generator and a failure becomes a seed: you can replay it, shrink it, fix it, and keep it as a regression test. Everything the code under test needs from the outside world must be something the simulator hands it.

IN PRODUCTION

Database and storage engines that cannot afford a once-a-year corruption, FoundationDB's simulation being the best documented. In your own services the cheap version is a fake clock, a fake network with a seeded delivery order, and a property checked after every run. The hard part is the discipline: one stray `Date.now()` or real socket breaks replay.

IN INTERVIEWS

“How would you test a distributed system for race conditions?” Say you would not rely on sleeping and retrying real runs. Describe a simulator: fake network and clock, one seeded generator choosing delivery order, delays and crashes, invariants checked after every run, failing seeds saved as regression tests. Mention the cost: the whole system must be written so it can run under it.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: CAPSTONE: THE GREEN SUITE →