cs.oldnet

All sheets / Architecture

System Design

A framework for design rounds plus the building blocks and numbers to reach for.

Interview framework (45 min)

  1. Requirements (5 min): functional, non-functional (scale, latency, availability, consistency).
  2. Estimates (3 min): users, QPS (avg & peak), storage/year, bandwidth.
  3. API (5 min): endpoints, request/response.
  4. High-level design (10 min): clients → LB → services → data stores → async workers.
  5. Deep dive (15 min): data model, bottlenecks, scaling, failure modes.
  6. Wrap up: trade-offs, monitoring, what you'd do next.

Back-of-envelope

QuantityValue
Seconds/day~86,400 ≈ 10^5
1M req/day~12 QPS
100M req/day~1,200 QPS
Peak2–3x average
L1 / RAM / SSD / disk seek1 ns / 100 ns / 100 µs / 10 ms
Same-DC round trip~0.5 ms
Cross-continent~150 ms
1 char / int / UUID1 B / 4–8 B / 16 B

Availability

SLADowntime / year/ month
99%3.65 days7.2 h
99.9%8.76 h43.8 min
99.99%52.6 min4.4 min
99.999%5.26 min26 s

Serial dependencies multiply: two 99.9% services in series ≈ 99.8%.

CAP & consistency

  • CAP: on a network partition, choose Consistency or Availability.
  • PACELC: else (no partition) choose Latency or Consistency.
  • Strong vs eventual vs read-your-writes vs causal consistency.
  • Quorum: R + W > N gives strong reads (e.g., N=3, W=2, R=2).

Scaling building blocks

  • Load balancer (L4/L7): round robin, least connections, consistent hashing; health checks.
  • Stateless services + horizontal scaling; session in Redis/JWT.
  • Cache: CDN → app cache (Redis) → DB cache.
  • DB: read replicas, sharding (hash/range/geo), partitioning, denormalization.
  • Async: queues to absorb spikes; workers process later.
  • Consistent hashing: minimal key movement when nodes change; virtual nodes for balance.

Caching

StrategyHow
Cache-asideapp reads cache, on miss loads DB & populates (most common)
Read-throughcache loads from DB itself
Write-throughwrite cache + DB synchronously
Write-backwrite cache, flush to DB later (fast, risk of loss)
  • Eviction: LRU, LFU, TTL. Invalidate on write or short TTLs.
  • Stampede: request coalescing, locks, jittered TTLs, stale-while-revalidate.

Rate limiting

  • Token bucket (bursts allowed), leaky bucket (smooth), fixed window, sliding window log/counter.
  • Store counters in Redis (INCR + EXPIRE); return 429 with Retry-After.

Data store choice

NeedStore
Relational, transactions, ad-hoc queriesPostgres / MySQL
Key-value at scale, known patternsDynamoDB / Cassandra
Cache, counters, leaderboardsRedis
Search, fuzzy textOpenSearch / Elasticsearch
Blobs, mediaS3
AnalyticsRedshift / BigQuery / ClickHouse
Time seriesTimestream / Prometheus / InfluxDB

Classic designs: key ideas

  • URL shortener: base62 of ID (or hash), KV store, 301/302, cache hot links.
  • News feed: fan-out on write for most users, fan-out on read for celebrities.
  • Chat: WebSockets, message store partitioned by conversation, presence via heartbeats.
  • Rate limiter: token bucket in Redis at the gateway.
  • Notification system: queue per channel, templates, retries, idempotency, user prefs.
  • CI/CD platform: job queue, ephemeral runners, artifact store, log streaming, secrets vault.
  • Feature flag service: rules in DB, streamed to SDKs, local evaluation, CDN-cached configs.

Reliability checklist

Redundancy across AZs, health checks + auto-healing, backups + tested restore, graceful degradation, idempotency, timeouts/retries/circuit breakers, observability + alerts on SLOs, runbooks.