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.
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
- A replica answers from the recent past. Some decisions can act on the past (a profile page can be a minute old); others cannot (charging a card needs the current balance). The design’s job is keeping those two kinds of reads on different paths.
- Replication lag is not an anomaly to apologize for; it is a number you measure and route around, like cache staleness. A replica 200 ms behind can serve a profile page; it cannot serve a balance.
- Most ‘database load problems’ are really ‘every read goes to the primary’ problems mislabeled as a hardware problem.
Follow-ups to expect
- How much lag is too much? The class table answers it: the alarm sits at the class threshold, not at a global guess.
- What if a replica falls minutes behind? It stops receiving lag-tolerant traffic: route by measured lag, serve without the panels that needed it, let it catch up.
- Does queueing writes harm consistency? It delays it, visibly and by stated policy; the class table again decides who may wait.
Red flags in an answer
- Replicas serving reads that decide writes, with no version check.
- “Lag is usually low”, with no measurement and no alarm.
- Read-your-writes left to chance.
- Reporting queries running on the primary “because the replica might be stale”.
Sources
- Read consistency, Amazon DynamoDB Developer Guide: the budget made into an explicit per-call choice. Eventually consistent as the cheaper option and strongly consistent as a priced one is exactly the routing table this answer describes.
- Designing Data-Intensive Applications, Martin Kleppmann, O’Reilly: chapter 5, replication lag and the read-your-writes problem. The monotonic-reads and consistent-prefix-reads discussion is the catalog of ways a copy that is only slightly behind still surprises readers.