Skip to the content.

What is cache eviction?

Eviction = Removing items from cache when cache is full (to make space for new data)

πŸ—‚οΈ Cache = Limited memory β†’ You can’t store everything forever β†’ Must evict less useful data


Common Cache Eviction Policies (with small real-world analogy)

Policy Meaning Real-life analogy
LRU (Least Recently Used) Remove the item not accessed for longest time Bookshelf β€” remove the book you haven’t touched in months
LFU (Least Frequently Used) Remove item with least # of accesses TV shows β€” delete show you watched only once
FIFO (First In First Out) Remove oldest added item First water bottle you put in fridge gets removed first
MRU (Most Recently Used) Remove most recently accessed item (rare) Remove the book you just read (not common)
Random Evict random item Pick any bottle and throw out (used in simple systems)
TTL (Time To Live) Remove item after fixed time expiry Milk carton expires after 3 days

Quick Summary Table (Pros & Cons)

Policy Good when… Pros Cons
LRU Recent items likely reused (web pages) Intuitive, widely used Slightly more metadata needed
LFU Items with high re-use freq matter (config values) Captures frequency Slow to adapt to recent trends
FIFO Very simple cases Easy to implement Doesn’t consider usage
MRU Access pattern flips fast (rare) Niche cases Rarely useful
Random Small/simple cache Ultra fast Unpredictable
TTL Data expires naturally (weather, prices) Auto refresh Can expire useful data

Where these are used (Real systems)

System Policy used
Redis LRU, LFU, TTL (all supported)
CDN (Cloudflare) TTL + LRU
Browser cache LRU
Memcached LRU

In short (interview answer)

Cache eviction = free up space by removing items Common policies: LRU (most used), LFU, FIFO, TTL β€” Choose based on access pattern & app needs.


Tiny real example (Redis)

# Set maxmemory & eviction policy (Redis)
CONFIG SET maxmemory 100mb
CONFIG SET maxmemory-policy allkeys-lru

β†’ Redis will evict least recently used keys when 100 MB full


Pro tip for interview

If you’re unsure, default safe answer = LRU (It balances recency and practical use)


Perfect β€” let’s make a clean table of: πŸ”Ή Policy β†’ Use case β†’ Why So in real system design interviews, you know exactly where to apply each πŸ‘‡


Cache eviction policies β€” where to apply in real systems

Policy Best used in… Why it works well
LRU (Least Recently Used) Web page caching (CDN, browser)
API response cache (Redis, Memcached)
Users often revisit recent pages
β†’ Old pages become less useful
LFU (Least Frequently Used) Config values cache
Dictionary lookup (common terms)
Some data is repeatedly used a lot
β†’ Keep items with high reuse
FIFO (First In First Out) Streaming apps (Video chunk buffer)
Batch processing queues
You naturally want oldest data out first
β†’ Process in arrival order
MRU (Most Recently Used) (rare) Undo history (Text editor)
Session cleanup (if user comes back soon)
When recent data is least useful
β†’ Unusual, niche cases
Random Very small caches (IoT devices)
Simple embedded systems
Hardware/resource constrained
β†’ Simpler, faster eviction logic
TTL (Time To Live) Weather data API
Stock prices cache
Product catalog (E-commerce)
Data naturally expires/changes
β†’ Expiry ensures freshness

Interview-ready crisp examples (You can say like this)

✨ β€œFor a web app with heavy reads (e.g. e-commerce product pages), I’d use LRU cache in Redis to serve hot products fast β€” as users mostly revisit recent products.”

✨ β€œFor config flags or common lookup tables, LFU makes sense to keep frequently accessed keys longer.”

✨ β€œIf caching weather or stock data, I’d apply TTL of 10 mins so users get fresh info automatically.”


If you want an easy rule of thumb πŸ₯³

Pattern Policy
Recent data reused? LRU
Frequently reused data? LFU
Data expires naturally? TTL
Stream / Queue? FIFO
Tiny embedded system? Random

Quick tip (interview) If unsure β†’ LRU is almost always a safe answer for general purpose caches

β€œUnder the hood, how does a cache implement LRU/LFU etc.?”


πŸš€ First β€” High level

Eviction policy (LRU, LFU etc.) = Strategy

Algorithm + Data structures = How to efficiently implement it


LRU (Least Recently Used) β€” Common algorithm

🎯 Goal: Quickly find and evict least recently accessed item

βš™οΈ Typical algorithm:

➑️ Use HashMap + Doubly Linked List

Data structure Purpose
HashMap (key β†’ node) Fast O(1) lookup
Doubly linked list Maintains recency order

πŸ› οΈ How it works

Time complexity

βœ… get() β†’ O(1) βœ… put() β†’ O(1)

πŸ“Œ Example (Java-ish)

class LRUCache {
  Map<Key, Node> map;
  DoublyLinkedList list;
}

LFU (Least Frequently Used) β€” Common algorithm

🎯 Goal: Evict least frequently accessed item

βš™οΈ Typical algorithm:

➑️ Use HashMap + Frequency List

Data structure Purpose
HashMap (key β†’ node) O(1) lookup
FreqMap (freq β†’ linked list of keys) Group keys by freq
Track minFreq Find LFU key fast

πŸ› οΈ How it works

Time complexity

βœ… get() β†’ O(1) βœ… put() β†’ O(1)


πŸŸ₯ Redis β€” What algorithm does Redis use?

Redis supports multiple eviction policies β€” here’s how Redis implements them:

Redis Policy Redis Algorithm
allkeys-lru / volatile-lru Approximate LRU using LRU clock + sampling
allkeys-lfu Approximate LFU with 8-bit counters + decay
volatile-ttl Evict keys expiring soonest (based on TTL)
noeviction Just error on memory full

πŸ” Why Redis uses Approximate LRU/LFU (not exact)?

πŸ“Œ Approx LRU example

β†’ Fast & scalable, especially for millions of keys


Redis LFU β€” How does it work?


In short (interview ready)

Cache design = eviction policy + fast data structure


Pro tip (interview)

✨ β€œRedis trades perfect LRU/LFU accuracy for speed and simplicity β€” using sampling and approximate counters.”