Try
↑ ↓ move↵ openesc close

Interview prep / Caching

Junior · Caching · System design interview question

How do LRU and FIFO eviction differ, and why is LRU usually better?

Sketch your own answer first, then compare it with the one below.

A strong answer

Both evict one key when the cache is full. FIFO evicts the key that was inserted earliest. LRU evicts the key that was used least recently.

The difference shows up on a hit. In LRU a hit moves the key to the most recent end, so keys that keep getting requested stay. In FIFO a hit changes nothing, so a popular key is eventually evicted just because it arrived early, and then immediately missed again.

Real request streams show temporal locality: a key used recently is likely to be used again soon. LRU bets on that and FIFO ignores it. Hence LRU usually wins on skewed workloads.

LRU pays for reordering on every hit, which takes work and, in a concurrent cache, coordination. FIFO is simpler and cheaper. LRU also has a weakness of its own: a long scan over cold keys pushes out the hot ones.

Sources

Learn it by running it

The caching and eviction lesson explains the mechanism behind this question, and its lab lets you change the parameters and watch the effect.

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