Interactive engineering lab

Projection lag: read your own write with a position token

Projection lag: read your own write with a position token. Replay agent turns against a lagging CQRS view and compare three read strategies.

By Sandeep Belgavi · Published 2026-11-18

Synthetic fixtures computed locally. No model, event store, simulator or production service is contacted.

Experiment workspace

Decision trace

    The question this experiment answers

    Jenya's help-desk agent at Belgavi's Book Store records a return (a command on the write side of a CQRS service) and then reads the case view (a projection) to decide what to tell the customer. The projector applies events in order after a delay and can stall. This lab replays a stream of such turns and shows, for each read strategy, how many decisions were made on a view that did not yet contain the agent's own write, how many duplicate returns that caused, and how long the agent waited.

    Change one assumption at a time

    Start from read_immediately: 40 turns, one command every 50 ms, a delay of 20 ms plus up to 100 ms of jitter, 30 ms to apply each event, and the agent reading 100 ms after writing. Turn write-side deduplication on and see the duplicates disappear while the stale decisions stay. Switch the strategy to wait_for_position. Raise apply time above the gap between commands to build a backlog. Then add a stall before turn 20 and compare the three timeout policies: read the write model, report the change as pending, or act on the stale view.

    Expected outcomes

    With seed 18: read_immediately makes 28 stale decisions and 28 duplicate returns out of 40 turns, with 12 correct replies and a largest lag of 145 ms. With deduplication the 28 resends are suppressed, but the 28 stale decisions remain. wait_for_position makes no stale decision, waits 35 ms on average and at most 50 ms. With apply time 70 ms, reading immediately is stale on all 40 turns, the lag reaches 910 ms, and waiting averages 440 ms (at most 850 ms). With a 1,500 ms stall before turn 20, 21 waits time out: reading the write model keeps all 40 replies correct, reporting pending gives 19 correct and 21 pending replies, and acting on the stale view brings back 21 duplicate returns.

    Reading the decision trace

    The first line restates every input. Each later line is one turn: when the command was written and its position, when the projection applied it (and whether a stall preceded it), when the agent read, the outcome (fresh, fresh after a wait, timeout with the chosen policy, or stale) and whether a duplicate return was recorded or suppressed. The last line summarises the run. The table lists every turn; the export contains every row, the trace, the inputs and these limits.

    What this teaches

    Write-side idempotency limits the damage of a stale read but does not stop the agent deciding on stale data. Returning the write position and waiting for the projector checkpoint does, at the cost of a short wait when the projector is healthy. A growing wait is a lag alarm, and the timeout policy is a business decision that should never default to acting on the stale view.

    Limits of this model

    One writer stream, one in-order projector and one read per turn. A stale read always leads the agent to re-plan and resend the command; real agents may do something else. Delays are synthetic draws from a seeded generator, not measurements of any event store, and positions are a single global sequence.

    Views are my own and do not represent any employer, past or present.