Part 06 · Revision sheet

Caching, Scalability and Performance

Key Terms

  • Hit ratio — the fraction of reads a cache layer answers without going further, hits ÷ (hits + misses) (→ 82)
  • Cache-aside — the application checks the cache first; on a miss it reads the database and fills the cache (→ 83)
  • Write-back — write to the cache immediately and flush to the database asynchronously, trading durability for speed (→ 84)
  • TTL — the upper bound on how long any stale cache entry, however it got stale, is allowed to live (→ 85)
  • LRU — evicts whichever cache entry hasn't been touched in the longest time (→ 86)
  • Cache stampede — a popular key expires and many concurrent requests all miss and recompute at once (→ 88)
  • Pull CDN — the edge fetches from the origin on first miss; the default CDN mode (→ 89)
  • Stateless service — keeps no state between requests that only one instance would know about (→ 91)
  • L7 load balancing — routes by reading the request itself — path, header, or cookie (→ 93)
  • Token bucket — a bucket of N tokens refilling at a fixed rate; allows a burst up to N, then throttles to the refill rate (→ 96)
  • Fan-out on write — pushes a post into every follower's pre-built feed at write time (→ 99)
  • Tail latency — the latency of the slowest fraction of requests (commonly p95, p99, p999) (→ 100)

Decision Rules

  • Default to cache-aside; move to write-through only when read-after-write freshness from the cache matters (→ 83, 84)
  • TTL is always the safety net; add explicit invalidation only when writes are known and few and staleness has real cost (→ 85)
  • Default to LRU eviction; switch to LFU only once measurement shows a stable popularity skew (→ 86)
  • Use pull CDN for most content; reserve push for predictable, large, launch-critical assets (→ 89)
  • Scale vertically first while it's cheap and the ceiling isn't close; scale horizontally once it is, or redundancy is needed (→ 90)
  • Pick round robin for uniform request cost, least connections for variable cost, consistent hashing when the same key must keep hitting the same instance (→ 92)
  • Use L4 when the decision doesn't need to see inside the request; use L7 when routing depends on path, header, or cookie (→ 93)
  • Never rely on auto scaling alone for a sudden spike; pair it with load shedding or pre-provisioned headroom for the reaction-lag window (→ 95, 97, 102)
  • Choose fan-out on write, fan-out on read, or a hybrid from write-cost-vs-read-cost arithmetic at actual scale, never from instinct (→ 99)
  • Plan and operate below roughly 70–80% utilization, never at the measured maximum (→ 101, 102)

Numbers & Formulas

  • Hit ratio = hits ÷ (hits + misses) (→ 82)
  • Blended mean latency across a hierarchy = h·Lh + (1 − h)·Lm (→ 82)
  • Tail amplification: P(at least one of n independent calls is slow) = 1 − (1 − p)ⁿ (→ 100)
  • Percentile by nearest rank: the p-th percentile sits at rank ⌈p/100 × N⌉ in the sorted list (→ 100)
  • Little's Law: L = λW — mean concurrency = arrival rate × mean time in the system (→ 101)
  • Maximum sustainable arrival rate: λ_max = C ÷ W (pool capacity ÷ mean hold time) (→ 101)
  • Amdahl's Law: S(n) = 1 ÷ ((1 − p) + p/n); as n → ∞, S(∞) = 1 ÷ (1 − p) (→ 101)
  • Capacity planning: required capacity = projected peak ÷ target utilization ceiling (→ 102)
  • Growth projection: projected peak = current peak × (1 + growth rate)^months (→ 102)
  • Utilization = busy time ÷ total time, for a given resource (→ 103)

Traps

  • A cold cache after a deploy or crash produces a latency and load spike exactly when the system is least ready for it (→ 82)
  • Cache-aside's correct write ordering still leaves a refill race: a reader fetching the old value just before a write can set the cache after the delete (→ 83)
  • Write-back loses whatever is still only in the buffer if the cache crashes before flushing (→ 84)
  • An unbounded queue anywhere in the chain defeats backpressure, load shedding, and admission control alike — none of them is ever signaled or triggered (→ 97)
  • Reporting mean latency alone hides exactly the tail that fan-out amplifies into a routine problem (→ 100)

Self-Quiz

  1. Can you compute a cache hierarchy's blended latency and origin load from per-layer hit ratios? (no → revisit 82)
  2. Can you state cache-aside's correct write ordering and the refill race that survives it anyway? (no → revisit 83)
  3. Can you compute a token bucket's total allowed requests over a stated time window? (no → revisit 96)
  4. Can you compute tail-latency amplification, 1 − (1 − p)ⁿ, for a given fan-out? (no → revisit 100)
  5. Can you size a pool's maximum sustainable throughput from Little's Law? (no → revisit 101)

Search the guide