# Caches: LRU v. random

DevFeed: [Caches: LRU v. random](<https://devfeed.tech/articles/caches-lru-v-random-77642.md>)

Original publisher: [Read original article](<https://danluu.com/2choices-eviction/>)

Published: 2014-11-03T00:00:00Z

Content type: article

Language: en

Sources: [Danluu](<https://devfeed.tech/sources/danluu.md>)

Topics: [CPU Cache](<https://devfeed.tech/topics/cpu-cache.md>), [Cache Coherence](<https://devfeed.tech/topics/cache-coherence.md>), [NUMA](<https://devfeed.tech/topics/numa.md>), [Algorithms](<https://devfeed.tech/topics/algorithms.md>)

Tags: [algorithm](<https://devfeed.tech/tags/algorithm.md>), [benchmark](<https://devfeed.tech/tags/benchmark.md>), [cache](<https://devfeed.tech/tags/cache.md>), [cpu](<https://devfeed.tech/tags/cpu.md>), [cpu-caches](<https://devfeed.tech/tags/cpu-caches.md>), [l3-cache](<https://devfeed.tech/tags/l3-cache.md>), [lru](<https://devfeed.tech/tags/lru.md>)

## AI overview

The article compares LRU, random, and multi-choice random cache eviction policies using SPEC CPU benchmarks across cache sizes and levels. LRU performs better in smaller caches, while 2-random and pseudo 3-random can outperform it in larger caches; the results also examine associativity and multi-level cache effects.

## Source excerpt

Once upon a time, my computer architecture professor mentioned that using a random eviction policy for caches really isn't so bad. That random eviction isn't bad can be surprising -- if your cache fills up and you have to get rid of something, choosing the least recently used (LRU) is an obvious choice, since you're more likely to use something if you've used it recently. If you have a tight loop, LRU is going to be perfect as long as the loop fits in cache, but it's going to cause a miss every time if the loop doesn't fit. A random eviction policy degrades gracefully as the loop gets too big. In practice, on real workloads, random tends to do worse than other algorithms. But what if we take two random choices (2-random) and just use LRU between those two choices? Here are the relative miss rates we get for SPEC CPU1 with a Sandy Bridge-like cache (8-way associative, 64k, 256k, and 2MB L1, L2, and L3 caches, respectively). These are ratios (algorithm miss rate : random miss rate); lower is better. Each cache uses the same policy at all levels of the cache. Policy L1 (64k) L2 (256k) L3 (2MB) 2-random 0.91 0.93 0.95 FIFO 0.96 0.97 1.02 LRU 0.90 0.90 0.97 random 1.00 1.00 1.00 Random and FIFO are both strictly worse than either LRU or 2-random. LRU and 2-random are pretty close, with LRU edging out 2-random for the smaller caches and 2-random edging out LRU for the larger caches. To see if anything odd is going on in any individual benchmark, we can look at the raw results on each sub-benchmark. The L1, L2, and L3 miss rates are all plotted in the same column for each benchmark, below: As we might expect, LRU does worse than 2-random when the miss rates are high, and better when the miss rates are low. At this point, it's not clear if 2-random is beating LRU in L3 cache miss rates because it does better when the caches are large or because it does better because it's the third level in a hierarchical cache. Since a cache line that's being actively used in L1 or L2 isn't