Q18 · What patterns help reduce database load without harming consistency?

Back to 24 Architecture Questions.

The question is not “tune the database”. The axis it turns on: what reads may be served from a copy that is behind. Every offload moves a read onto data that is, by definition, not current. The design decides which decisions tolerate which past; consistency is harmed not by lag itself, but by making a current-needing decision from a behind copy.

Route by staleness tolerance A reader who just wrote goes to the primary, because it needs its own write. A reader that tolerates a few seconds of lag goes to the replica, which trails the primary by the replication lag. FIGURE 18 ROUTE BY STALENESS TOLERANCE Primary owns writes Replica seconds behind replication lag Reader A just wrote Reader B tolerates 5s read-your-writes stale-tolerant read the primary keeps only the reads that must be current; everything else is offloaded by class

What the answer should contain

Enumerate the read classes first. Each class gets a staleness tolerance: read-your-writes (zero for the user’s own writes), session-sticky, seconds-tolerant, minutes-tolerant analytics. This table is the entire safety argument: offloading is safe exactly where the table says it is, and nowhere else.

Offload the lag-tolerant bulk to replicas. Automatic, no invalidation code, and lag is a measured number with an alarm, not a surprise. The primary keeps the writes, the current-needing reads, and nothing else.

Solve read-your-writes with a token, not with hope. After a write, the user’s next read either goes to the primary or waits for the replica to pass the write’s position (catch-up token / session pinning). Without this, users see their own writes vanish and support tickets arrive before the lag does.

Guard the decision reads. The dangerous pattern is not the stale display; it is read-copy, decide, write: the decision assumes a premise that the copy’s lag just broke. The fix is a conditional write: guard the write with the version that was read, so a stale premise fails the write instead of corrupting it.

Attack write-side load too. Batching writes, fewer round-trips per transaction, moving reporting off the primary, pooling connections, queueing the writes that can wait. The cheapest query on the primary is the one that never reaches it.

Caches follow the same table. A cached read is a copy that is behind by its TTL or by event lag; it belongs to a class, and a class with no stated tolerance has no cache entry either.

Ideas worth mentioning out loud

Follow-ups to expect

Red flags in an answer

Sources