← Back to System Design, page 1

system-design-0009 · Easy fresher

What are cache eviction policies such as LRU, LFU, FIFO and TTL-based expiry, and how do you choose one?

An eviction policy decides which entry to remove when the cache is full. LRU removes the least recently used item, LFU the least frequently used, FIFO the oldest inserted. TTL expiry is different: it removes items after a set time even if there is space. LRU is the usual default; LFU helps when a stable set of items stays popular.

Full answer

Eviction versus expiry

  • Eviction happens because memory is full. The cache must pick a victim.
  • Expiry (TTL) happens because an item is too old to trust, whether or not memory is full.

Most production caches use both: every key has a TTL, and an eviction policy handles memory pressure.

Common policies

Imagine a library desk that can hold only 5 books for quick lending.

  • LRU (least recently used): remove the book nobody has asked for in the longest time. Works well when recent use predicts near-future use, which is true for most web traffic. Implemented with a hash map plus a doubly linked list so get and put are O(1).
  • LFU (least frequently used): remove the book with the fewest total requests. Good when popularity is stable, such as a fixed set of top courses. Plain LFU can keep an item that was popular last month forever, so real versions decay counts over time.
  • FIFO: remove the book that arrived first. Simple, but it may throw out a book that everyone still borrows.
  • Random: remove any entry. Cheap and surprisingly reasonable at large sizes.

What real systems do

Redis has a maxmemory-policy setting, with options such as allkeys-lru, allkeys-lfu, volatile-lru (only keys with a TTL) and noeviction (reject writes when full). Redis uses an approximate LRU or LFU that samples a few keys rather than keeping a perfect ordering, which saves memory.

Choosing

  • General web cache: LRU plus TTL.
  • Strongly skewed, stable popularity: LFU.
  • Cache used as a store of must-keep data (sessions): noeviction or volatile-* with care, and enough memory, because silent eviction would log users out.
  • Scan-heavy workloads (a nightly job reading every row once) can flush a plain LRU cache; keep them away from the shared cache.

By level

Fresher: Define LRU, LFU, FIFO and TTL with a simple example, and say that LRU is the usual default. Separating eviction from expiry is the main thing to get right.

Mid: Explain the O(1) LRU structure, Redis policies, approximate LRU, and how to pick a policy from access patterns and hit ratio. Expect knowledge of the real Redis policy names.

Senior: Discuss scan resistance (segmented LRU, TinyLFU style admission), memory fragmentation, sizing for target hit ratio, and the impact of eviction on downstream database load. The answer should link eviction choices to hit ratio and database load.

Key points

  • Eviction is triggered by memory pressure; expiry by age
  • LRU is the common default and is O(1) with a hash map plus linked list
  • LFU suits stable popularity but needs decay
  • Redis offers allkeys-lru, allkeys-lfu, volatile-* and noeviction
  • Large one-time scans can pollute an LRU cache

Common mistakes

  • Confusing TTL expiry with eviction
  • Assuming Redis keeps exact LRU order
  • Using an evicting cache for data that must not disappear without planning for it
  • Forgetting that LFU without decay keeps stale favourites

Follow-ups

  • How would you implement an LRU cache with O(1) operations?
  • What happens in Redis with noeviction when memory is full?
  • How would you protect a cache from a full-table scan?

Tips

  • Separate eviction from expiry in the first sentence
  • Use one simple analogy and then the real Redis settings
  • Mention the data structure behind LRU

Evaluation rubric

  • Correct definitions of the policies
  • Distinguishes eviction from TTL
  • Gives selection guidance tied to workload
  • Mentions at least one real implementation detail