Quick reference for cache layers, patterns, eviction policies, stampede fixes, and Redis vs Memcached.
RedisLRUInvalidationStampede
Core Formulas
hit_ratio = hits / (hits + misses)
miss_ratio = 1 - hit_ratio
avg_latency = hit_ratio*cache_lat + (1-hit_ratio)*origin_lat
origin_load = total_rps * miss_ratio # capacity-plan for THIS
Ex: cache 1ms, origin 40ms
0.90 -> 4.9ms | 0.99 -> 1.4ms
50k rps @ 95% hit -> origin sees 2.5k rps
Where to Cache
| Layer | Latency | Note |
| Browser | 0 | Cache-Control, ETag; no remote purge |
| CDN/edge | ~10ms | Static + cacheable API; purge lag |
| App-local | ~100ns | Per-instance, not coherent |
| Redis | ~0.5ms | Shared, coherent, network hop |
| DB buffer | n/a | Transparent, not yours to control |
Patterns
| Pattern | Behavior | Consistency |
| Cache-aside | App loads on miss | Eventual |
| Read-through | Cache lib loads on miss | Eventual |
| Write-through | Write cache+DB sync | Strong |
| Write-behind | Cache now, DB async | Loss risk |
| Refresh-ahead | Reload before expiry | Eventual |
v = cache.get(k)
if v is None:
v = db.read(k)
cache.set(k, v, ttl=300) # always set a TTL
return v
# on write: db.write(); cache.delete(k) # invalidate after commit
Eviction Policies
| Policy | Evicts |
| LRU | Least recently used (default) |
| LFU | Least frequently used; scan-resistant |
| FIFO | Oldest inserted |
| TTL | Expired entries |
| Random | Random key; cheapest |
Redis: allkeys-lru, volatile-lfu. Caffeine uses W-TinyLFU (beats LRU).
Failure Modes & Fixes
| Problem | Fix |
| Stampede (herd) | Single-flight, per-key lock, jittered TTL, early refresh |
| Penetration (missing keys) | Cache null sentinel + Bloom filter |
| Hot key | Replicate key#1..N, local cache tier |
| Stale (delete race) | Delete after commit + short TTL, double-delete |
| Avalanche (mass expiry) | Randomize TTLs across keys |
Distributed & Engines
Routing
hash(key) % N -> add node remaps ~all keys (bad)
consistent hashing -> ring; add/remove moves ~1/N keys
+ virtual nodes to balance load
Redis Cluster -> CRC16(key) % 16384 slots
Redis vs Memcached
| Redis | Memcached |
| Types | Rich structures | Strings only |
| Persist | RDB+AOF | None |
| Threads | Single core | Multi |
| HA | Sentinel/Cluster | Client-side |