Eventual Consistency in Assertions

The sleep that fixed the flaky test until the replica got busy

L215 MINTESTING
PREDICT

A flaky read-after-write test is 'fixed' with sleep(500). Replication lag is usually 120 ms. A month later the replica gets busy and lag reaches 900 ms, still inside the 2000 ms SLO. What happens to the build?

A write goes to the primary. A read goes to a replica, a search index or a queue consumer, and that copy catches up after some lag. The lag is not a constant. It grows when the replica is busy, and it is zero on the laptop where you wrote the test. If you want the mechanism itself, read replication lag.

A test that reads straight after the write races the copy. A test that sleeps first races it with a bigger head start, and loses on the first bad day. The honest test waits for the condition, not for a duration, and it gives up at a deadline taken from the promise the system makes.

▶ SIMULATION
TEST
waits: read straight after the write
await api.write(row); expect(await replica.read(id)).toEqual(row);
REPLICA RUNS
no runs yet
SLO: row visible within 2000 ms
TEAM SEES
red builds: 0 of 0
red but replica fine: 0
last—
✓ the build is red exactly when the replica misses the 2000 ms SLO
SCHEDULE
01[TEST]test: write, then read the replica at once
02[CI]run 1: lag 0 ms (replica was idle)
03[CI]run 2: lag 120 ms
04[CI]run 3: lag 340 ms
05[CI]run 4: lag 0 ms
06[TEAM]report
Start of break: read straight after the write scenario. 6 steps.
// Read-after-write against a replica. Passes when the replica is quick.
await api.write(row);
await sleep(500);                       // 'fixes' it until lag grows
expect(await replica.read(row.id)).toEqual(row);

Watch the red builds next to the replica column. In READ STRAIGHT AFTER THE WRITE and SLEEP 500 MS, builds go red while the replica is within its SLO. In POLL, the one red build is the run where the row never arrives, and that red is the correct answer.

BREAK IT NOT YET

The test writes, sleeps 500 ms, then reads the replica once. The code is correct and replication keeps its 2000 ms promise. Insert one run with a replica lag that turns the build red anyway.

GOAL: the build goes red although the replica met its SLO

FIX IT NOT YET

Write eventually(check, { timeoutMs, intervalMs }, clock). check returns a truthy value when the condition holds. A falsy return, or a thrown error, means "not ready yet". clock is simulated: clock.now() in ms and clock.sleep(ms) advances it. Return the first truthy value, as soon as it appears. If the deadline passes first, throw an error whose message includes the last error from check. Make one last check exactly at the deadline, then throw; never check or return after it, and never wait longer than needed. The grader tries fast, slow, never, a check that throws while not ready, and a deadline that is not a multiple of the interval.

Mental model:

Wait for the condition, not for a duration. A poll returns the moment the state is right, so fast runs stay fast, and it fails at a deadline you took from the SLO, so a broken replica still turns the build red. A fixed sleep is a guess that someone has to raise later.

IN PRODUCTION

Integration tests that write through an API and read from a read replica, a search index such as Elasticsearch, an event consumer or a cache. Also end-to-end UI tests that wait for a spinner. Test libraries ship the helper: Jest and Testing Library have waitFor, Awaitility in Java and Eventually in Go test suites do the same job. Related: replication lag, timeouts.

IN INTERVIEWS

“This integration test is flaky because the read hits a replica. How do you fix it?” Say why sleep is wrong, describe polling with a deadline set from the SLO, report the last error on timeout, mention testing the helper with a fake clock, and note that if the product promises read-your-writes, the test should read from the primary and a separate test should check the promise.

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: Snapshot Tests →