Learn
Lesson · Caching and Hit Rates · Skew, size and policy

Cache Size and Hit Rate

Holding the C most popular keys gives a hit rate of H(C, s)/H(N, s), the same ratio the previous lesson computed, read as a function of C. It is a curve that rises steeply and then flattens, so the cache size a target requires is read off it rather than scaled: at N = 50 and s = 2, ninety per cent costs five keys and ninety-nine costs twenty-eight.

h(C) = H(C, s) / H(N, s) cache the top C of N keys N = 50, s = 2 C = 1 → 61.533% C = 5 → 90.061% 90% costs 5 keys C = 10 → 95.363% C = 28 → 99.060% 99% costs 28 keys C = 47 → 99.923% 99.9% costs 47 keys 5 → 28 keys is 5.6× the cache for one more nine
The hit-rate curve, and what each nine costs on it

Core ideas

What this lesson establishes

The formula is the previous lesson’s, with k renamed C and read backwards. What is new is the shape of the curve and what the shape implies.

1

A cache of the top C keys hits H(C, s)/H(N, s)

The idealisation is that the cache holds exactly the C most popular keys and never anything else. Then a request hits when its key is in the top C, which happens with probability H(C, s)/H(N, s). Real caches approximate this badly or well depending on the policy, which is Replacement Policies on a Trace; this lesson is the ceiling they aim at.

2

The curve rises steeply and then flattens

At N = 50 and s = 2 the first key alone gives 61.533%, five keys give 90.061%, twenty-eight give 99.060% and forty-seven give 99.923%. The marginal value of a key falls with its rank because its probability does, so each further nine costs several times the cache of the one before.

3

Hit rate is not proportional to cache size

Doubling a cache does not double its hit rate and cannot: the hit rate is bounded by 1 and the curve is concave. Going from 90% to 99% here takes the cache from 5 keys to 28, which is 5.6 times the memory for 9 percentage points — and, by Hit Rate and Backend Load, a tenfold cut in backend load, which is what you were buying.

The material

The hit-rate curve, the size a target costs, and where the returns go

One formula read as a function of C, the scan that inverts it, and what this replaces from Capacity Estimation.

Definition · Hit rate as a function of cache size

For a Zipf workload over N keys with exponent s, a cache holding the C most popular keys has hit rate h(C) = H(C, s)/H(N, s), for C from 0 to N.

h(0) = 0 and h(N) = 1. Between them h is strictly increasing and concave: each additional key adds p_{C+1}, and the probabilities are decreasing, so the increments shrink.

That is the whole model, and the assumption inside it is worth naming: the cache holds the right C keys. No online policy knows which those are, so h(C) is an upper bound on what a cache of C slots achieves on this workload, reached only if the popularity ranking is stable and the policy is good.

Inverting the curve: the C a target costs

To find the smallest C reaching a target h, add probabilities in rank order until the running total crosses it. No formula is needed and none exists in closed form for integer C; the lab runs the scan over exact fractions, so the C it reports is the smallest one, not the smallest one up to rounding.

N = 50, s = 2 running total of p_i, in rank order C h(C) target reached 1 61.533% 5 90.061% ← 90% costs 5 keys, a tenth of the catalogue 10 95.363% ← 95% costs 10 28 99.060% ← 99% costs 28, more than half the catalogue 47 99.923% ← 99.9% costs 47
Example · What the second nine costs

From 90% to 99% the cache goes from 5 keys to 28: twenty-three more keys, 5.6 times the memory. From 99% to 99.9% it goes from 28 to 47, and the catalogue only has fifty keys in it — past that point you are not caching, you are copying.

Put the two courses together and the trade is visible. Five keys of cache cut the backend load by a factor of ten (from 100% to 10% of λ); twenty-eight keys cut it by a hundred. The second factor of ten cost 5.6 times the memory of the first. Whether that is a good deal depends entirely on what the backend was doing at the higher load, which is the ρ of Hit Rate and Backend Load.

This replaces a number you were previously handed

“Memory and the Working Set” sized a cache by stating a hot fraction — assume ten per cent of the data is hot, multiply by the row size, buy that much memory. That is a serviceable estimate and it is an assumption. Here the hot fraction is a result: it comes out of N and s, and at N = 50 and s = 2 the ten per cent that lesson assumed turns out to deliver 90.061%, which is either lucky or a coincidence depending on the workload.

So the honest procedure is the other way round from the one that course taught: measure the skew, choose the hit rate you need from what the backend can take, and let the curve tell you the cache size. If the skew is unknown, bracket it — compute the required C at s = 1 and at s = 2 and carry both numbers, which is the error bar Capacity Estimation asked you never to drop.

One caution about the flattening. The curve says the last nine is expensive in memory; it does not say the last nine is worthless. Whether it is worth buying is a question about the backend, and there the same nine is a tenfold cut in load every time. Read the two curves together or you will talk yourself out of a cache that was about to save a database.

Interactive

Cache size and hit rate

Read the C off the curve, and see what the next nine costs

Cache size against hit rateh(C) = H(C,s)/H(N,s), and the C a target costs
h(C)the targetthe C that reaches itthe next nine

Method

Sizing a cache from a target hit rate

The target comes from the backend, not from the cache. Everything after that is a scan.

1

Get the target from the backend, not from taste

Decide what backend load the backend can take, divide by λ, and that is your miss rate. A backend that can serve 500 rps under 10 000 rps of traffic needs 1 − h ≤ 1/20, so h ≥ 95%. Choosing 99% because it sounds professional sizes the cache from a slogan.

2

Measure N and s for the window that matters

N is the distinct keys touched, s is the skew. Both are measurements. If you have a rank-frequency sample, fit s roughly by comparing the top key’s share against the table; if you have nothing, bracket s between 1 and 2 and carry both answers.

3

Scan the running total to the target

Add p₁, p₂, p₃, … until the total reaches h. The C at which it crosses is the answer, and it is an integer — there is no interpolating a cache to two and a half keys.

4

Price the next nine before you commit

Compute C for your target and for one nine beyond it. If the second number is a small multiple of the first, buy it now; if it is most of the catalogue, you have found the point where this workload stops rewarding memory and the answer is a different design.

Sizes, targets and diminishing returns

Score 0 / 0

N = 50, s = 2: the cache for 90% and the cache for 99%

The scan is a running sum over exact fractions. Only the crossings are written out here; the lab shows every step.

N = 50, s = 2. H(50,2) ≈ 1.625133. h(C) = H(C,2)/H(50,2) C = 1 H(1,2) = 1 h = 61.533% C = 2 + 1/4 h = 76.917% C = 3 + 1/9 h = 83.754% C = 4 + 1/16 h = 87.600% C = 5 + 1/25 = 5269/3600 h = 90.061% ← first ≥ 90% … C = 28 h = 99.060% ← first ≥ 99% for 90%: C = 5 = 10% of the catalogue for 99%: C = 28 = 56% of the catalogue cost of the extra nine: 28 − 5 = 23 keys, 28/5 = 5.6× the cache what it buys, by Hit Rate and Backend Load: backend load 1/10 → 1/100 of λ

Five keys reach ninety per cent and twenty-eight reach ninety-nine. Notice that the second figure is more than half the catalogue: on a workload this small, the last nine means keeping most of the data, and the word “cache” has stopped being accurate.

For a faded rehearsal, keep N = 50 and set s = 1. The supplied first move is that the head is much flatter, so every target costs more; find the C for 90% and for 99% at that skew and state the ratio between them. Then say, in one sentence, what the two skews together tell you about how confident you can be in a cache size when s is only known to within a point.

The lab draws the curve with the target line across it and the required C marked, and the red bar under the axis is the distance between the ninety and the ninety-nine. Move the skew slider and watch that bar grow.

Common mistakes

1
Scaling the hit rate with the cache size“We have a 90% hit rate on 5 keys, so 10 keys would give us 180%” is obviously wrong, and the same reasoning with smaller numbers is not. h is concave and bounded: at N = 50 and s = 2, 10 keys give 95.363%, not twice 90.061%.
2
Sizing the cache before choosing the targetThe target hit rate is a statement about what the backend can absorb, and it comes from λ and μ. Picking a cache size first and reporting whatever hit rate it happens to give is how a team ends up defending a number they did not choose.
3
Treating the assumed hot fraction as a measurementThe ten-per-cent working set of “Memory and the Working Set” is an assumption that makes an estimate possible, not a property of your workload. Here it is a consequence of N and s, and at s = 1 the same ten per cent of fifty keys delivers far less than it does at s = 2. If the skew is unknown, carry both answers.

Check your understanding

✓
Finish when you size a cache from a target and price the nine beyond it without being asked.

You should be able to compute h(C) for a given C, scan to the smallest C reaching a target hit rate, express that C as a share of the catalogue, and state what the next nine costs in keys and buys in backend load.

Note: Both of the last two lessons assumed the cache holds the most popular keys. Nothing running in production knows which those are. “Replacement Policies on a Trace” counts what four policies actually achieve on a reference string, with the offline optimum as the ceiling — and finds a trace where a bigger cache makes one of them worse.

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.