Vol. I  ·  Pattern §1 ← back to cover MMXXVI
Pattern · Reference

Caching Strategies

Where you put the cache and how you write to it changes correctness, latency, and failure modes — not just speed.

i. The Four Strategies

1. Cache-Aside (Lazy Loading)

App → check cache → hit: return
                  → miss: read DB → write to cache → return

2. Write-Through

App → write to cache AND DB simultaneously

3. Write-Behind (Write-Back)

App → write to cache → return immediately
Cache → async flush to DB (batched)

4. Read-Through

App → check cache → miss: cache itself fetches from DB → returns to app

ii. Cache Stampede (Thundering Herd)

Problem: A popular cached item expires. 10,000 simultaneous requests all miss → all hit DB simultaneously.

SolutionHowTrade-off
Mutex / lockFirst thread to miss acquires lock, fetches DB, populates cache; others waitSerializes misses; adds latency on expiry
Probabilistic early recomputeBefore TTL expires, randomly recompute based on proximity to expirySmall chance of unnecessary recompute; no lock needed
Stale-while-revalidateReturn stale value immediately; recompute in backgroundBrief stale data acceptable; zero extra latency
Jitter on TTLRandomize TTL (base_ttl ± jitter) to spread expiryPrevents synchronized mass expiry

iii. Eviction Policies

PolicyBehaviorBest for
LRUEvict item not accessed longestGeneral-purpose; temporal locality
LFUEvict item accessed fewest timesStable hot-set (celebrity profiles, trending pages)
TTLEvict after fixed time regardless of accessData freshness requirements (prices, inventory)
FIFOEvict oldest inserted itemSimple; rarely best choice
RandomEvict random itemSurprisingly effective at scale; avoids LRU's worst cases

Redis default: LRU (approximate algorithm for performance). Can configure LFU in Redis 4+.

iv. Redis vs Memcached

RedisMemcached
Data structuresStrings, Lists, Sets, Sorted Sets, Hashes, StreamsStrings only
PersistenceRDB snapshots + AOFNone
ReplicationBuilt-in leader-followerExternal (e.g., mcrouter)
ClusteringRedis Cluster (built-in)Client-side sharding only
Pub/SubYesNo
Multi-threadingSingle-threaded (I/O multiplexed)Multi-threaded

v. CDN Caching

Push vs Pull

Push CDNPull CDN
HowYou upload content to CDN proactivelyCDN fetches from origin on first miss
Best forKnown-static content (JS, CSS, videos)Dynamic-ish content (user avatars, thumbnails)
Stale riskLow (you control upload)Higher (TTL-based expiry)
ComplexityHigher (must manage uploads)Lower

Cache Invalidation

vi. Key Numbers

TierLatency
Redis GET (same DC)~0.1 ms
Memcached GET (same DC)~0.05 ms
DB read (Postgres, indexed)~1–5 ms
DB read (Cassandra)~1–3 ms
CDN edge cache hit~5–20 ms
CDN miss (origin fetch)~50–200 ms

vii. Key Points