Cache Eviction Policies Lab (Interactive)
Replay accesses and batch-scan bursts against LRU, LFU, FIFO, and TTL on a capacity-limited cache and compare hit ratios. Step through eviction decisions: which key each algorithm sacrifices when RAM fills, and how scan traffic or viral bias defeats it.
Eviction Policy Replay: LRU vs LFU vs FIFO vs TTL
Replay a request trace against a full cache and watch which key each algorithm sacrifices.
maxmemory-policy
› Loaded A–D. Policy: LRU. Access keys or fire a scan burst, then watch whom each policy sacrifices.
How It Works Under the Hood
DRAM is finite: when Redis hits maxmemory, an eviction policy decides which key dies to make room while protecting the hit ratio. LRU (hash map + doubly linked list, O(1)) assumes temporal locality but a one-shot batch scan flushes the hot set; LFU keeps frequency leaders but historical viral keys squat forever without logarithmic decay; FIFO evicts by insertion order; TTL expires on schedule via passive checks plus an active sampler sweeping 20 keys every 100ms. Production Redis uses approximated LRU — sampling 5–10 random keys — to dodge pointer overhead at near-ideal accuracy.
Core Architectural Principles
- True LRU is HashMap + Doubly Linked List giving O(1) get/put; Redis approximates it by random sampling.
- LFU needs decay counters or old viral keys never leave RAM; scan bursts poison plain LRU.
- Redis policies differ: allkeys-lru evicts anything, volatile-lru only TTL-tagged keys, noeviction errors on write.
Expect the O(1) LRU data-structure question in both design and coding rounds: HashMap for lookup, doubly linked list for recency moves. Then show production depth: "Redis approximates LRU by sampling 5–10 keys to avoid pointer memory," and pick allkeys-lru versus allkeys-lfu based on whether popularity is sticky or transient — warning that one-off scans pollute LRU.
Recency-based eviction fits most web traffic but must be hardened against scan pollution, while frequency-based fits static popularity at the cost of stale-favorite bias.