14.1 · Functional vs non-functional — NFRs come first
| Functional | Non-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
| Operation | Order |
|---|---|
| L1 cache | 0.5 ns |
| L2 cache | 7 ns |
| RAM | 100 ns |
| SSD random | ~150 μs |
| Same-DC RTT | ~0.5 ms |
| Cross-region RTT | ~150 ms |
14.3 · The laws
| Law | What it says | Use for |
|---|---|---|
| Little's Law | $L = \lambda W$ — concurrency = arrival rate × wait time. | Sizing pools, queues. |
| Amdahl's Law | Speedup limited by serial fraction. | Parallelisation budgets. |
| Universal Scalability Law | Speedup limited by contention + coherence. | Why scaling stops linear. |
| Brewer's CAP / PACELC | Distributed trade-offs. | Storage choice. |
| Conway's Law | System mirrors the org chart. | Team / arch alignment. |
| Hyrum's Law | Every observable behaviour will be depended on. | API design. |
| Postel's Law | Be conservative in what you send, liberal in what you accept. | Interop (controversial today). |
14.4 · Scaling toolkit
| Move | When |
|---|---|
| Vertical scale | Bigger box. Simplest first move. |
| Horizontal scale | More boxes behind LB. Stateless services. |
| Read replicas | Read-heavy; tolerate replica lag. |
| Sharding | Once one box can't hold the data. |
| CQRS | Read/write patterns diverge. |
| Asynchronous | Push slow work to queues. |
| Caching | Repeated reads; expensive computations. |
| CDN / edge | Geographically distributed users. |
| Pre-compute | Materialised views, denormalisation. |
14.5 · Caching strategies
| Pattern | How |
|---|---|
| Cache-aside | App checks cache; on miss → DB → fill. |
| Read-through | Cache library hits DB on miss. |
| Write-through | Write to cache + DB sync. |
| Write-behind | Write to cache; async flush. |
| Refresh-ahead | Pre-fetch keys near expiry. |
| Negative cache | Cache "not found" results. |
Eviction: LRU (default), LFU, TTL. Invalidation: TTL, write-through, CDC-driven.
14.6 · Rate limiting algorithms
| Algorithm | How |
|---|---|
| Token bucket | Bucket refills tokens at rate r; each request consumes one. Bursts allowed up to bucket size. |
| Leaky bucket | Fixed-size queue; constant processing rate. |
| Fixed window | N requests per window; boundary bursts possible. |
| Sliding window | Smoothed rate; more accurate. |
| Concurrency limit | N 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
- Clarify — functional + NFR. Get numbers: users, RPS, payload, latency, retention.
- Capacity estimate — back-of-envelope: RPS × payload × growth.
- API / event surface — sketch endpoints / messages.
- Data model — entities, access patterns, consistency needs.
- Architecture — services, DBs, caches, queues, edge. Diagram.
- 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