tech, developers, and the code underneath

issue 237· essay·

The read replica trap

Adding a replica looks like free read capacity. It is free right up until replication lag becomes a correctness bug.

Read replicas are the standard answer to a read-heavy database: add a replica, point reads at it, halve the primary's load.

It works. It also introduces a class of bug that is intermittent, unreproducible locally, and disproportionately hits your most engaged users.

the bug#

python
order = db.write.create_order(...)      # goes to the primary
return db.read.get_order(order.id)      # goes to a replica

The replica has not received the write yet. The read returns nothing. The user places an order and lands on a page that says the order does not exist.

Replication lag is usually milliseconds, which is why this passes every test and every manual check. Under load it is tens of milliseconds, and then a small fraction of users see it. During a large batch job it is seconds, and then a lot of them do.

The users who hit it most are the ones doing things quickly — the power users, the ones running integrations, the ones you least want having a broken experience.

read-your-own-writes#

The property you actually need is that a user always sees at least their own writes. Four ways to get it, in increasing sophistication:

1. Route by operation. After any write in a request, use the primary for the rest of that request.

python
if request.did_write:
    db = primary

Crude, effective, and covers the common case. The gap is a subsequent request — a redirect after a POST reads from a replica again.

2. Sticky window per user. After a write, pin that user's reads to the primary for a short period.

python
cache.set(f"wrote:{user_id}", 1, ttl=5)   # 5s > worst observed lag

Covers the redirect case. The TTL must exceed real lag, so measure it rather than guessing.

3. Wait for the position. Record the write's replication position, and have the replica wait until it has caught up.

sql
-- Postgres: on the replica, wait for the primary's LSN
SELECT pg_wal_replay_wait('16/B374D848');

Correct rather than approximate. Costs latency exactly when needed and no more.

4. Route by query intent. Classify each read: does it need to be current, or is stale acceptable? An account balance needs current. A list of blog posts does not.

This is the most work and the best outcome, because most reads genuinely tolerate staleness and this is the only approach that lets them.

the other traps#

Lag is not constant. It spikes during batch jobs, vacuum, large migrations, and network problems. Your five-second window is a guess about the worst case, and the worst case is set by whatever runs at 2 a.m.

Monitor lag as a first-class metric. In seconds, not bytes — bytes are meaningless to reason about. Alert on it. A replica lagging by minutes serving "current" data is a correctness problem masquerading as a performance one.

Replicas do not reduce write load. They add to it — every write is shipped and applied on every replica. If the primary is write-bound, replicas make it worse.

Failover promotes a replica that may be behind. Asynchronous replication means promotion can lose the transactions that had not shipped. That is a durability decision, and a lot of teams discover which one they made during their first failover.

Long-running queries on a replica can block replay. In Postgres, a query holding a snapshot conflicts with applying changes that would remove rows it can see. Either the query is cancelled or replay pauses and lag grows. hot_standby_feedback trades bloat on the primary for query stability on the replica. Pick deliberately.

the honest question first#

Before adding a replica, check whether you need one.

A very large share of read load is the same queries returning the same results, and a cache in front of the database is simpler, cheaper, and does not introduce a consistency model. Replicas are the right answer for genuinely diverse read traffic against a large working set — which is a narrower case than "our database is busy."

And check the queries first. One missing index frequently accounts for more load than a replica would remove, and it takes an afternoon rather than a permanent increase in operational surface.

get README in your inbox

One dispatch, no noise. Tech and developer news, plus the occasional long piece on the craft.

subscribe →