Skip to main content
HS
HiSETSuccess
LeetCode Prep

System Design

Comprehensive interview guide: scalability, databases, distributed systems, cloud architecture, and full case-study walkthroughs — plus adaptive quizzes.

Chapter

Distributed Systems

CAP, consistency models, messaging, idempotency, and protecting services under failure.

CAP & PACELC

CAP theorem: in a partition, a distributed system must choose Consistency (every read returns the latest write or errors) or Availability (every request gets a response, possibly stale). Partition tolerance is non-negotiable in real networks — so the tradeoff is really C vs A during failure.

ChoiceDuring partitionExample systems
CPReject writes/reads to preserve consistencyHBase, MongoDB (default), ZooKeeper
APServe requests; replicas may divergeCassandra, DynamoDB, CouchDB
CAOnly works without partitions (single node)Traditional RDBMS on one server

PACELC extends CAP: else (normal operation, no partition), choose Latency vs Consistency. Dynamo-style systems pick EL (low latency, eventual consistency); Spanner picks EC (consistent but higher latency).

How to use CAP in interviews

Don't recite the theorem — apply it: 'During a network split between US and EU, do we block checkout (CP) or allow orders with later reconciliation (AP)?' Tie to business requirements.

Consistency Models

Consistency models define what guarantees readers observe about writes. Stronger guarantees simplify application logic but cost latency and availability. Weaker models scale better but require careful client handling.

ModelBehaviorExample use
LinearizableAll ops appear in a single global orderLeader election, distributed locks
Strong / sequentialReads see latest committed writeBank transfers, inventory deduction
CausalCausally related ops seen in orderComment threads, chat ordering
Read-your-writesUser always sees own updatesProfile edits, draft saves
EventualReplicas converge given no new writesLike counts, CDN, DNS
Monotonic readsUser never sees time go backwardSocial feed pagination
  • Leader-based replication enables strong consistency on primary
  • Quorum reads/writes tune consistency vs latency in leaderless systems
  • Version vectors and conflict-free replicated data types (CRDTs) for merge
  • Session tokens route reads to replica that has seen user's writes

Practical default

Strong consistency for money and inventory. Eventual for analytics, counters, and social engagement metrics. Read-your-writes for user-generated content.

Message Queues

Message queues decouple producers from consumers, absorb traffic spikes, and isolate failures. Point-to-point queues deliver each message to one consumer (work distribution). Messages are deleted after acknowledgment.

SystemModelStrength
Amazon SQSManaged queueSimple, fully managed, at-least-once
RabbitMQAMQP brokerRouting, exchanges, complex topologies
Redis StreamsIn-memory logLow latency; memory-bound
ActiveMQ / Service BusEnterprise messagingTransactions, sessions
Queue-based decoupling
API Server ──produce──▶ [ Queue ] ──consume──▶ Worker Pool
                              │
                         (buffers spikes;
                          retries on failure)
  • Visibility timeout: message hidden while processing; extend if slow
  • Dead-letter queue (DLQ) for messages that fail max retries
  • Back-pressure: slow consumers cause queue depth to grow — monitor lag
  • Ordering: single partition or FIFO queue when order matters

When to introduce a queue

Async work (email, image processing), spike absorption, cross-service decoupling. Don't add a queue to synchronous user-facing paths unless you can accept delay.

Pub/Sub & Event Streams

Publish/subscribe fan-out delivers one message to many subscribers. Log-based event streams (Kafka, Pulsar) retain ordered, replayable history in partitions — enabling event sourcing, stream processing, and multiple consumer groups at different offsets.

PatternDeliveryRetention
Pub/Sub (ephemeral)Fire-and-forget to subscribersMessages deleted after ack
Event log (Kafka)Durable partitioned logConfigurable retention (days/forever)
Event sourcingState = fold of all eventsFull history for audit/replay
CDC streamDB changes as eventsSync search indexes, caches, warehouses
Kafka topic with consumer groups
Producer → Topic (partitions P0, P1, P2)
              │
    ┌─────────┼─────────┐
    ▼         ▼         ▼
 Group A   Group B   Group C
(analytics)(indexer) (audit)
Each group reads all partitions independently
  • Partition key determines ordering — same key → same partition
  • Consumer group: one consumer per partition for parallel scale
  • Replay: reset offset to reprocess history for new downstream
  • Schema registry (Avro/Protobuf) for evolution without breaking consumers

Delivery Guarantees

Delivery semantics define whether messages can be lost or duplicated. Exactly-once is the holy grail but expensive; most production systems achieve 'effectively once' via at-least-once delivery plus idempotent consumers.

GuaranteeMeaningConsumer requirement
At-most-onceFire and forget; may lose messagesTolerate loss (metrics, logs)
At-least-onceRetry until ack; may duplicateIdempotent processing or dedup
Exactly-onceProcess once and only onceTransactional outbox, Kafka EOS, or idempotency keys
  1. Producer acks: wait for leader (acks=1) or all replicas (acks=all)
  2. Consumer commit offset after successful processing (not before)
  3. Transactional producer + read-process-write in one Kafka transaction
  4. Outbox pattern: write business row + event in same DB transaction

Exactly-once is hard

True exactly-once across services requires distributed transactions or careful idempotency. In interviews, say 'at-least-once with idempotent handlers' unless the problem demands stronger guarantees.

Idempotency & Retries

Idempotent operations produce the same result whether executed once or multiple times. Retries with exponential backoff and jitter are essential in distributed systems where timeouts don't distinguish failure from slow success.

PatternImplementationUse case
Idempotency keyClient sends UUID; server dedups in storePayment POST, order creation
Natural idempotencyPUT with same body overwritesResource updates by ID
Upsert / ON CONFLICTDB merges duplicate insertsEvent ingestion
Dedup tableTrack processed message IDs with TTLQueue consumers
  • Exponential backoff: 1s, 2s, 4s, 8s… cap at max delay
  • Full jitter: randomize delay to prevent synchronized retries
  • Retry budget: limit total retries to avoid retry storms
  • Retry only idempotent operations or those with idempotency keys

Design idempotent payment API

Client sends Idempotency-Key header. Server checks Redis/DB for key → return cached response if exists. Otherwise process, store result with key, TTL 24h. Mention PCI and double-charge prevention.

Rate Limiting & Circuit Breakers

Rate limiting protects services from overload by throttling requests per user, IP, or API key. Circuit breakers stop calling a failing dependency, fail fast, and periodically probe for recovery — preventing cascade failures.

AlgorithmMechanismBurst handling
Token bucketTokens refill at fixed rate; request consumes tokenAllows bursts up to bucket size
Leaky bucketFixed outflow rate; queue or drop excessSmooth output rate
Fixed windowCount requests per time windowBoundary spike (2× at edges)
Sliding window logTimestamp log per request in windowAccurate; more memory
Sliding window counterWeighted previous + current windowGood Redis approximation
  • Circuit states: Closed (normal) → Open (fail fast) → Half-open (probe)
  • Open after N failures or error rate threshold in sliding window
  • Bulkhead: isolate thread pools per dependency — one slow service can't starve others
  • Return 429 Too Many Requests with Retry-After header
Circuit breaker state machine
  CLOSED ──(failures > threshold)──▶ OPEN
    ▲                                  │
    │                                  │ (timeout expires)
    │                                  ▼
    └──(probe succeeds)──────── HALF-OPEN
         (probe fails → OPEN)

Distributed rate limiter

Redis INCR with EXPIRE for fixed window. For accuracy use sliding window log in Redis sorted set. Mention per-user vs global limits and rate limit at API gateway edge.

Consensus & Leader Election Intro

Consensus algorithms (Raft, Paxos) let distributed nodes agree on a single value or leader despite failures. Leader election ensures exactly one primary handles writes at a time, enabling strong consistency and coordinated decisions.

AlgorithmApproachUsed in
RaftLeader election + log replication; understandableetcd, Consul, CockroachDB
PaxosMulti-round voting; theoretically provenChubby (Google), early ZK
ZAB (ZooKeeper)Atomic broadcast for coordinationZooKeeper, Kafka (old controller)
Bully / ringSimple election for homogeneous nodesLess common in production DBs
  • Raft: follower timeout → candidate → request votes → leader appends log entries
  • Quorum (majority) required for election and commit — tolerate f failures with 2f+1 nodes
  • Split-brain prevented by requiring majority to hold leadership
  • etcd/Consul provide distributed locks, service discovery, and config

Interview depth

You rarely implement Raft — you use etcd, ZooKeeper, or a managed DB with built-in consensus. Explain why leader election matters for 'single writer' consistency and failover.