Try
↑ ↓ move↵ openesc close
In this lesson · topic 5 of 7
  1. 1. Why a small cache works at all
  2. 2. FIFO, LRU, LFU, and TinyLFU
  3. 3. Move the hot keys under four policies
  4. 4. Why plain LFU stays stuck after a shift
  5. 5. How big a cache do you need
  6. 6. When a hot key expires
  7. 7. Check the eviction rules

Topic 5

How big a cache do you need

7 min read

Policy comparisons tell you which cache is best at a fixed size. A design question comes first: how large should the cache be? Under the Zipf model you can answer it with a sum.

The ceiling

Imagine a cache that knows the true popularity of every key in advance and never makes a mistake. It holds the C most popular keys and nothing else. Its hit rate is the share of requests that go to those keys. With Zipf skew 1, the key at rank k gets weight 1/k, so the share is the sum of 1/k up to C divided by the sum of 1/k up to the keyspace size N. These sums are harmonic numbers, written H.

For a cache of 50 keys over a keyspace of 1000, H(50) is about 4.50 and H(1000) is about 7.49. The ceiling is 4.50 / 7.49, or 60 percent.

Predict first

A cache holds 50 of 1000 keys and requests are Zipf with skew 1. A cache that knew the popularity in advance would hit about how often?

Compare that ceiling with the lab. Eviction policies work without advance knowledge, and they land below it: LFU and TinyLFU reach 57%measured and 57%measured, LRU 46%measured and FIFO 41%measured. A cache that misses 59 percent of the time sends about 1.5 times the traffic to the database that the perfect cache would, since 59 divided by 40 is about 1.5. That is what a policy choice is worth here.

What a doubling buys

The same sum gives hit rate for any size. Skew 1, keyspace 1000:

Cache keys Ceiling hit rate Miss rate
10 39% 61%
25 51% 49%
50 60% 40%
100 69% 31%
200 79% 21%
500 91% 9%

Each doubling adds about 9 to 10 points. That looks steady, but the cost is not: doubling from 50 to 100 keys costs 50 more slots to cut the miss rate from 40 to 31 percent, and doubling from 200 to 400 costs 200 slots for a similar gain. The harmonic sum grows like the logarithm of C, so every doubling buys the same absolute amount and costs twice as much memory as the one before.

Here the number to watch is the miss rate, because the backing store sees misses. Going from 50 to 100 keys cuts database traffic by about a quarter, from 40 to 31 percent of requests.

Skew moves the ceiling more than size does

Keep the 50-key cache and change only the skew. Summing 1/k^s instead:

Skew Ceiling hit rate
0.5 21%
0.8 42%
1.0 60%
1.4 88%

A workload with skew 0.5 would need a far larger cache than one with skew 1.4 to hit the same rate. Before you size a cache, estimate the skew from a real trace of your requests. Guessing it is the largest error available.

Sources

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms