Q15 · How do you design caching layers (L1/L2) to avoid stale data and thundering herds?
Back to 24 Architecture Questions.
The question is not “add Redis”. The axis it turns on: how stale a read is allowed to be, and what happens when a key expires under load. The staleness tolerance decides what may be cached at all, and the expiry behavior decides whether the cache protects the origin or stamps it: expire one hot key and every instance hits the database at the same moment.
What the answer should contain
No staleness tolerance, no cache. Per data class, name the number: prices tolerate seconds, profiles tolerate minutes, stock answers must be current. If the business cannot name a number, caching that data is not a design, it is a bet with the product’s money.
Two layers, two jobs. L1 in-process: microseconds, per instance, bounded small, evicted by size; it exists to absorb hot keys without network hops. L2 shared: milliseconds, one copy for the fleet, survives instance restarts. L1 always sits in front of L2, never the reverse, and L1 entries carry a shorter TTL than L2 so the fleet converges.
TTL is the safety net, not the mechanism. Invalidation by event or write-through is the mechanism (write the DB, then bust or update the key); the TTL catches what the mechanism misses. Order matters: update the source, then the cache, never cache-first.
The herd is the failure mode. When a hot key expires, every instance misses together and the origin receives the same query N times at once. Three fixes, in order of strength: single-flight (one request refreshes, the rest of the herd waits for its result), jittered TTLs (instances expire at different times), and probabilistic early refresh (refresh before expiry under load).
Do not cache what cannot be shared. Per-request, per-user-session data in a shared cache is complexity nobody asked for, with a latency story attached.
Version the keys, not the payloads. user:17:v2 beats clever in-place payload evolution:
a deploy that changes shape reads a new key, and old entries age out by TTL instead of
poisoning readers.
Ideas worth mentioning out loud
- “A cache is a trade: faster reads for answers that are old by a known amount. Name the amount per data class, or do not cache the class at all.”
- Hit rate is a vanity metric next to staleness violations and origin QPS during expiry events. Measure the herd directly: origin QPS at the moment a hot key expires.
- Cold start is the same herd with a different cause: warm the hot keys before traffic (precompute at deploy), or single-flight absorbs the start too.
Follow-ups to expect
- Why not invalidate everything on deploy? It is a deliberate herd. Invalidate per class, with the herd protections on.
- Read-through, write-through, or write-behind? Read-through for read-heavy, write-through when the cache must track writes, write-behind only when a bounded loss window is acceptable and stated.
- How do you find the hot keys? Access logs and cache hit counters by key, top-N monitored; the herd finds you anyway, but finding it first is cheaper.
Red flags in an answer
- “TTL of a few minutes, seems fine.”
- Caching per-request or per-user data in the shared layer.
- L1 with no size bound.
- Cache updated before the source of truth.
- No jitter or single-flight on a hot key.
Sources
- Caching challenges and strategies, AWS Builders’ Library: TTL jitter, the thundering herd, and protecting the origin, from the team operating DynamoDB and CloudFront. The delayed expiration and jitter recommendations here are theirs.
- Scaling Memcache at Facebook, Nishtala et al., NSDI 2013: the largest documented shared-cache deployment, and the leases mechanism that answers the herd at the cache itself. Evidence that expiry under load is a first-class design problem, not a footnote.