Skip to main content
PreparationZone Logo
PrepZone

Eviction, TTL, and Cache Stampede

LRU policies, time-to-live, and preventing thundering herd on hot keys.

Pass checkpoint below

Read these first

Why this matters

  • VaultCommerce's homepage hero product key expiring at noon once took Postgres from 40% to 98% CPU in 30 seconds — a textbook stampede during a flash sale.
  • LRU sounds simple until you realise one large key evicts thousands of small session tokens.
  • Interviews ask how you'd protect a hot key beyond "add a TTL."
App
RedisCache
PostgresSource of truth
Redis
App checks cache first. On miss, reads DB and populates cache.

Eviction policies

Common eviction algorithms

  • LRU (Least Recently Used) — evict the key untouched longest; Redis default for maxmemory-policy allkeys-lru.
  • LFU (Least Frequently Used) — evict rarely accessed keys; better when access is bursty.
  • TTL-based — expire keys on schedule regardless of access frequency.
  • Random — simple but unpredictable; rare in production caches.
Java
CONFIG SET maxmemory 4gb
CONFIG SET maxmemory-policy allkeys-lru

VaultCommerce runs Redis with allkeys-lru and monitors evicted_keys — a sustained spike means memory is undersized or keys are too large.

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 3 sections

  • TTL design
  • Cache stampede mechanics
  • Stampede prevention techniques

Test yourself

Answer these before moving on — recall is what makes it stick.