Replication Lag
You wrote it, but the replica hasn't heard yet
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.
// 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]);// remember the LSN of your last write (and of your last read)
await primary.query("UPDATE users SET email = $1 WHERE id = $2", [email, id]); // autocommit
// read the LSN after COMMIT, on the same connection: a token taken
// inside the transaction is older than its commit record
const { lsn } = await primary.query("SELECT pg_current_wal_lsn() AS lsn");
session.token = max(session.token, lsn);
// read from a replica that has replayed at least that far, else the primary
const node = route(read, session.token, primary, replicas);
const row = await node.query("SELECT email FROM users WHERE id = $1", [id]);
session.token = max(session.token, node.appliedLsn); // monotonic reads too
return row; 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.
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
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.
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.
“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.
USED IN
Chapters that build on this lesson.
- Replication: physical, logical, sync and asyncDatabases
- Technical decisions as a managerEngineering Manager