"Caching makes things faster"
The claim
Section titled “The claim”“The database is slow. Put a cache in front of it.”
The counterexample
Section titled “The counterexample”A service reads product details. A database read takes 10 ms. The team adds a remote cache: a lookup takes 2 ms. On a hit, the cache answers. On a miss, the service has already spent 2 ms on the cache and still needs the 10 ms database read.
| Assumption | Value |
|---|---|
| Database read | 10 ms |
| Cache lookup (hit or miss) | 2 ms |
| Traffic | 5,000 reads per second |
| Average latency with a hit rate h | h × 2 ms + (1 − h) × (2 + 10) ms |
| Hit rate | Average latency | Compared with no cache (10 ms) |
|---|---|---|
| 90% | 3 ms | 3.3 times faster |
| 50% | 7 ms | Faster |
| 20% | 10 ms | No better |
| 10% | 11 ms | Slower |
Below a 20% hit rate, the cache makes the average slower. Low hit rates are common: highly personalized data, long-tail catalogs, keys with random components, or a cache too small for the working set.
The cache also creates two failure modes the database never had.
The stampede. One product is on the home page and gets 5,000 reads a second. Its cache entry expires. Rebuilding it takes about 200 ms. During those 200 ms, every request misses and goes to the database: about 1,000 identical queries at once, for one row.
The cold cache. With a 90% hit rate, the database sees only 500 of the 5,000 reads a second, and over time it’s sized for that. Then a deployment flushes the cache, or the cache cluster restarts. The database suddenly receives all 5,000 reads a second: ten times what it’s now sized for. The cache has become load-bearing, and its failure takes the database down with it.
- Every request pays for the cache lookup, hit or miss.
- On a miss, the database read comes on top, and after a flush every request is a miss.
Try it yourself
Section titled “Try it yourself”Change the hit rate and the cost of a cache lookup. The chart shows the average read time at every hit rate, against reading from the database directly. The first scenario reproduces the example above.
The mechanism
Section titled “The mechanism”A cache is a bet that the same data will be read again soon. It pays off in proportion to the hit rate, and every miss pays the lookup cost plus the original cost. The break-even hit rate is the lookup time divided by the backend time: here, 2 ÷ 10 = 20%.
Beyond averages, caches change the system’s failure behavior. They concentrate load in time (stampedes when popular entries expire) and they hide how much capacity the backend really needs (cold starts after flushes). A system that only works with a warm cache has a new single point of failure.
When the claim holds
Section titled “When the claim holds”- The hit rate is high: the same data is read many times, and the working set fits in the cache.
- The cache is much faster than the backend, ideally in-process memory rather than a network hop.
- Stale data is acceptable for the cache’s lifetime, or invalidation is reliable.
- The design handles expiry and cold starts: request coalescing, staggered or refreshed expiry, and a backend that survives a cold cache or warms up gradually.
The better rule
Section titled “The better rule”Cache when the hit rate will comfortably exceed the lookup-to-backend latency ratio, and design for misses: coalesce concurrent misses for the same key, refresh hot entries before they expire, and make sure the backend survives a cold cache, or plan how to warm it.
For educators
Section titled “For educators”Discussion question. Your dashboard shows a 95% cache hit rate and fast average latency, but the 99th percentile is terrible. What could the cache be doing to the slowest requests?
Exercise. The team moves the cache in-process, so a lookup takes 0.1 ms instead of 2 ms. What’s the new break-even hit rate? What new problem does an in-process cache bring when the service runs as 40 instances?
Worked answer
Break-even is 0.1 ÷ 10 = 1%: almost any reuse now pays off. But each of the 40 instances keeps its own cache. A key read once per instance misses 40 times, so the hit rate per instance is lower than a shared cache would get. Invalidation must reach all 40 copies, and a deployment that restarts every instance creates 40 cold caches at once. A common answer is two levels: a small in-process cache for the hottest keys in front of a shared cache.
Seen in the real world
Section titled “Seen in the real world”- Facebook’s memcache paper describes stampedes (“thundering herds”) at scale and the leases they added to prevent them: Scaling Memcache at Facebook (opens in a new tab) (NSDI 2013).
- Amazon’s Builders’ Library covers cache stampedes, cold starts and the risk of depending on a cache’s availability: Caching challenges and strategies (opens in a new tab).
Related pages
Section titled “Related pages”Written and edited by Abhinav Taduka, enterprise software architect.
New pages are added regularly. Follow Abhinav on LinkedIn to see them first.