Eviction Policies: LRU, LFU, FIFO & TTL
Cache memory is finite. When the cache is full and a new entry arrives, something must be removed — that decision is the eviction policy. The right policy depends on your access patterns.
LRU (Least Recently Used) evicts the entry that has not been accessed for the longest time. It works well for most workloads because recently used data tends to be used again — temporal locality. LFU (Least Frequently Used) evicts the entry with the fewest accesses over its lifetime, better when some items are consistently popular. FIFO evicts the oldest inserted entry regardless of usage — simple but often suboptimal. Random eviction picks any entry arbitrarily; surprisingly effective and zero bookkeeping overhead. TTL (time-to-live) expiry removes entries after a fixed duration regardless of usage — essential for data that becomes stale on a schedule.
Check your understanding
What does LRU evict?Show answerHide answer
Answer
The entry unused for the longest time — good when temporal locality holds.When might LFU beat LRU?Show answerHide answer
Answer
When a few keys stay popular over long periods while recent one-offs should not stick.Why combine LRU with TTL?Show answerHide answer
Answer
Capacity eviction handles memory; TTL bounds staleness even for frequently hit keys.