1.Why caching works: locality and avoiding repeated work
Caching 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.
Key features
- Temporal locality: data accessed recently is likely to be accessed again soon (a trending article, a logged-in user's own profile)
- Spatial locality: data near recently accessed data is likely to be accessed next (adjacent rows, related records fetched together)
- Caching only helps when the underlying computation or fetch is genuinely expensive relative to a cache read — caching something already fast adds cost for no benefit
- A cache trades memory (or a faster storage tier) for avoiding repeated CPU, I/O, or network cost
- Caching helps most when access patterns are skewed — a small number of keys account for a large share of requests (the "hot" subset)
What makes data a good caching candidate
- 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
Signature
caching pays off when: expensive to produce + read >> written + skewed access
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.