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

Popularity Is Skewed: Zipf

Requests are not spread evenly over keys. Under a Zipf popularity the key of rank i is requested in proportion to 1/iˢ, and the share the top k keys carry is the ratio of two harmonic sums — an exact fraction, computable term by term. That ratio is far larger than k/N, and the gap between the two is the entire case for caching.

p_i ∝ 1/i^s rank 1 is the most popular; s is the Zipf exponent H(n, s) = 1 + 1/2^s + 1/3^s + … + 1/n^s top-k share = H(k, s) / H(N, s) uniform would give k/N N = 50, s = 2: k = 5 → 90.06% of the requests from 10% of the keys k = 1 → 61.53% of the requests from one key in fifty
A popularity law, and the share it gives the head

Core ideas

What this lesson establishes

One assumption about the workload, one sum, and one comparison. The comparison is what the lesson is for: the uniform answer is the misconception, and it is computed here rather than described.

1

Popularity falls with rank, and it falls fast

Sort the keys by how often they are requested. Under a Zipf law the i-th most popular key takes a share proportional to 1/iˢ, so rank 2 gets 1/2ˢ of what rank 1 gets, rank 10 gets 1/10ˢ. At s = 1 that is a tenth; at s = 2 it is a hundredth. s is the Zipf exponent, and it is the only thing on this course called s.

2

The normaliser is a harmonic sum

Probabilities must sum to 1, so p_i = (1/iˢ)/H(N, s) where H(N, s) = Σ 1/iˢ over i from 1 to N — the sigma notation of Algebra, summed exactly. H(20, 1) = 55835135/15519504. Dividing by it is the only step in this lesson that is not addition.

3

The top-k share is a ratio, and it beats k/N

Adding the first k probabilities gives H(k, s)/H(N, s). At N = 50 and s = 2 the top five keys — a tenth of the catalogue — carry 90.06% of the requests, 9.01 times their share of the catalogue. Under uniform popularity they would carry exactly 10%. That multiple is what a cache is buying.

The material

A rank law, a harmonic normaliser, and the share of the head

Where the sum comes from, what it gives at two exponents, and the one figure on this course that is not exact.

Definition · Zipf popularity

Rank N keys from most to least requested. Under a Zipf popularity with exponent s ≥ 0, the key of rank i is requested with probability p_i = (1/iˢ)/H(N, s), where H(N, s) = 1 + 1/2ˢ + 1/3ˢ + … + 1/Nˢ is the generalised harmonic number.

s = 0 makes every key equally likely: H(n, 0) = n and p_i = 1/N. Larger s concentrates the requests on the head. Measured web and key-value workloads usually land between s = 0.7 and s = 1.2; this lesson uses integer exponents, for a reason given below.

Two things are being assumed and both are worth saying out loud. The first is the rank law itself, which is an empirical regularity rather than a theorem — workloads are measured and found to look like this, not derived and found to be this. The second is that the ranking is stable over the window you care about, which is false for news and true for a product catalogue.

Given the law, everything after it is addition. The share of the requests carried by the top k keys is the sum of the first k probabilities, and the normaliser cancels down to a ratio of two sums of the same shape.

k k Σ p_i = (1/H(N,s)) Σ 1/i^s = H(k,s) i=1 i=1 ────── H(N,s) uniform popularity (s = 0): H(k,0)/H(N,0) = k/N
Example · Fifty keys at s = 2

H(5, 2) = 1 + 1/4 + 1/9 + 1/16 + 1/25 = 5269/3600. Dividing by H(50, 2) gives 90.06%: five keys out of fifty carry ninety per cent of the requests. One key on its own — two per cent of the catalogue — carries 61.53%, 30.77 times its share. Under uniform popularity those two numbers would be 10% and 2%.

Why this is the whole case for caching

A cache is worth building when a small store serves a large fraction of the requests, and the ratio H(k, s)/H(N, s) divided by k/N is exactly how much better than proportional you are doing. At N = 50 and s = 2 that multiple is 9.01 for the top five keys and 30.77 for the top one. At s = 0 it is 1 for every k, and a cache holding a tenth of the catalogue serves a tenth of the requests — which is the misconception, arrived at by computing it.

The multiple also shrinks as k grows, and that is the shape that decides how big a cache should be: the first key is worth thirty times its weight, the fiftieth is worth almost nothing. Cache Size and Hit Rate reads a required cache size off exactly this curve.

The one figure on this course that is not exact

H(n, s) is a finite sum of fractions, so it is an exact rational and the lab computes it that way, term by term, over arbitrary-precision integers. What goes wrong is not accuracy but readability: H(50, 2) is a fraction whose numerator and denominator each run to forty-three digits, and a page that prints it has stopped communicating.

So the lab prints the exact fraction while the catalogue has at most fifty keys, and above that it switches to a rounded value, marks the figure with a ~, and says on its own face that it has switched. Drag N to 200 and the top two keys read ~25.865% of the requests — one per cent of the catalogue carrying a quarter of the traffic, 25.87 times its share — with the tilde there to tell you the sum is no longer exact. Every other number on this course is a fraction.

The exponent is an integer here for a related reason: at a fractional s every term 1/iˢ is irrational and there is no exact fraction to print at all. Real workloads have fractional exponents, and the right way to use this lesson on one is to bracket it — compute the share at s = 1 and at s = 2 and report the interval, rather than quote a rounded number with a confidence it has not earned.

Interactive

Popularity is skewed: Zipf

The top-k share as a ratio of harmonic numbers, exact while it is readable

Popularity is skewedpi ∝ 1/is, so the top k carry H(k,s)/H(N,s)
the top kthe tailuniform k/Nrounded

Method

Finding the share the head carries

Rank, sum, divide, and then compare against the uniform answer, because the comparison is the result.

1

Rank the keys and name N and s

Rank 1 is the most requested. N is how many distinct keys the workload touches in the window, not how many rows exist in the database — a table of ten million rows with forty thousand distinct daily readers has N = 40 000.

2

Sum the first k terms

H(k, s) = 1 + 1/2ˢ + … + 1/kˢ, as fractions. At s = 2 and k = 5 that is 1 + 1/4 + 1/9 + 1/16 + 1/25 = 5269/3600. Keep the fractions: the decimals here agree to four places and diverge after.

3

Sum all N terms and divide

H(k, s)/H(N, s) is the share. If N is large enough that the sum is unreadable, the lab will round it and mark it; take the rounding as a signal to report an interval rather than a figure.

4

Divide by k/N and report the multiple

The share on its own sounds impressive and means little. 90.06% from 10% of the keys is 9.01 times proportional, and that multiple is what tells you whether a cache is worth building for this workload.

Skew, sums and shares

Score 0 / 0

N = 50, s = 2: what the top five keys carry

Five terms, one division, and one comparison. The comparison at the end is the only part a reader will remember.

N = 50 keys, s = 2, so p_i ∝ 1/i² H(5, 2) = 1 + 1/4 + 1/9 + 1/16 + 1/25 = 3600/3600 + 900/3600 + 400/3600 + 225/3600 + 144/3600 = 5269/3600 H(50, 2) = 1 + 1/4 + … + 1/2500 43 digits over 43 digits ≈ 1.625133 share = H(5,2)/H(50,2) = 90.06% exact, and unreadable printed uniform would give k/N = 5/50 = 1/10 = 10% multiple = 90.06 / 10 = 9.01× and one key alone: H(1,2)/H(50,2) = 61.53% = 30.77 × (1/50)

The 9.01 is the number to carry away. It says that at this skew a cache is nine times more effective than its size suggests, and that is a statement about the workload, not about the cache.

For a faded rehearsal, take N = 20 and s = 1. The supplied first move is that H(20, 1) = 55835135/15519504; compute the share carried by the top two keys, which is ten per cent of that catalogue, and report it as a multiple of k/N. Then say what the same two numbers become at s = 0, and why.

The lab draws the rank curve with the top k shaded and the cumulative share over it, with the uniform line dashed across for comparison. Set s = 0 and watch the two lines meet: that is the misconception, drawn.

Common mistakes

1
Assuming uniform popularity — “cache 10%, get 10%”This is the default model people carry without noticing they hold it, and it is exactly s = 0. At N = 50 and s = 2 a tenth of the keys carries 90.06% of the requests, not 10%. Any argument that a cache is not worth building because it can only hold a small fraction of the data is this assumption, unstated.
2
Putting the exponent on the wrong thingp_i ∝ 1/iˢ raises the rank to the exponent, so at s = 2 the terms are 1, 1/4, 1/9, 1/16. Raising the whole term instead gives 1, 1/2, 1/3 squared away somewhere else and a harmonic number that is not H(n, s). Check against H(3, 2) = 49/36 before going further.
3
Taking N as the size of the databaseN is the number of distinct keys the workload touches in the window under discussion. A ten-million-row table read by forty thousand distinct keys a day has N = 40 000 for a daily cache, and using ten million instead makes every share look hopeless and every cache look pointless.

Check your understanding

✓
Finish when the uniform answer is something you compute in order to reject it.

You should be able to write p_i for a Zipf workload, sum H(k, s) and H(N, s) exactly at an integer exponent, produce the top-k share as a fraction, and state it as a multiple of k/N.

Note: This lesson reads the share off a chosen k. The question a cache actually poses is the other way round: given a hit rate you want, how many keys must you hold? “Cache Size and Hit Rate” inverts the same ratio, and the answer has a shape — the first nine is cheap and the second is not.

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.