What is System Design?

System design is the process of defining architecture, components, interfaces, and data flow to satisfy specified requirements at scale. It answers how a system will work — not just what it will do. Sub-pages: System Design - Scalability & CAP, System Design - Databases, System Design - Caching, System Design - Microservices, System Design - APIs & Networking, System Design - Case Studies.

Interview Approach — 7 Steps

Clarify requirements → Estimate scale (QPS, storage) → Define APIs → Draw HLD → Deep dive components → Identify bottlenecks & trade-offs → Discuss failure & recovery

🗺 Learning Roadmap

Beginner → Advanced Path

flowchart LR
    A["🟢 Beginner"] --> B["Client-Server\nHTTP · DNS · CDN"]
    B --> C["SQL vs NoSQL\nIndexing · ACID"]
    C --> D["Caching\nRedis · LRU · TTL"]
    D --> E["🟡 Intermediate"]
    E --> F["CAP Theorem\nReplication · Sharding"]
    F --> G["Message Queues\nKafka · RabbitMQ"]
    G --> H["Microservices\nAPI Gateway · Service Mesh"]
    H --> I["🔴 Advanced"]
    I --> J["Event Sourcing\nCQRS · Saga"]
    J --> K["Distributed Locks\nLeader Election · Raft"]
    K --> L["Case Studies\nYouTube · Chat · Twitter"]

Topic Map

TopicDifficultyKey Concepts
System Design - Scalability & CAP🟢 BeginnerVertical/Horizontal scaling, CAP, PACELC, SLA/SLO
System Design - Caching🟢 BeginnerRedis, LRU, TTL, Write-through, Cache stampede
System Design - Databases🟡 IntermediateReplication, Sharding, Indexing, Connection pooling
System Design - APIs & Networking🟡 IntermediateREST, gRPC, GraphQL, WebSockets, DNS, CDN, Rate limiting
System Design - Microservices🔴 AdvancedService mesh, Circuit breaker, Saga, CQRS, Outbox
System Design - Case Studies🔴 AdvancedURL shortener, Chat, YouTube, Twitter feed, Uber

⚡ Core Concepts Quick Reference

Scalability Cheat Sheet

StrategyWhatUse When
Vertical ScaleBigger machine (more CPU/RAM)Simple, small-scale. Quick fix.
Horizontal ScaleMore machines + load balancerProduction at scale. Always prefer.
Read ReplicasCopies of DB for read trafficRead-heavy workloads (10:1 ratio)
ShardingSplit DB rows across serversWrite-heavy or >TB data scale
CachingIn-memory data (Redis)Frequently read, rarely changed data
CDNEdge-cached static contentStatic assets, global user base
Message QueueAsync decoupling (Kafka)Background jobs, event-driven flows

CAP Theorem at a Glance

  • CAP — Pick 2 of 3 (but P is always required) CP (consistency) or AP (availability).

    In practice you choose

SystemTypeBehaviour
PostgreSQL (single node)CAFull ACID — no partition tolerance
MongoDB, HBase, ZookeeperCPReturns error rather than stale data
Cassandra, DynamoDB, CouchDBAPAlways responds — may be stale

Availability Numbers

SLADowntime / yearDowntime / month
99%3.65 days7.2 hours
99.9%8.76 hours43.8 min
99.99%52.6 minutes4.38 min
99.999%5.26 minutes26 sec

Latency Numbers (Know These!)

L1 cache reference          0.5 ns
L2 cache reference            7 ns
Main memory reference       100 ns
SSD random read             150 μs
Read 1MB from RAM           250 μs
Round trip in same datacenter 0.5 ms
Read 1MB from SSD             1 ms
Disk seek                    10 ms
Packet CA → Netherlands     150 ms
  • Rules of thumb

    Memory is 1000× faster than SSD. SSD is 100× faster than HDD. Cross-datacenter latency ≈ 150–300 ms. Design accordingly.

🏗 Architecture Patterns

Request Flow — Typical Web System

flowchart TD
    U[👤 User] --> DNS[DNS Resolution]
    DNS --> CDN[CDN — Static Assets]
    DNS --> LB[Load Balancer]
    LB --> API1[App Server 1]
    LB --> API2[App Server 2]
    API1 --> Cache[Redis Cache]
    API2 --> Cache
    Cache -->|miss| DB_R[Read Replica]
    API1 -->|writes| DB_P[Primary DB]
    DB_P -->|replicate| DB_R
    API1 --> MQ[Message Queue]
    MQ --> Worker[Background Worker]
    Worker --> DB_P

Monolith vs Microservices

AspectMonolithMicroservices
DeploySingle unitIndependent per service
ScaleScale entire appScale individual services
Fault isolationOne bug can crash allFailures isolated
ComplexityLow initiallyHigh (distributed systems)
Team sizeSmall teamsLarge, independent teams
Start with✅ Always❌ Don’t start here
Migrate viaStrangler Fig pattern

Load Balancing Algorithms

AlgorithmHowBest For
Round RobinRotate sequentiallyEqual-capacity servers
Weighted Round RobinMore requests to stronger serversMixed-capacity fleet
Least ConnectionsRoute to least busyLong-lived connections
IP HashSame client → same serverSession stickiness
Least Response TimeRoute to fastest respondingLatency-sensitive APIs

🗄 Data Layer Patterns

Caching Strategies

StrategyWrite FlowProsCons
Cache-AsideApp reads cache → miss → load DB → populateSimple, flexibleCold start, stale risk
Write-ThroughWrite cache + DB togetherAlways freshSlow writes
Write-BackWrite cache → async sync to DBFast writesData loss risk on crash
Read-ThroughCache handles DB fetch on missTransparentLibrary needed

Database Sharding Strategies

MethodKeyProsCons
Rangeuser_id 1–1M → Shard 1Simple, range queries easyHot spots
Hashhash(id) % NEven distributionRange queries hard, resharding painful
DirectoryLookup table → shardMost flexibleLookup table = SPOF
GeographicUS → US shardLow latency, GDPRUneven regions

Redis vs Memcached

FeatureRedisMemcached
Data typesStrings, Lists, Sets, Hashes, Sorted Sets, StreamsStrings only
PersistenceYes (RDB + AOF)No
Pub/SubYesNo
ClusteringRedis Cluster (built-in)Client-side only
Lua scriptingYesNo
Best forSessions, queues, leaderboards, rate limitingSimple high-throughput key-value cache

🔌 API & Communication

REST vs GraphQL vs gRPC

AspectRESTGraphQLgRPC
ProtocolHTTP/1.1–2HTTPHTTP/2
FormatJSONJSONProtocol Buffers (binary)
Type safetyNoYes (schema)Yes (protobuf)
CachingEasy (HTTP cache)ComplexNo native
StreamingNoSubscriptionsFull bidirectional
Browser support❌ (limited)
Use forPublic APIs, CRUDMobile / BFFInternal microservices

Real-Time Communication

MethodDirectionWhen to Use
Short PollingClient → Server (repeated)Simple, infrequent updates
Long PollingClient waits, server holdsNear real-time, simple fallback
WebSocketsBidirectional persistentChat, gaming, live collaboration
SSEServer → Client onlyDashboards, feeds, notifications

Rate Limiting Algorithms

AlgorithmHowAllows Burst?Accuracy
Token BucketTokens refill at fixed rate✅ YesHigh
Leaky BucketFixed output rate❌ SmoothedHigh
Fixed WindowCount per time window✅ at edgesLow (boundary bug)
Sliding Window LogTrack timestampsVery High
Sliding Window CounterWeighted windowPartialHigh

🧩 Distributed System Patterns

Key Patterns Quick Reference

PatternProblem SolvedKey Tool
Circuit BreakerCascading failuresResilience4j, Hystrix
BulkheadThread pool isolationSeparate pools per service
Retry + BackoffTransient failuresExponential backoff + jitter
SagaDistributed transactionsChoreography or Orchestration
OutboxAtomic DB write + event publishTransactional outbox table
CQRSRead/write model separationSeparate read/write stores
Event SourcingAudit trail + state replayKafka / EventStore
SidecarInfrastructure separationEnvoy, Istio
Strangler FigMonolith → microservices migrationAPI Gateway routing

Circuit Breaker States

stateDiagram-v2
    [*] --> CLOSED
    CLOSED --> OPEN : N failures threshold
    OPEN --> HALF_OPEN : Timeout expires
    HALF_OPEN --> CLOSED : Test request succeeds
    HALF_OPEN --> OPEN : Test request fails

Message Queue Comparison

FeatureApache KafkaRabbitMQAWS SQS
TypeDistributed logAMQP brokerManaged cloud queue
RetentionConfigurable (days/weeks)Deleted on consumeUp to 14 days
ThroughputMillions/secTens of thousands/secUnlimited (managed)
OrderingPer partitionPer queueFIFO queues available
Replay✅ Yes (any offset)❌ No❌ No
Best forEvent streaming, audit logs, CQRSTask queues, RPC, complex routingAWS ecosystem, serverless

📐 Back-of-Envelope Estimation

Storage Size Reference

ItemSize
ASCII character1 byte
Integer4 bytes
Long / Double8 bytes
UUID16 bytes
Tweet (text)~280 bytes
Profile photo~200 KB
HD photo~3 MB
1 min HD video~50 MB

QPS Estimation — Twitter Scale

Inputs:
  300M MAU, 50% DAU = 150M daily active users
  100 reads/day, 2 writes/day per user

Read QPS:  150M × 100 / 86,400 ≈ 174,000 QPS   (peak 2×: 350K)
Write QPS: 150M × 2   / 86,400 ≈   3,500 QPS   (peak 2×: 7K)

Storage / year:
  3,500 writes/sec × 86,400 × 365 = 110B tweets
  110B × 780 bytes ≈ 86 TB/year

🔐 Security Quick Reference

Auth Patterns

PatternHowUse When
Session + CookieServer stores session IDTraditional web apps
JWTStateless signed tokenSPAs, mobile, microservices
OAuth 2.0Delegate auth to provider”Login with Google/GitHub”
API KeyStatic key in headerServer-to-server, developer APIs
mTLSMutual certificate authMicroservice-to-microservice

Common Vulnerabilities & Fixes

AttackFix
SQL InjectionParameterised queries / ORM
XSSSanitise output, Content-Security-Policy
CSRFCSRF tokens, SameSite cookies
IDORAlways verify ownership server-side
DDoSRate limiting + CDN + WAF
Broken AuthMFA, short JWT TTL + refresh tokens

🏆 Case Studies

At a Glance

SystemCore ChallengeKey Solutions
URL ShortenerUnique ID generation, high-read cacheBase62 encoding, Redis, 301/302 redirect
Chat (WhatsApp)Real-time delivery, message orderingWebSockets, Kafka, Cassandra for history
YouTubeVideo upload, transcoding, global deliveryObject storage (S3), CDN, async transcoding
Twitter FeedFan-out on write vs fan-out on readHybrid fanout, Redis sorted sets for timeline
UberReal-time location, geo matchingGeohash / S2, WebSockets, eventual consistency
Rate LimiterDistributed counters, accuracyRedis + Lua script, sliding window counter

📚 Useful Links & Resources

Books

Online Resources