DETERMINISTIC SIMULATION
Run the distributed system under a scheduler you can replay
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.
// 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)// Follower: ignore anything older than what it already has
function onRepl(msg) {
if (msg.seq > this.seq) { this.x = msg.v; this.seq = msg.seq; }
}
// Simulator: one seeded PRNG chooses the next event
const rng = makeRng(seed);
while (queue.length) run(queue.splice(pick(queue, rng), 1)[0]);
// same seed -> same trace, every time, on every machine 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.
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
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.
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.
“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.
USED IN
Chapters that build on this lesson.
- Testing across services: fault injection and deterministic simulationSoftware Testing and Quality