When your cache is full and a new item needs space, something must go — the eviction policy you choose determines whether you keep the most valuable items or throw them away randomly.
Scenario: You manage a parking lot with 10 spaces (cache). All spaces are full. A new car arrives (new data). Which car do you remove to make space?
Option A: Remove the car that's been parked longest (first in, first out)
Option B: Remove the car that hasn't moved in the longest time (least recently used)
Option C: Remove the car that moves least frequently (least frequently used)
Option D: Remove a random car
Question: Which strategy makes the most sense for keeping the lot efficient?
When cache is full, we need rules for what to remove. Let's explore each!
Rule: Remove the item that hasn't been accessed in the longest time.
Mental model: If you haven't used it recently, you probably won't need it soon.
Visual example:

LRU Implementation:
When LFU works best:
✅ Content popularity matters (viral videos)
✅ Long-term access patterns
✅ Identifying "hot" data
✅ Media streaming platforms
Examples:
YouTube video cache (popular videos stay)
Spotify song cache
News article cache (trending stories)
LFU Problem: The "Aging" Issue
Problem: Old item: 1000 accesses (from 2 years ago) New item: 5 accesses (from today, trending!)
LFU keeps old item, evicts new trending item! ❌
Solution: Use "Time-aware LFU" with decay
Reduce frequency counts over time
Rule: Remove the oldest item (first added), regardless of usage.
Mental model: Like a queue at a store - first person in line leaves first.
Visual example:

FIFO Implementation:
When FIFO works best:
✅ Simple requirements
✅ Time-based expiration matters
✅ All data equally likely to be accessed
✅ Predictable access patterns
Examples:
FIFO Problems:
❌ Doesn't consider access patterns
❌ May evict frequently used items
❌ No locality optimization
❌ Generally worse hit ratio than LRU/LFU
General performance (typical workloads):

LRU wins most scenarios! ✓
Complexity:
| Policy | Get Complexity | Set Complexity | Implementation |
|---|---|---|---|
| LRU | O(1) | O(1) | Moderate (doubly-linked list + hash map) |
| LFU | O(1)* | O(log n)* | Complex (multiple data structures) |
| FIFO | O(1) | O(1) | Simple (queue + hash map) |
Choose LRU when:
✓ General-purpose caching
✓ Temporal locality important
✓ Simple to implement
✓ Good performance for most workloads
Choose LFU when:
✓ Popularity matters (hot content)
✓ Long-term patterns
✓ Willing to handle complexity
✓ Media/content streaming
Choose FIFO when:
✓ Simplicity is critical
✓ Time-based expiration
✓ All items equally likely to be accessed
✓ Learning/prototyping
Most common choice: LRU (best balance of performance and simplicity!)