Cache eviction lab
Zipf-distributed requests hit a fixed-size cache. Compare FIFO, LRU, LFU and a TinyLFU-style admission policy, then move the hot keys mid-run to see which policies adapt.
hit rate = hits / requests= 166 / 400 = 41.5%hot keys have not moved yet
| Series | Latest | Lowest | Highest |
|---|---|---|---|
| LRU | 42% | 36% | 43% |
Keyspace, 0 to 999
The shaded band is the current hot set. Tall orange marks are cached hot keys. Short grey marks are cached keys that went cold.
Cache contents, next eviction candidate first for FIFO and LRU
- 8541
- 5801
- 4841
- 3491
- 181
- 223
- 7971
- 1511
- 961
- 4271
- 2031
- 951
- 95
- 701
- 752
- 9711
- 921
- 8751
- 332
- 291
- 2041
- 6041
- 6731
- 1701
- 551
- 1151
- 1761
- 52
- 83
- 171
- 214
- 3521
- 192
- 2581
- 581
- 1861
- 841
- 216
- 6071
- 32
- 2191
- 9131
- 2311
- 6331
- 44
- 059
- 128
- 281
- 381
- 531
Each cell is a key and its hit count. P marks the protected segment in TinyLFU + SLRU.
- requests
- 400
- hit rate
- 41.5%
- recent hit rate
- 41.5%
- cache still hot
- 34.0%
| Policy | Overall | Recent | Cache share that is hot |
|---|---|---|---|
| LRU | 41.5% | 41.5% | 34.0% |
What to try
Each one sets up a starting point. Then make the change it describes.
FIFO against LRU
Compare all policies with the hot-key move switched off. Look at FIFO and LRU.
Look for. FIFO throws out a popular key just because it was inserted early. LRU keeps keys that keep getting hit.
Watch LFU go stale
Compare all policies with the hot keys moving at request 6000. After the move, watch the LFU line and the share of its cache that is still hot.
Look for. LFU keeps the old favourites because their counts are high, so the new hot keys cannot get in. LRU forgets quickly and recovers.
Skew decides everything
Set the skew to 0 and then to 1.4 with one policy.
Look for. With uniform requests no policy can beat the ratio of cache size to keyspace. With heavy skew a small cache catches most traffic.
A cache that is too small
Shrink the cache to 10 keys and widen the keyspace to 5000.
Look for. The hit rate falls for every policy. Policy choice only matters once the cache is big enough to hold the hot set.
Move the hot keys by hand
Pick TinyLFU + SLRU and press Move the hot keys now a few times.
Look for. Admission uses a counter sketch that periodically halves its counts, so old popularity fades and new hot keys can win a slot.
New to the idea? Read the lesson first. Every number here comes from a seeded simulator, so the same seed gives the same run.
Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms