Learn
Lesson · Caching and Hit Rates · The miss rate

Average Latency under a Cache

Under a cache a request has two possible latencies and nothing in between. The mean is the weighted average of the two, with the hit rate as the weight, and it is a number no request ever takes. The percentile is a different object: a rank on the same two outcomes, so it is always one of them, and it does not move gradually — it steps.

E[latency] = h·t_hit + (1 − h)·t_miss h = 95%, t_hit = 1 ms, t_miss = 40 ms mean = (19/20)(1) + (1/20)(40) = 59/20 = 2.95 ms P(latency ≤ t_hit) = h, so the q-quantile is t_hit exactly when h ≥ q p50 = 1 ms p95 = 1 ms p99 = 40 ms p99.9 = 40 ms
One expectation, and four ranks on the same two outcomes

Core ideas

What this lesson establishes

The distribution here has exactly two values in it. That is what makes both calculations short and what makes them disagree so visibly.

1

The mean is weighted, and the weight is the hit rate

E[latency] = h·t_hit + (1 − h)·t_miss is the expected value of Discrete Mathematics applied to a two-outcome random variable. At h = 95%, t_hit = 1 ms and t_miss = 40 ms it is 59/20 = 2.95 ms — nowhere near the midpoint 20.5 ms, because the outcomes are not equally likely.

2

No request takes the mean

Every request takes 1 ms or 40 ms. 2.95 ms is a property of the stream, not of any request in it, and it is the number that a cache improvement moves smoothly. If somebody reports a mean latency of 3 ms for a cached endpoint, one request in twenty is still taking 40.

3

A percentile is a rank, so it steps

P(latency ≤ t_hit) = h and P(latency ≤ t_miss) = 1. There is nothing between the two values, so the q-quantile is t_hit exactly when h ≥ q and t_miss otherwise. It cannot land between them, it does not interpolate, and it jumps by t_miss − t_hit all at once at h = q.

The material

An expectation over two outcomes, and the percentile that ignores it

The mean, the staircase it comes from, and the one hit rate at which each percentile flips.

Definition · Latency under a cache

A request is served in t_hit with probability h and in t_miss with probability 1 − h. Its mean latency is E[latency] = h·t_hit + (1 − h)·t_miss, and its distribution takes only those two values.

t_miss here is the whole miss path: the lookup that failed, the backend call, and the write back into the cache. It is not the backend’s service time alone.

The mean lies strictly between the two latencies whenever h is strictly between 0 and 1, and it equals neither. It sits close to t_hit because h is close to 1: the weighted average is dragged toward whichever outcome is common, and in a cache that is the hit.

Example · A ninety-five per cent cache over a forty-millisecond backend

h = 95%, t_hit = 1 ms, t_miss = 40 ms. The mean is (19/20)(1) + (1/20)(40) = 19/20 + 2 = 59/20 = 2.95 ms. The unweighted average of the two latencies is 20.5 ms, seven times too large, and it is the answer you get by forgetting that one outcome happens nineteen times as often as the other.

The percentile is a different question with a different answer

A percentile asks for a value, not an average: the q-quantile is the smallest t with P(latency ≤ t) ≥ q, which is the nearest-rank convention of “Percentiles from a Sample”. Here the function P(latency ≤ t) is a staircase with two steps — it is 0 below t_hit, h from t_hit up to t_miss, and 1 at t_miss and above.

P(latency ≤ t) 1 ┤ ┌────── │ │ h ┤ ┌─────────────────┘ │ │ 0 ┼──────┘ └──────┴─────────────────┴────── t_hit t_miss the curve is FLAT at h between the two values, so no percentile can land between them: q ≤ h → t_hit, q > h → t_miss

At h = 95% that gives p50 = 1 ms, p95 = 1 ms, p99 = 40 ms and p99.9 = 40 ms. The median and the p95 are the hit latency; the p99 is the miss latency, because more than one request in a hundred misses and the hundredth-slowest is one of them.

Where each percentile flips

The q-quantile becomes t_hit at exactly h = q and not before. So the p99 is the miss latency for every hit rate below 99%, and the p99.9 is the miss latency for every hit rate below 99.9%. Improving a cache from 95% to 98.9% does not move the p99 at all, and then one more tenth of a point moves it by 39 ms.

Example · Three hit rates, one tail

t_hit = 1 ms, t_miss = 40 ms. At h = 95% the mean is 2.95 ms and the p99 is 40 ms. At h = 99% the mean is 139/100 = 1.39 ms and the p99 has just become 1 ms — but the p99.9 is still 40 ms. At h = 99.9% the mean is 1039/1000 = 1.039 ms and the p99.9 is 1 ms too. The mean moved by under two milliseconds across that whole range; the tail moved in two jumps of thirty-nine.

This is why a cache is a poor answer to a tail problem and a good answer to a throughput problem. It removes work, which is what Hit Rate and Backend Load measured, and it shifts the mean, but the slow requests it leaves behind are exactly as slow as they were. To move the tail you have to move t_miss, or push h past the percentile you care about — and the second of those gets expensive fast, which is what Cache Size and Hit Rate is about.

The model assumes two latencies and one hit rate. A real cache has a spread on each — a hit is 0.8 to 1.4 ms, a miss depends on what the backend was doing — so treat t_hit and t_miss as the typical values and read the answer as a shape rather than a measurement. What survives the simplification is the staircase: two clusters with a gap, and a percentile that lands in one cluster or the other.

Interactive

Average latency under a cache

An expectation over two outcomes, and the percentile that ignores it

The mean and the percentile disagreeh·thit + (1 − h)·tmiss, against a rank
hit latencymiss latencythe meanthe quantile

Method

Reporting the latency of a cached path

Compute the mean first because it is one line, then stop trusting it and compute the rank.

1

Write both latencies and the hit rate

t_hit, t_miss and h, with t_miss covering the whole miss path including the write back. Getting t_miss wrong is the largest error available here, because it carries almost all of the tail.

2

Take the weighted average

h·t_hit + (1 − h)·t_miss. Keep h as a fraction and the answer stays exact: (19/20)(1) + (1/20)(40) = 59/20. Sanity-check it against t_hit: a mean far above the hit latency means h is lower than you thought.

3

For a percentile, compare h with q

No arithmetic is needed. h ≥ q gives t_hit; otherwise t_miss. Do this for every percentile anyone quotes to you, because the answer is different for each and there is no interpolation to fall back on.

4

Say which percentiles moved and which did not

A cache improvement moves the mean smoothly and moves each percentile once, at one specific hit rate. Reporting “latency improved” without saying which of the two you mean is how a team ships a change that helps the average and leaves every slow request exactly as slow.

Means, ranks and the gap between them

Score 0 / 0

t_hit = 1 ms, t_miss = 40 ms: the mean and the four ranks

One expectation and four comparisons. The comparisons are the part worth slowing down for, because each one has a different answer.

h = 95% = 19/20, t_hit = 1 ms, t_miss = 40 ms mean = (19/20)(1) + (1/20)(40) = 19/20 + 40/20 = 59/20 = 2.95 ms no request takes this ranks: q ≤ h → t_hit, q > h → t_miss p50 q = 1/2 19/20 ≥ 1/2 → 1 ms p95 q = 19/20 19/20 ≥ 19/20 → 1 ms p99 q = 99/100 19/20 < 99/100 → 40 ms p99.9 q = 999/1000 19/20 < 999/1000 → 40 ms now raise h to 99% = 99/100: mean = (99/100)(1) + (1/100)(40) = 139/100 = 1.39 ms p99 99/100 ≥ 99/100 → 1 ms it just flipped p99.9 99/100 < 999/1000 → 40 ms unchanged

The p95 line is the one to look at twice. h = q exactly, and the rule is ≥, so the p95 is the hit latency — the boundary belongs to the hit. One tenth of a point lower and it is 40 ms.

For a faded rehearsal, take t_hit = 2 ms, t_miss = 120 ms and h = 99%. The supplied first move is that the mean is (99/100)(2) + (1/100)(120); finish it, then find the smallest hit rate in tenths of a per cent at which the p99.9 becomes the hit latency, and say what the mean is there. State which percentile you have not moved.

In the lab the staircase is drawn with the four percentile lines across it. Drag the hit rate slowly from 95% upward and watch the mean slide while the p99 sits still, then jumps.

Common mistakes

1
Averaging the two latencies instead of weighting them(1 + 40)/2 = 20.5 ms is the mean of a cache that hits half the time. The weight is the hit rate: (19/20)(1) + (1/20)(40) = 2.95 ms. The unweighted answer is wrong by a factor of seven here, and it gets worse as the cache gets better.
2
“Ninety-nine per cent hit rate, so the p99 is the hit latency”It is, at exactly 99% and above — and the sentence is still misleading, because the p99.9 is the miss latency until the hit rate passes 99.9%. Below 99% the p99 is the miss latency outright. The rule is h ≥ q, applied to each percentile separately, and every quoted percentile needs its own comparison.
3
Expecting the tail to improve graduallyBetween h = 95% and h = 98.9% the p99 does not move by a microsecond, and then it moves by 39 ms in one tenth of a point. A latency graph that shows a flat p99 across a cache improvement is not a broken graph; it is the shape of a two-valued distribution.

Check your understanding

✓
Finish when you compute the mean and the percentile as two different questions.

You should be able to produce the mean latency as a weighted average, decide any percentile by comparing h with q, and say which percentiles a proposed hit-rate improvement will and will not move.

Note: Both lessons so far took the hit rate as given. It is not given — it comes out of how skewed the requests are and how much you are willing to store. “Popularity Is Skewed: Zipf” computes the first of those exactly, and it is the reason a cache holding one per cent of a catalogue is worth building at all.

Kept in this browser only, for you. Nothing is sent anywhere and nothing checks it. Carry it to another device.

Feedback and recommendations

Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.

No recommendations yet for this lesson.