System Design

System Design Fundamentals

Core building blocks every senior interview expects — scalability, CAP, caching, load balancing, databases, CDN, and estimation.

Interview tip Start every answer: requirements (functional + non-functional) → estimate scale → high-level diagram → deep dive 2 components → trade-offs.

① The 45-minute interview framework

Every system design answer follows the same skeleton:
  1. Clarify (5 min): functional requirements, non-functional (latency, availability, consistency, scale), out of scope
  2. Estimate (5 min): DAU, QPS, storage, bandwidth — round numbers, state assumptions
  3. High-level design (10 min): boxes and arrows — client, CDN, LB, services, cache, DB, async queues
  4. Deep dive (15 min): pick 2 components the interviewer cares about — data model, hot path, sharding
  5. Trade-offs (5 min): SQL vs NoSQL, sync vs async, strong vs eventual consistency
  6. Wrap-up (5 min): failure modes, monitoring, future scale
Say aloud: "I'll start with requirements and scale, then draw the architecture, then go deep on X and Y." Interviewers reward structure over jumping to Redis.

② Scalability — vertical vs horizontal

ApproachHowProsCons
Vertical scaleBigger CPU/RAM/disk on one machineSimple, no code changesHard ceiling, single point of failure, expensive at top tier
Horizontal scaleMore machines behind a load balancerNear-unlimited, fault tolerantNeeds stateless apps, data partitioning, ops complexity
Auto-scalingAdd/remove instances on metricsHandles traffic spikesCold start latency, config tuning
Read replicasCopy DB for read-heavy workloadsOffload reads from primaryReplication lag, write still on primary
Stateless services scale easily: session data lives in Redis or DB, not in app memory. Sticky sessions reduce flexibility — prefer shared session store.

Stateful services (WebSocket rooms, in-memory counters) need sharding, consistent hashing, or dedicated state stores.
Scaling decision tree
Traffic growing?Optimize firstCache + CDNScale readsScale writes

③ Back-of-envelope estimation

Assumptions
  • 1M DAU × 10 requests/day = 10M req/day ≈ 116 req/s average
  • Plan for 10× peak → ~1,200 req/s at peak
  • Avg request 1 KB in + 5 KB out → 1.2K × 6 KB ≈ 7 MB/s bandwidth
  • 100 bytes per record × 1B records = 100 GB raw (plus indexes ~2–3×)
  • 1 photo 200 KB × 10M uploads/month ≈ 2 TB/month storage growth
ResourceFormulaExample
QPSDAU × actions/day ÷ 86,40010M DAU × 20 ÷ 86,400 ≈ 2,300 avg
Storagerecords × size × retention1B users × 500 B × 5 yr
BandwidthQPS × avg payload5K RPS × 50 KB = 250 MB/s
Memory (cache)hot data % × total dataset20% of 100 GB = 20 GB Redis
Rules of thumb: round to nearest power of 10; state assumptions ("assuming 10:1 read:write"); latency budget: CDN 10ms + LB 5ms + app 50ms + cache 1ms + DB 10ms = ~76ms p99 target.

④ Load balancing

AlgorithmBehaviorBest for
Round robinRotate requests evenlyHomogeneous stateless servers
Least connectionsSend to server with fewest active connsLong-lived connections, varying request times
Weighted round robinMore traffic to stronger machinesMixed hardware capacity
IP hash / consistent hashSame client → same serverSession affinity without shared store (limited)
Layer 4 (TCP)Route by IP/port, fastRaw throughput, WebSocket
Layer 7 (HTTP)Route by URL, headers, cookiesMicroservices, A/B tests, canary
Load balancer placement
Clients
DNS (geo-routing)
Global LB
Regional LB (L7)
App Server 1
App Server 2
App Server N
Health checks: LB polls /health — remove unhealthy nodes within seconds. SSL termination at LB offloads crypto from app servers. Active-passive for stateful components; active-active for stateless.

⑤ Caching strategies

PatternRead pathWrite pathRisk
Cache-asideApp → cache → miss → DB → populate cacheApp writes DB, invalidates cacheStale data if invalidation missed
Read-throughCache fetches from DB on missSame as cache-aside writesCache library must support it
Write-throughRead from cacheWrite cache + DB togetherWrite latency; cache and DB must sync
Write-behindRead from cacheWrite cache; async flush to DBData loss if cache crashes before flush
Always set TTL — even with invalidation, TTL is your safety net. Eviction: LRU (common), LFU (frequency), TTL-based.

Hot key problem: one viral key hammers a single Redis shard — replicate hot keys, local in-process cache, or pre-warm.

Cache stampede: many requests miss simultaneously — use singleflight/locking so only one thread repopulates.
Cache-aside flow
Read requestCheck RedisHit → returnMiss → DBSet cache + return

⑥ CAP theorem & consistency

CAP: During a network partition, you choose between Consistency (all nodes see same data) and Availability (every request gets a response). Partition tolerance is non-negotiable in distributed systems — so you pick CP or AP.
ChoiceExample systemsUse when
CP (Consistency + Partition)ZooKeeper, etcd, HBaseBanking, inventory, leader election
AP (Availability + Partition)Cassandra, DynamoDB, CouchDBSocial feeds, shopping carts, analytics
Strong consistencySingle-leader replication, sync writesFinancial transactions, unique constraints
Eventual consistencyAsync replication, quorum readsUser profiles, like counts, metrics
PACELC extension: Else (no partition), choose Latency vs Consistency. Most web apps pick low latency with eventual consistency for non-critical reads.

Read-your-writes: user sees their own updates immediately — route to primary or use session stickiness + version checks.

⑦ Database types — when to use what

TypeExamplesStrengthsWeak for
Relational (SQL)PostgreSQL, MySQLACID, joins, complex queriesMassive horizontal write scale
Wide-columnCassandra, HBaseHigh write throughput, time-seriesAd-hoc joins, transactions
DocumentMongoDB, DynamoDBFlexible schema, nested JSONMulti-document ACID (improving)
Key-valueRedis, MemcachedSub-ms reads, sessions, countersComplex queries, durability (Redis)
SearchElasticsearch, OpenSearchFull-text, aggregations, logsPrimary transactional store
GraphNeo4j, NeptuneRelationship traversalsSimple CRUD at huge scale
VectorPinecone, pgvectorSimilarity search, RAGExact lookups by primary key
Polyglot persistence: PostgreSQL for orders, Redis for sessions, Elasticsearch for search, S3 for blobs — each store optimized for its access pattern.

Sharding: split rows by user_id hash or range when single-node limits hit (~10K writes/sec PG, higher with Citus/Cockroach).

⑧ CDN & edge delivery

Origin server — your app or object storage (S3)
CDN edge PoPs — cache static assets and cacheable API responses globally
DNS / anycast — route user to nearest edge
Client — receives content from edge, not origin (lower latency, less origin load)
Cache at CDN: static files (JS, CSS, images), video segments, and cacheable GET responses with Cache-Control headers.

Do NOT cache: personalized pages, auth tokens, POST responses, data with Cache-Control: private/no-store.

Invalidation: versioned URLs (app.v2.js) beat purge APIs. Edge compute (Cloudflare Workers) for redirects and A/B at edge.
CDN in request path
User (Tokyo)
User (London)
CDN Edge Tokyo
CDN Edge London
Origin / API (US)

⑨ End-to-end request path

Typical web request path
ClientDNSCDNLoad BalancerApp ServerCacheDatabase
Latency budget example (p99 < 200ms):
  • DNS + TLS: 20ms
  • CDN hit: 10ms (miss adds origin round-trip)
  • LB + routing: 5ms
  • App logic: 30ms
  • Redis: 1ms
  • DB query (indexed): 10ms
  • Serialization + network: 20ms
Async off critical path: analytics, emails, search indexing → message queue (Kafka/SQS) → workers. User-facing response returns before side effects complete.

⑩ Revision checklist

  • Opened with functional + non-functional requirements
  • Calculated QPS, storage, bandwidth with stated assumptions
  • Drew CDN → LB → App → Cache → DB diagram
  • Explained vertical vs horizontal scaling choice
  • Named caching strategy and invalidation approach
  • Applied CAP with CP vs AP example relevant to the problem
  • Picked SQL vs NoSQL with access-pattern justification
  • Mentioned CDN for static assets and cacheable reads
  • Identified async processing for non-critical work
  • Discussed failure modes: DB down, cache stampede, hot keys
scalabilityCAPcachingload-balancingestimationCDN