9. Cache eviction algorithms
One-liner. Every cache is an eviction policy plus a data structure — and scans are the enemy.
Memorize
- LRU: exact recency, needs a hash map + doubly linked list; expensive under high concurrency.
- CLOCK / sampled LRU: approximate recency with bits — what most systems actually ship.
- LFU / TinyLFU: frequency sketch (count-min) + doorkeeper; W-TinyLFU adds a small LRU window to admit new items.
- ARC: adapts between recency and frequency automatically; patented history complicated adoption.
- 2Q: probation + protected queues, scan-resistant.
- S3-FIFO (2023): small/main/ghost FIFO queues, strong hit ratios with low overhead.
- Scan resistance is why pure LRU loses to W-TinyLFU on analytics workloads.
Trap. Using LRU for a workload with periodic full scans — the scan evicts everything useful. Do this. Choose scan-resistant policies (W-TinyLFU, 2Q, S3-FIFO) for mixed workloads.
Check
Why does a full table scan hurt an LRU cache so much?
Why
LRU keeps what was used recently; a scan makes everything recent and nothing reused.