Topic 082 / Core
Caching fundamentals: cache hierarchy, hit ratio
Chapter 6.1 · Caching Strategies
What and why
Every system already has a memory hierarchy — registers, cache, RAM, disk — trading capacity for speed (→ 9). Caching fundamentals extend that same trade-off across the whole request path: a web client, a CDN edge, a reverse proxy, the application tier, a distributed cache, and the database's own buffer pool each hold a copy of data closer to where it's used, at the cost of possible staleness. The hit ratio — the fraction of reads a layer can answer without going further — is the single number that tells you whether a layer is earning its keep. Builds on: → 9 (CPU, RAM, storage and I/O).
Diagram
| Layer | Typical location | Round trip (illustrative) | A hit skips |
|---|---|---|---|
| Browser cache | user's device | ~0 ms | any network call |
| CDN edge | PoP near the user | ~10–50 ms | the trip to the origin |
| Reverse proxy / gateway cache | edge of the origin network | ~1–5 ms | the application tier |
| Application / distributed cache | same datacenter (→ 87) | ~1 ms | a database round trip |
| Database buffer pool | inside the DB process | <1 ms | a disk read |
| Disk | origin store | ~5–10 ms (SSD) | — the last resort |
Each layer's miss is the next layer's problem; a request only reaches the database or disk once every layer above it has missed.
How it works
- A request arrives at the outermost layer that can possibly serve it (browser, then CDN, and so on).
- Hit: the layer returns its stored copy; nothing below it is touched.
- Miss: the layer forwards the request to the next layer down and, on getting an answer, usually stores a copy for next time.
- Hit ratio for a layer = hits ÷ (hits + misses) over some window; it is only meaningful with a window and a workload attached — "95% hit ratio" means nothing without knowing over what traffic.
- A cold cache (just started, or just flushed) has a hit ratio of 0% until it warms up; capacity planning (→ 102) for a cache must account for this warm-up period after a restart.
- The hierarchy compounds: the fraction of original requests reaching the database is the product of every layer's miss rate above it, not any single layer's miss rate alone.
Practical example
(numbers illustrative). 1,000 requests arrive at a two-layer hierarchy: a browser cache with a 50% hit ratio, and — for the 500 requests that miss — a CDN edge with an 80% hit ratio on what reaches it. Browser hits: 500 requests at 0 ms. CDN hits: 80% of the remaining 500 = 400 requests at 20 ms. Reach the origin: the remaining 100 requests at 100 ms. Mean latency = (500×0 + 400×20 + 100×100) ÷ 1,000 = (0 + 8,000 + 10,000) ÷ 1,000 = 18 ms, and only 10% of original traffic ever reaches the origin — the number that determines how much origin capacity to provision.
When to use it
- Use when: the same data is read far more than it changes, and a layer closer to the request measurably reduces load or latency on the layer below it.
- Avoid when: access is near-uniform with no hot subset (nothing to cache profitably), or correctness requires every read to see the absolute latest write.
- Failure scenarios: a layer with a low hit ratio that still costs memory and operational complexity for no benefit; a cold cache after a deploy or crash producing a latency and load spike exactly when the system is least ready for it; measuring hit ratio over too short a window and over-trusting the number.
- Simpler alternative: one well-indexed database (→ 64) with its own buffer pool is already a cache; add explicit layers only where measurement shows a specific layer's miss traffic is the bottleneck.
Practice
(numeric drills — 15–20 min; compute by hand, then verify). Use hit ratio h, hit cost Lh, miss cost Lm; mean latency = h·Lh + (1−h)·Lm unless stated otherwise.
- h = 80%, Lh = 2 ms, Lm = 40 ms. Find the mean latency.
- h = 95%, Lh = 1 ms, Lm = 25 ms. Find the mean latency.
- 1,000 requests: 50% hit layer 1 (0 ms); of the rest, 80% hit layer 2 (20 ms); the remainder reaches the origin (100 ms). Find the mean latency and the percentage reaching the origin.
- A single cache layer normally runs h = 90%, Lh = 1 ms, Lm = 30 ms. The cache goes down (every request now costs 30 ms). By what factor does mean latency rise?
- Hit cost is 1 ms, miss cost is 50 ms. What is the minimum hit ratio needed to keep mean latency at or below 5 ms?
Answer key: (1) 0.8×2 + 0.2×40 = 9.6 ms. (2) 0.95×1 + 0.05×25 = 2.2 ms. (3) mean = (500×0 + 400×20 + 100×100) ÷ 1,000 = 18 ms; origin share = 100 ÷ 1,000 = 10%. (4) normal mean = 0.9×1 + 0.1×30 = 3.9 ms; 30 ÷ 3.9 ≈ 7.7×. (5) solve 50 − 49h ≤ 5 → h ≥ 45/49 ≈ 91.8%.
Done when all five answers match the key.
Key takeaway
Caching is one idea repeated at every layer of the request path; hit ratio, measured over a real workload, is what tells you whether a layer is worth its complexity. Done when you can: name every layer between a browser and a disk, in order, and compute a hierarchy's blended latency and origin load from per-layer hit ratios. Used next in → 83 (cache-aside, the application layer), → 86 (eviction, what keeps a layer's hit ratio up), → 89 (CDN, the outermost layer).
References
None — this topic makes no external claims.
Source directory