Visualize MRU cache replacement and see how evicting the most recently used item outperforms LRU on cyclic workloads.
About this algorithm
An MRU cache evicts the item that was used most recently. That sounds backwards, but it is the right rule for one common pattern: a loop that reads more data than the cache can hold, over and over. In that loop the page you just finished with is the one you will not need again for the longest time.
Reach for MRU when the access pattern is a repeated sequential scan larger than the cache, such as a database nested-loop join reading the same table again for every outer row. LRU fails badly there: each new page pushes out the oldest one, which is exactly the one the next pass needs first, so every request misses. For ordinary traffic, where recent items come back soon, use LRU or Clock; LFU suits long-lived popularity, and FIFO is the cheapest rule when nothing much repeats. MRU on ordinary traffic throws away the hottest item.
Keep the keys ordered by last use, most recent first, the same structure LRU uses: a hash map from key to node and a doubly linked list.
The key just used becomes the most recent one, exactly as in LRU.
Here MRU parts ways with LRU. Instead of removing the tail, it removes the head: the most recently used key.
The requested key goes in as the newest. If the next request is also a miss, this key is the one that will be evicted.
The page requests 1 2 3 4 1 2 5 1 2 3 4 5 into four slots, most recent on the left. Four misses fill it: 4 3 2 1. Then 1 and 2 hit, leaving 2 at the front. When 5 misses, MRU evicts 2, the key used one step ago, and the cache reads 5 1 4 3. Request 1 hits. Request 2 misses and evicts 1, the most recent, giving 2 5 4 3. Now 3, 4 and 5 are all still in the cache, because MRU kept evicting from the front and left the older keys alone, and all three hit. The run ends with 6 hits out of 12, a 50% hit rate, the best of the five policies on this sequence. LRU gets 4 hits and FIFO 2.
Picture a cache of 4 pages and a loop reading pages 1 to 5 forever. LRU evicts 1 to load 5, then 1 is needed and evicts 2, and so on: it misses every time. MRU evicts 4 to load 5, so 1, 2 and 3 survive into the next pass and hit. The longer the loop runs, the bigger the gap.
Time: the map finds the node and the linked list moves or removes it at the head in constant time, the same costs as LRU. Space: one map entry and one list node for each cached key.