Replication Lag

You wrote it, but the replica hasn't heard yet

L214 MINDISTRIBUTED SYSTEMS
PREDICT

Writes go to a primary that copies them to replicas asynchronously. You pin every client's reads to one replica (sticky sessions). Client A writes its email, then reads it back from its pinned replica. Which guarantee do you now have: read-your-writes (you always see your own write) or monotonic reads (what you see never goes back in time)?

With asynchronous replication the primary acks a write as soon as it is in its own log. Replicas replay that log later, usually milliseconds later, sometimes seconds. A read sent to a replica reads the past.

Two guarantees break. Read-your-writes: after your write is acked, you never read older than it. Monotonic reads: you never read older than something you already read. Both are per-client promises. You keep them by tracking the client’s log position (LSN) and refusing to read from any node behind it.

▶ SIMULATION
PRIMARY
LSN 100 · email old
WAL entries not on every replica: 0
t = 0 ms · routing: round-robin
CLIENT READS (value@LSN)
A: —
B: —
stale reads 0 · backwards reads 0
REPLICAS (async)
R1: LSN 100 · email old · lag 0 ms
R2: LSN 100 · email old · lag 0 ms
last eventeveryone at LSN 100, email = old
✓ read-your-writes: no client reads older than its own acked write✓ monotonic reads: no client reads older than what it already read
SCHEDULE
01[SYS]reads go to any replica (round-robin)
02[A]A: UPDATE email = new → primary
03[REPL]R2 applies the next WAL entry
04[A]A: SELECT email
05[B]B: SELECT email
06[B]B: SELECT email
07[REPL]R1 applies the next WAL entry
08[SYS]report
Start of stale read scenario. 8 steps.
// write to the primary, read from whatever the balancer picks
await primary.query("UPDATE users SET email = $1 WHERE id = $2", [email, id]);

const replica = loadBalancer.pick();          // any replica, any lag
return replica.query("SELECT email FROM users WHERE id = $1", [id]);

Watch the CLIENT READS box and both invariants. STALE READ breaks both: A reads old@100 from R1, and B goes new@101 → old@100. LSN ROUTING sends every read to a node at or past the client’s token and keeps both green on the same schedule. LSN ROUTING, BOTH REPLICAS BEHIND shows the fallback: A reads before either replica has replayed its write, so that read goes to the primary. STICKY REPLICA fixes B and still serves A stale.

BREAK IT NOT YET

Reads go round-robin: R1, R2, R1, … Client A writes and then reads its own email. B reads twice. Order every step so that A reads older than its own write and B sees the new email and then the old one.

GOAL: A reads stale after its write and B reads go backwards

FIX IT NOT YET

Write route(read, clientLsn, primary, replicas). clientLsn is the highest LSN this client has written or read. Return a replica whose appliedLsn is at least clientLsn; only if none is caught up, return primary. Prefer replicas: the primary is the one node you cannot scale.

Mental model:

an async replica is the primary, some time ago. A client’s consistency is a position in the log, not a server: remember the highest LSN you wrote or read, and only read from nodes that have replayed at least that far. If none has, read the primary or wait. Sticky routing stops you going backwards; it does not bring you forwards.

IN PRODUCTION

Postgres streaming replicas (pg_last_wal_replay_lsn()), MySQL GTIDs with WAIT_FOR_EXECUTED_GTID_SET, MongoDB causally consistent sessions, Aurora and Cloud SQL read replicas behind a proxy. The usual shortcut is to read from the primary for a few seconds after a write. It works until replication lag exceeds that window.

IN INTERVIEWS

“Users report their changes don't save, but the data is in the database. What is going on?” Explain async replication lag, name read-your-writes and monotonic reads, give the LSN-token fix and its fallback to the primary, and say why sticky sessions and a fixed read-from-primary window are only partial fixes.

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: LEASES & FENCING TOKENS →