Scaling data / Intermediate / 5 min
Cache eviction policies
LRU, LFU, FIFO and Random race on the same traffic. Scans and shifting trends pick the winner.
Start the simulation → Change the load, break a part, watch what happens. Free, no sign-up.
In short
- What it is
- When a cache is full, an eviction policy picks what to throw out: least recently used (LRU), least frequently used (LFU), FIFO or random.
- Why it matters
- The cache is small, so keeping the items most likely to be asked for again decides the hit rate.
- The trade-off
- LRU suits recent-heavy traffic but one big scan can flush it; LFU keeps steady favourites but is slow to forget old ones.
- Say it in an interview
- "LRU is the default; for a few very popular keys LFU does better, and a scan-resistant policy avoids one-off reads flushing the cache."
Check yourself
A nightly job reads thousands of items once each. Which policy is most likely to lose the hot set?
Answer: LRU. Each scanned item becomes "most recently used" and pushes popular items out. LFU remembers that hot items had many hits.
Popular items change every few minutes. Which policy adapts most slowly?
Answer: LFU. LFU keeps high counts for yesterday’s favourites, so new popular items get evicted first.
What eviction approach does Redis use by default when you set maxmemory with allkeys-lru?
Answer: Approximate LRU by sampling a few keys. Redis samples a handful of keys and evicts the least recently used among them, which is cheap and close to true LRU.