STACK
Ch 14 · System Design Patterns
Chapter 14 · The thinking toolkit

System Design — numbers, laws, and patterns

The mental toolkit a senior engineer uses to whiteboard any system. Numbers to memorise, laws to invoke, patterns to reach for.

14.1 · Functional vs non-functional — NFRs come first

FunctionalNon-functional
"User can place an order""99.95% available, p95 < 200ms, 10k RPS peak"
"Admin can refund""PII encrypted at rest; audit log immutable for 7 years"

Architecture is shaped by NFRs, not the feature list.

14.2 · Numbers worth memorising

OperationOrder
L1 cache0.5 ns
L2 cache7 ns
RAM100 ns
SSD random~150 μs
Same-DC RTT~0.5 ms
Cross-region RTT~150 ms

14.3 · The laws

LawWhat it saysUse for
Little's Law$L = \lambda W$ — concurrency = arrival rate × wait time.Sizing pools, queues.
Amdahl's LawSpeedup limited by serial fraction.Parallelisation budgets.
Universal Scalability LawSpeedup limited by contention + coherence.Why scaling stops linear.
Brewer's CAP / PACELCDistributed trade-offs.Storage choice.
Conway's LawSystem mirrors the org chart.Team / arch alignment.
Hyrum's LawEvery observable behaviour will be depended on.API design.
Postel's LawBe conservative in what you send, liberal in what you accept.Interop (controversial today).

14.4 · Scaling toolkit

MoveWhen
Vertical scaleBigger box. Simplest first move.
Horizontal scaleMore boxes behind LB. Stateless services.
Read replicasRead-heavy; tolerate replica lag.
ShardingOnce one box can't hold the data.
CQRSRead/write patterns diverge.
AsynchronousPush slow work to queues.
CachingRepeated reads; expensive computations.
CDN / edgeGeographically distributed users.
Pre-computeMaterialised views, denormalisation.

14.5 · Caching strategies

PatternHow
Cache-asideApp checks cache; on miss → DB → fill.
Read-throughCache library hits DB on miss.
Write-throughWrite to cache + DB sync.
Write-behindWrite to cache; async flush.
Refresh-aheadPre-fetch keys near expiry.
Negative cacheCache "not found" results.

Eviction: LRU (default), LFU, TTL. Invalidation: TTL, write-through, CDC-driven.

14.6 · Rate limiting algorithms

AlgorithmHow
Token bucketBucket refills tokens at rate r; each request consumes one. Bursts allowed up to bucket size.
Leaky bucketFixed-size queue; constant processing rate.
Fixed windowN requests per window; boundary bursts possible.
Sliding windowSmoothed rate; more accurate.
Concurrency limitN in-flight max; back-pressure tightly.

14.7 · Resilience patterns

Retry with jitter

Exponential backoff + randomness. Never naked retry.

Timeout

Every network call has one. Default is always wrong.

Circuit breaker

Open → half-open → closed; stops cascading failure.

Bulkhead

Isolate pools so one slow dep can't drain all threads.

Hedged requests

Send a backup after p95; cancel slower.

Graceful degradation

Serve stale cache; hide non-essential features.

Idempotency key

Safe retries on side-effectful ops.

Dead-letter queue

Catch poison messages without losing them.

Chaos engineering

Kill instances on purpose so prod surprises don't.

14.8 · Hot key / hot shard

  • Detect via per-key metrics (slow log, top-N).
  • Mitigate: salt the key, split into shards-of-one, dedicated cache, request coalescing (single-flight).
  • For multi-tenant: cap per-tenant QPS to keep one tenant from blowing the shard.

14.9 · The whiteboard rubric — design any system in 6 steps

  1. Clarify — functional + NFR. Get numbers: users, RPS, payload, latency, retention.
  2. Capacity estimate — back-of-envelope: RPS × payload × growth.
  3. API / event surface — sketch endpoints / messages.
  4. Data model — entities, access patterns, consistency needs.
  5. Architecture — services, DBs, caches, queues, edge. Diagram.
  6. Trade-offs & failure modes — what breaks first; how you'd evolve.

14.10 · Anti-patterns to call out fast

  • Distributed monolith — services that must all deploy together.
  • Synchronous chains of N services. p99 = product of N tails.
  • "Eventual consistency" used to mean "we'll figure it out". Define it explicitly.
  • No bulkheads — one slow dep takes the whole app.
  • Unbounded queues — hide back-pressure until they explode.
  • Caching layer that's also the source of truth.

14.11 · Self-check

Q1 · 5000 RPS at 200ms p95 → size in-flight for…
~50
~1000
~10,000
Depends on language