Cache Strategies: Cross-Chapter Comparison Reference

comparisons cache redis memcached

Type: Cross-chapter reference — NOT a chapter notes file
Covers: Vol 1 Ch01, Ch04, Ch06, Ch08, Ch11, Ch14, Ch15 · Vol 2 Ch05, Ch10, Ch13
Last Updated: 2026-04-13


1. Cache Placement Strategies

Where you place a cache determines latency, control, and consistency trade-offs.

PlacementLocationLatencyWho controls TTLTypical use
Client-sideBrowser, mobile app memorySub-msClientStatic assets, user prefs
CDN / EdgeISP-adjacent PoPs worldwide~5–20ms from userCDN + origin headersImages, JS/CSS, video segments
Load balancerReverse proxy (NGINX, HAProxy)~1msProxy configSession affinity, small static pages
App-level cacheIn-process memory (guava, caffeine)Sub-msApplication codeHot config data, per-JVM cache
Distributed cacheRedis / Memcached cluster~1ms networkApplication codeSession data, DB query results, counters
Database query cacheDB engine buffer pool (InnoDB buffer)TransparentDB engineHot table rows, index pages
Object storage cacheCDN in front of S3~10–50msCDN + originVideo chunks (YouTube), file downloads

Rule of thumb: Cache as close to the user as possible. Move further in only when you need write consistency or shared state across servers.


2. Cache Eviction Policies

When the cache is full, one of these decides what gets removed.

PolicyFull nameBest forWorst forMemory overheadComplexity
LRULeast Recently UsedGeneral purpose; temporal localityScan-heavy workloads (evicts hot data on sequential scan)Low (doubly-linked list + hashmap)Low
LFULeast Frequently UsedStable hot-set (recommendations, leaderboard)New entries starved out early; bursty access patternsMedium (frequency counter per key)Medium
FIFOFirst In First OutSimple queue semanticsNo regard for hotnessVery lowVery low
RandomEvict a random entryApproximating LRU cheaplyUnpredictable eviction of hot dataNegligibleVery low
TTL-basedExpire after N secondsTime-sensitive data (sessions, tokens, rate limit windows)Long-lived data forced to re-fetch unnecessarilyMinimalLow
SLRUSegmented LRUMixed hot/warm workloads (DB buffer pools)Added implementation complexityLowMedium

Redis default: LRU (configurable per policy via maxmemory-policy). Common choices: allkeys-lru, volatile-lru, allkeys-lfu.

SDI relevance:

  • Gaming leaderboard (Vol 2 Ch10): LFU or sorted set with explicit TTL keeps hot scores
  • Rate limiter (Vol 1 Ch04): TTL-based expiry of counters per window
  • URL shortener (Vol 1 Ch08): LRU — recent redirects are hottest

3. Cache Invalidation Strategies

The hardest problem in caching is knowing when cached data is stale.

StrategyWrite pathRead pathConsistencyWrite latencyRiskWhen to use
TTL expiryWrite to DB onlyRead cache; miss → DB → cacheEventual (stale until TTL)LowServes stale data up to TTLSession tokens, rate-limit windows, feed previews
Cache-aside (lazy loading)Write to DB; optionally delete cache keyCheck cache; miss → DB → populate cacheEventualLowThundering herd on popular missMost general-purpose reads; URL shortener, news feed
Write-throughWrite to cache AND DB synchronouslyAlways hit cacheStrongHigher (2 writes)Wasted writes for rarely-read dataUser profile, session store — data read soon after write
Write-aroundWrite directly to DB, bypass cacheRead cache; miss → DB → populate cacheEventualLowCold reads after every writeLarge file metadata, infrequently-read blobs
Write-back (write-behind)Write to cache; async flush to DBRead from cacheEventualVery lowData loss if cache crashes before flushHigh-throughput counters, view counts, like counts

Pros/cons at a glance:

  • Write-through: No stale reads, but wastes writes on cold keys
  • Cache-aside: Only cache what’s needed, but first request always slow
  • Write-back: Fastest writes, but durability risk — never use for financial data
  • Write-around: Best when writes are rarely re-read (video upload metadata)

4. Distributed Cache Challenges

Cache Stampede (Thundering Herd)

Problem: Popular cache key expires → thousands of requests simultaneously miss and all query the DB.

Solutions:

  • Mutex / distributed lock: First request acquires lock, fetches from DB, populates cache. Others wait.
  • Probabilistic early expiration: Randomly refresh cache slightly before TTL expires based on request rate.
  • Background refresh: Background job pre-warms cache before TTL expires (News Feed feed rebuilding).
  • Stale-while-revalidate: Serve stale data while async refresh happens.

SDI example: News feed (Vol 1 Ch11) — celebrity posts expiring could cause stampede; solved with pre-computation and background refresh.


Cache Penetration (Null Value Attacks)

Problem: Requests for keys that don’t exist in DB (e.g., invalid user IDs) always miss cache and hit DB.

Solutions:

  • Cache null values: Store null or sentinel in cache with short TTL (e.g., 30 sec).
  • Bloom filter: Before hitting cache, check a bloom filter. If key definitely not in DB → reject immediately.

SDI example: URL shortener (Vol 1 Ch08) — invalid short codes shouldn’t fan through to MySQL.


Cache Avalanche (Mass Expiry)

Problem: Many cache keys expire at the same time → sudden DB spike.

Solutions:

  • Stagger TTLs: Add random jitter to TTL (TTL = base_ttl + random(0, 300)) to spread expiry.
  • Warm cache on startup: Pre-load hot keys before traffic hits.
  • Circuit breaker on DB: If DB is overwhelmed, return degraded response rather than cascading failure.

SDI example: Metrics monitoring (Vol 2 Ch05) — downsampled aggregates written with staggered TTLs to avoid mass expiry at the same minute boundary.


Cache Hotspot (Hot Key Problem)

Problem: One key gets disproportionate traffic (celebrity, viral post, trending item) → single Redis node bottleneck.

Solutions:

  • Local in-process cache: Cache the hot key in each app server’s memory (short TTL, 1–5 sec).
  • Key replication: Replicate hot key to multiple Redis slots (hot_key:shard_0, hot_key:shard_1), read from random shard.
  • Read replicas: Route reads to Redis replicas, not just primary.

SDI examples:

  • News feed (Vol 1 Ch11): Celebrity users with millions of followers create hot keys in fanout-on-write; hybrid model mitigates this.
  • Gaming leaderboard (Vol 2 Ch10): Top-ranked players’ scores accessed constantly.
  • Ad click aggregation (Vol 2 Ch06): Top-100 ads endpoint → Redis sorted set hotspot.

5. Which SDI Chapter Uses What Cache Strategy

ChapterCache technologyWhat is cachedStrategyWhy
Vol 1 Ch04 — Rate LimiterRedisRequest counters per user/IPTTL expiry, atomic INCRSub-ms counter ops; TTL auto-resets windows
Vol 1 Ch06 — Key-Value StoreRedis / customThe store itself is a cacheWrite-through or write-backCore topic — Dynamo-style design
Vol 1 Ch08 — URL ShortenerRedisshortCode → longURL mappingCache-aside + TTLRead-heavy redirects; 80% hit rate expected
Vol 1 Ch10 — Notification SystemRedisUser device tokens, preferencesCache-asideAvoid DB on every notification fan-out
Vol 1 Ch11 — News FeedRedisPre-built timelines per userWrite-through on fanoutFeed must load < 200ms; can’t query DB on read
Vol 1 Ch12 — Chat SystemRedisOnline presence, last-seenTTL (heartbeat refresh)Presence expires naturally if heartbeat stops
Vol 1 Ch13 — Search AutocompleteRedis / Trie cacheTop-K suggestions per prefixWrite-through (batch)Autocomplete must respond < 100ms
Vol 1 Ch14 — YouTubeCDN + RedisVideo segments (CDN); video metadata (Redis)Pull CDN; Cache-asideServe video from edge; metadata on API calls
Vol 1 Ch15 — Google DriveRedisFile metadata, chunk upload stateCache-asideMetadata reads vastly outnumber writes
Vol 2 Ch05 — Metrics MonitoringRedisRecent metric aggregatesWrite-through (streaming results)Fast dashboard queries against recent data
Vol 2 Ch06 — Ad Click AggregationRedisReal-time click counts (sorted sets)Write-back to batch layerHigh-frequency updates to counts
Vol 2 Ch10 — Gaming LeaderboardRedis Sorted SetFull leaderboardWrite-through on score updateSorted set ZADD/ZRANK are O(log N)
Vol 2 Ch13 — Stock ExchangeRedisOrder book snapshots, market dataWrite-through (microsecond)Matching engine state must be instantly readable

6. Key Numbers

Redis Performance

MetricValue
Throughput (single node)~100K–1M ops/sec (simple GET/SET)
Read latency (local)< 1ms (sub-millisecond)
Write latency (local)< 1ms
Max memory per node~100–300 GB typical deployment
Persistence overhead~10–30% write slowdown with AOF
Cluster max nodes1000 nodes (official recommendation)

Redis vs Memcached

FeatureRedisMemcached
Data structuresStrings, Lists, Sets, Sorted Sets, Hashes, Streams, HyperLogLogString (key-value) only
PersistenceYes (RDB snapshots + AOF log)No (memory only)
ReplicationMaster-replica + SentinelNo built-in replication
Cluster modeYes (Redis Cluster with hash slots)Yes (client-side sharding)
Pub/SubYesNo
Atomic operationsYes (INCR, LPUSH, ZADD are atomic)Yes (CAS)
MultithreadingSingle-threaded (I/O multi-threaded in 6.x)Multithreaded
Memory efficiency~2–3x overhead vs raw dataSlightly more memory efficient
Typical use in SDISessions, counters, leaderboards, timelines, pub/subSimple key-value cache at extreme throughput

Choose Redis when you need data structures (sorted sets for leaderboard, lists for queues, pub/sub for real-time).
Choose Memcached when you need pure key-value at maximum throughput with minimum overhead and zero persistence needed.


7. Interview Decision Framework

“Which cache strategy should I choose?”

START
  │
  ▼
Is the data written frequently AND re-read immediately?
  ├── Yes → Write-through (keep cache warm on every write)
  └── No
       │
       ▼
    Is the data written frequently but rarely re-read right after?
       ├── Yes → Write-around (bypass cache on write; lazy load on read)
       └── No
            │
            ▼
         Is ultra-low write latency critical (e.g., counters, view counts)?
            ├── Yes → Write-back (async flush) — ONLY if data loss is tolerable
            └── No → Cache-aside (lazy loading) — DEFAULT CHOICE
                       │
                       ▼
                    Add TTL to all keys to prevent stale data buildup
                    Add jitter to TTL to prevent cache avalanche
                    Consider null-value caching to prevent penetration

Quick rules for SDI interviews:

  1. Default to cache-aside with TTL — works for 80% of cases
  2. Use write-through when cache miss = visible latency (news feed, leaderboard)
  3. Use write-back only for high-frequency low-stakes writes (view counts, click counts)
  4. Always ask: “What happens on cache miss?” and “What happens when cache is down?”
  5. For distributed systems: mention thundering herd protection (mutex or stagger TTL)
  6. Redis sorted set = leaderboard; Redis counter + TTL = rate limiting; Redis list = message queue

See also: