Caching — Hit Rates, Zipf and the Working Set
What a cache of a given size actually buys under realistic popularity skew, and why prefix caching is governed by a different quantity than object caching.
What a hit rate is worth
Let be the probability a request hits, the cost of a hit, and the cost of a miss (including the work to populate the cache). Then
The speedup relative to no cache is
and for below the miss term dominates as soon as .
Key idea
With , a hit rate of 90% gives speedup , and 99% gives . The last percent of hit rate is worth more than the first fifty. Cache work is therefore worth doing precisely when you are already close to the ceiling — the opposite of most optimisation.
Where hit rates come from
Items ranked by popularity have request probabilities
with the skew exponent. is the generalised harmonic number. Larger means more concentrated popularity.
A cache of capacity holding the most popular items — the best any eviction policy can do against i.i.d. requests — achieves hit rate
For and ,
for , , and for , a constant close to even for .
Checking the approximation against exact sums with :
| exact | approximation | ||
|---|---|---|---|
| 0.5 | 0.031 | 0.032 | |
| 0.5 | 0.099 | 0.100 | |
| 0.8 | 0.207 | 0.251 | |
| 1.0 | 0.520 | 0.500 | |
| 1.2 | 0.822 | — |
Key idea
For the hit rate is a power law in cache size, so there is no knee. Going from 1 GB to 10 GB multiplies the hit rate by — at , a factor of — and going from 10 GB to 100 GB does exactly the same again. Cache sizing is therefore an economic question with no natural stopping point, which is why it is usually settled by cost per byte rather than by finding a threshold.
LRU rather than an oracle
Real caches do not know the popularity ranking. The standard analytic handle is Che's approximation.
Heuristic — a reasoning shortcut that predicts the right answer in the regimes discussed here. It is not a proof and can fail outside them.
Define the characteristic time as the unique solution of
where is the total request rate. Then item 's hit probability is approximately , and the overall LRU hit rate is .
Assumptions
The approximation replaces the per-item eviction time, which genuinely depends on the whole request sequence, by a single deterministic constant shared by all items. It is an approximation with no general error bound, reported to be accurate for large caches under independent-reference workloads, and it is offered here as a modelling tool rather than a theorem.
Prefix caching is a different problem
Caching an LLM's KV state for a shared prompt prefix does not fit the model above, and the reason is worth stating precisely: the unit of reuse is not an object but a prefix, and prefixes form a tree rather than a set.
Let a request have prompt length of which the first tokens are already cached. Using the prefill cost from Inference Arithmetic and the Roofline, the work saved is
so the saving is superlinear in the cached prefix length whenever the quadratic attention term is not negligible.
Key idea
Object caches save a fixed cost per hit; prefix caches save a cost that grows faster than linearly in how much of the prompt is shared. A system prompt shared across every request is therefore worth far more than its token count suggests, and the correct eviction policy weights entries by rather than by recency — a cost-aware policy, not an LRU one.
What this does not say
Nothing here models cache coherence. A read-only cache of immutable KV blocks has no invalidation problem; a cache of anything mutable does, and by the argument in Scaling Limits — Amdahl, Gustafson and the Coherency Penalty cross-replica invalidation introduces exactly the pairwise coherency term that produces a throughput maximum. Distributed caches with invalidation are in the retrograde regime, and their capacity curve should be measured rather than assumed monotone.
References
- PaperH. Che, Y. Tung, Z. Wang (2002). Hierarchical Web Caching Systems: Modeling, Design and Experimental ResultsSource of the characteristic-time approximation for LRU quoted here as a heuristic.
- PaperL. Breslau, P. Cao, L. Fan, G. Phillips, S. Shenker (1999). Web Caching and Zipf-like Distributions: Evidence and ImplicationsThe measurement study behind the Zipf modelling assumption and its consequences for hit rates.
These works are cited as sources to read, not reproduced. All exposition, examples and problems on this site are written independently; where an argument is standard, the citation points to a canonical treatment.
Results stated on this page
- PropositionEffective latency under a cacheproof includedMean latency is the hit rate times the hit cost plus the miss rate times the miss cost, so the benefit is linear in the hit rate and bounded by the miss cost.
- PropositionStatic optimal hit rate under a Zipf popularity lawproof includedCaching the C most popular of N items under Zipf exponent a gives a hit rate equal to a ratio of generalised harmonic numbers.
- CorollaryThe power-law form of the hit rateproof includedFor exponent below one the hit rate grows like (C/N)^(1−a), so each order of magnitude of cache buys a fixed multiplicative gain.
- CorollaryA cache cannot beat its own miss pathproof includedHowever large the cache, speedup is capped by the ratio of miss cost to hit cost, which is Amdahl's law with the miss rate as the serial fraction.
- DefinitionZipf popularityRank-ordered request probabilities proportional to a negative power of the rank, normalised by a generalised harmonic number.
- HeuristicChe's approximation for LRUReplacing each item's eviction time by one shared characteristic time gives a closed-form estimate of the LRU hit rate.
- PropositionPrefill saved by a shared prefixproof includedA cached prompt prefix of k tokens saves 2Nk + 4Ldk^2 FLOPs, which is superlinear in the shared length.