Why caching works: locality and avoiding repeated work
corebeginnerCaching works because real access patterns are not random — the same data tends to be requested again soon (temporal locality), and related data tends to be requested together (spatial locality). A cache stores the result of expensive work once and serves it repeatedly from somewhere far cheaper to read, exploiting exactly those patterns.
Think of it as
A cache is like keeping today's most-asked-for library books on a cart by the front desk instead of walking to the stacks every time someone requests one. It works because a small number of books actually account for most requests (temporal locality) and because someone asking for one popular book is likely to ask for a related one next (spatial locality) — the cart pays off precisely because requests are not evenly spread across every book in the building.
What we're doing: Show the actual cost difference a cache exploits, quantified.
- 5
- This is the cost caching is trying to avoid paying repeatedly — the expensive join, run on nearly every request.
- 13
- A 95% hit rate turns nearly all of that repeated cost into a single-millisecond cache read instead.
Why this works: The benefit of caching is not free or automatic — it is exactly proportional to how expensive the original work was and how often the same result would otherwise be recomputed, which is why locality (the same or related data being requested repeatedly) is the precondition that makes the trade worthwhile.
Caching data with a low hit rate or near-uniform access pattern
Wrong
Better
What you see: A cache layer is added, but its hit rate stays low (well under 50%) and the system still shows the same database load as before, plus the added memory and invalidation-logic cost of the cache itself — a sign the underlying access pattern didn't actually have the locality caching depends on.
Why: Caching's entire value proposition depends on the same or related data being requested again — without real temporal or spatial locality in the access pattern, a cache is paying memory and complexity cost for a hit rate too low to recoup it.
- Without a cache
- Every request runs the expensive 4-table join (~40ms)
- 10,000 requests/min → ~6.7 minutes of cumulative DB time
- Load scales linearly with traffic — no ceiling
- With a cache (95% hit rate)
- 9,500 requests/min served from cache in ~1ms each
- Only 500 requests/min still reach the database
- Database load drops by ~95% for the same traffic
What makes data a good caching candidate
Remember: Caching works because real access is skewed, not random — the same or related data gets requested again soon. It only pays off when the underlying work is genuinely expensive and read far more often than it's written.
See also: cache patterns · ttl eviction and invalidation

