You Added Read Replicas, and Your Users Started Watching Their Own Saves Disappear
Routing GETs to read replicas is the most common scaling move in web backends, and it quietly breaks read-after-write consistency. Why the sleep(400) patch is worse than it looks, how to route reads using Postgres LSNs or MySQL GTIDs, where the position token has to travel across your API and queues, and what the whole thing costs you.

A bug report lands: "I changed the post title, hit save, the page reloaded, and it still shows the old one. I did it three times, so I guess there are three of them now?"
Nothing is broken. The write committed. The read went somewhere else.
This is what you buy when you add read replicas and point your GETs at them, which is the most common scaling move in web backends and the one people make with the least thought. You went from one copy of the truth to several copies that agree eventually. "Eventually" is usually 5ms. It is also occasionally 9 seconds, because somebody ran a migration, or a backup, or a delete that touched two million rows.
The 200 that lies
The API said the write succeeded, which was true, then answered the next question with a snapshot from before the write, which was also true. Both responses are correct on their own. The session is what's wrong. The user did a thing and the system forgot.
DDIA calls the guarantee you want read-after-write consistency, and names a sibling you also want: monotonic reads. Once a user has seen a newer version, they should never see an older one again. Round-robin replica selection breaks that one too. Refresh twice, land on replica A and then replica B, and the title flickers. I watched a QA person lose twenty minutes to that and write it up as "save button is random".
The patch everyone writes first
You know this patch.
await save(post);
await sleep(400); // let the replica catch up
await refetch(post.id);Or the classier version, where the mutation response gets merged into local state so nothing refetches at all. The optimistic one is genuinely fine for the screen you're standing on, and it's why plenty of teams never notice they have this bug: that component already holds the new value. The bug shows up on the next page, in another tab, in the PDF export worker, in the webhook that reads the record back before sending it.
The sleep is worse than it looks, and not because 400ms is too short. It usually isn't. It's bad because it hardcodes a guess about replication lag into application code, and lag is the one number your app can't see. No sleep value is correct during a lag spike, and the rest of the time you've added 400ms to every save for nothing.
Ask the database where it is
Both of the big open source databases will tell you precisely how far along a replica is, and almost nobody asks.
In Postgres, every commit has a WAL position. After a write, get it from the primary:
select pg_current_wal_insert_lsn(); -- 3A/1C8E2A90On a replica, ask what it has applied:
select pg_last_wal_replay_lsn();Compare those two LSNs and you have a yes or no answer to "can this replica serve this user's read". Not a guess, an answer. MySQL gives you the same thing via GTIDs, plus a nicer primitive: WAIT_FOR_EXECUTED_GTID_SET(gtid, 0.05) blocks on the replica until it catches up or gives up, so you can wait 50ms instead of bouncing to the primary.
The routing is about thirty lines:
// on the way out of any write
const { rows } = await primary.query('select pg_current_wal_insert_lsn() as lsn');
res.setHeader('x-db-pos', rows[0].lsn);
// on the way into a read
function pickReader(minPos) {
if (!minPos) return leastLoadedReplica();
const fresh = replicas.filter(r => r.replayPos >= minPos); // cached, see below
return fresh.length ? pickSticky(fresh) : primary;
}One thing to get right: never query the replay position per request. That's a round trip to answer a question with the same answer for every request in that millisecond. Poll each replica every 100ms in the background, hold replayPos in memory, compare against the cached value. You end up slightly pessimistic, since your number can be 100ms stale and you'll send some reads to the primary that didn't need to go there. Good trade.
pickSticky is the monotonic reads half. Hash the session to one replica so a user stops bouncing between two machines with different lag.
The position has to cross your API boundary
Where that position lives is the real design decision, and it's an API question more than a database one.
A header or cookie on the browser session is the easy version. It also lets a client hand you an invented LSN from the far future, which pins all of their reads to the primary. Clamp it to the primary's current position and the attack evaporates.
Inside the system it gets messier. If your checkout service writes a row and then drops a job on a queue, the position has to ride along in the message body, or the worker reads the row before it exists and silently does nothing. That's where this pattern earns its keep, because the worker case corrupts data instead of merely annoying someone. Cloudflare's D1 did the honest thing and put it in the API surface: you pass a bookmark string between requests and the platform picks a replica that has it. Aurora and Vitess have their own flavors. If you're on a managed service, go check whether you already have this before you build it.
What it costs
Primary read traffic becomes elastic. During a lag spike, every session that just wrote falls back to the primary, which is precisely when the primary is already struggling. Put a ceiling on it: past some number of fallback reads per second, serve stale and mark the response as stale, or queue. The failure you want is a visibly out of date page, not a primary tipping over.
Here's the one that bites people. A cache in front of this makes the whole scheme moot. If Redis is holding a value that was read off a lagging replica, that staleness is now frozen for the full TTL and no LSN check saves you, because the read never reaches a database at all. Either key the cache entry by the position it was built from, write through the cache on mutation, or accept that those endpoints aren't read-after-write and keep users' own records off them.
Also: this is more moving parts. For a small app, sending every read to the primary and giving replicas only the analytics queries is the better engineering call, and I'd argue with anyone who says otherwise.
What I'd actually do on day one is much smaller than any of this. Reads go to the primary by default. Move specific endpoints to replicas on purpose, one at a time. Put replay lag per replica on a dashboard panel where you'll see it. Most teams arrive at LSN routing after the second incident regardless, so you may as well have the lag graph waiting.