Other eviction policies can win on other traffic
LRU bets that what was used recently will be used again. Three other policies:
- FIFO evicts the oldest write.
- LFU, least frequently used, evicts the entry with the fewest hits.
- Random evicts a key picked at random.
Popularity moves to 1,000 new keys every 10,000 requests. Which policy's hit rate collapses?
Which wins depends on the traffic. Here are hit rates at capacity 100. Each request's key is drawn from 1,000 keys. The key of rank r, counting from 0, is drawn with weight 1/(r + 1), so the most popular key comes up twice as often as the second, and a few keys are very popular:
| 100,000 requests | Same popular keys throughout | Popularity moves every 10,000 |
|---|---|---|
| LRU | 57.6% | 57.6% |
| FIFO | 52.1% | 52.1% |
| LFU | 65.4% | 13.5% |
| Random | 52.1% | 52.0% |
In the first stream, 69.3% of the requests went to the 100 most popular keys. So a cache that held exactly those keys from the start would hit 69.3% of the time. LFU gets closest while popularity holds still.
Then LFU collapses when popularity moves, and LRU's hit rate doesn't move at all. Redis's LFU decays its counts over time for exactly this reason.
The simulation:
import random
from collections import OrderedDict
def hit_rate(policy, requests, capacity, seed=0):
rng = random.Random(seed)
# key -> uses while stored
store, hits = OrderedDict(), 0
for key in requests:
if key in store:
hits += 1
store[key] += 1
if policy == "lru":
store.move_to_end(key)
continue
if len(store) >= capacity:
if policy in ("lru", "fifo"):
store.popitem(last=False)
elif policy == "lfu":
del store[min(store, key=store.get)]
else:
del store[rng.choice(list(store))]
store[key] = 1
return hits / len(requests)
def skewed(n_keys, n_requests, seed=0, offset=0):
rng = random.Random(seed)
weights = [1 / (rank + 1) for rank in range(n_keys)]
return [offset + key for key in rng.choices(range(n_keys), weights, k=n_requests)]
stable = skewed(1_000, 100_000)
shifting = [key for phase in range(10) for key in skewed(1_000, 10_000, seed=phase, offset=phase * 1_000)]
for requests in (stable, shifting):
print([f"{hit_rate(p, requests, 100):.1%}" for p in ("lru", "fifo", "lfu", "random")])['57.6%', '52.1%', '65.4%', '52.1%']
['57.6%', '52.1%', '13.5%', '52.0%']This LFU finds its victim with min, which is O(n) per eviction. An O(1) LFU keeps a list of keys per hit count. Big systems often approximate instead. Redis keeps no exact LRU order: it samples a few keys at random and evicts the ones unused longest. It sweeps expired keys the same way, testing a few random keys with a TTL at a time instead of keeping a heap.
In the editor, write the LFU part of hit_rate as lfu_hits(requests, capacity): count the hits, and evict the key with the fewest uses.
- 1
A stored key is a hit: count it and add one to its uses.
- 2
A new key in a full store evicts the key with the fewest uses,
min(store, key=store.get), then goes in with 1 use.
Run your code to check it. Output shows here.