Where a cache actually helps
A cache is a bet that the same data will be asked for twice, soon, and can be slightly stale.
What you will be able to do
- Estimate the capacity gain from a given hit ratio
- Place a cache at the right layer of a design
- Explain what must be true for a cache to be safe
- Recognise when a cache is the wrong fix
The arithmetic of a hit ratio
If 100,000 reads per second arrive and the hit ratio is 95%, 5,000 reach the database. At 99%, 1,000 do. This is why a small improvement in hit ratio is worth more than an extra database replica, and why the hit ratio belongs on a dashboard.
reads/sec = 100,000 hit ratio = 0.95 db reads/sec = 100,000 x (1 - 0.95) = 5,000 hit ratio = 0.99 db reads/sec = 1,000 -> one replica you no longer need
Note the third row. A cache in the same building saves network and query work, but a database with the row in its buffer pool is already sub-millisecond. The cache wins when it avoids expensive computation, serialisation of large objects, or a fan-out — not merely because it is memory.
Layers, and what each one protects
| Layer | Holds | Protects |
|---|---|---|
| Browser / client | Static assets, API responses | Your bandwidth and the user's latency |
| CDN edge | Images, video, JS, cacheable JSON | Your origin entirely for cache hits |
| API gateway | Assembled responses | Backend CPU for repeated identical requests |
| Service memory | Hot objects, config, computed views | Network hop and deserialisation |
| Shared cache (Redis) | Sessions, counters, query results | Database reads across all instances |
| Database buffer pool | Pages recently read | Disk I/O |
Each layer multiplies the effect and adds a place where staleness can hide. In a design round, name two or three layers with a reason, not six because they exist.
When not to cache
- Low hit ratio. A long tail of unique requests fills memory with one-hit objects and evicts the hot ones.
- Writes dominate. Every write invalidates, so a write-heavy path gets little benefit and extra complexity.
- Correctness is the product. Balances, inventory, and permissions should be read from a source that is authoritative, or cached with a version the reader verifies.
- The query is the problem. An unindexed scan cached for 60 seconds is still a scan sixty times an hour.
One more: a cache hides load from capacity planning. Teams discover their real traffic only when the cache is cold after a deploy — the thundering herd. Mitigate with request coalescing (one loader per key), staggered warm-up, and a TTL jitter so entries do not all expire together.
Worth remembering
- Cache hit ratio determines everything: 90% means one in ten requests reaches the database, 99% means one in a hundred.
- The working set — the hot subset actually read — is usually far smaller than total data.
- A cache is safe only when stale data is acceptable and invalidation has an owner.
- Adding a cache to a slow query hides the problem; fixing the query is still the cheaper win.