Chapter 1: System Design
System design is about tradeoffs, not optimal solutions: every choice has costs, and the skill is in naming them. Almost every design problem reduces to three questions. What are the access patterns? What are the scale requirements? Which consistency and availability tradeoffs are acceptable? The answers to those three questions determine most of the architecture, and the rest of this chapter is about asking them well.
Concepts from other sections (19)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Design review: a reader who did not write the document checking it against a rubric and returning comments and a decision. Read more: 1.1 Design review: the tracer and the serving platform
- Control plane: the API server, etcd, scheduler, and controller manager, which store desired state and decide what should run where; worker nodes carry out those decisions. Read more: Chapter 10: Kubernetes Workloads and Deployment
- Design document: a description of a system or change, before or while it is built, holding many decisions and the arithmetic that ties them together. Read more: 1.1 Design review: the tracer and the serving platform
- Consistent hashing: placing nodes and keys on a ring so membership changes move minimal data. Read more: Chapter 6: Distributed Data and Caching
- reverse proxy: a server that forwards each request to an upstream and relays the response. Read more: 1.6 Go: packages, interfaces, goroutines, channels, context, net/http, testing in Programming and Algorithms
- Network partition: a split that leaves some replicas unable to reach others. Read more: 12.13 A Raft partition (optional)
- Quorum: requiring write and read acknowledgements out of replicas, with for overlapping reads and writes. Read more: Chapter 6: Distributed Data and Caching
- Time travel: querying a past snapshot of a table. Read more: Chapter 2: Storage and Warehousing in Data Engineering
- Exactly-once: the guarantee that each record affects state exactly once. Read more: Chapter 3: Batch and Streaming Processing in Data Engineering
- Effectively-once processing: at-least-once delivery combined with an idempotent effect. Read more: Chapter 13: Case Study: Exactly-Once Event API
- Token bucket: a counter capped at that refills at per minute and admits a request when it holds the request’s cost. Read more: 10.2 Rate Limiting: RPM Token Buckets and TPM Reserve/Settle in Software Engineering
- Sliding window: a contiguous range of an array or string whose two ends move only forward, so each element enters and leaves once and the whole scan is . Read more: Chapter 4: Two pointers and sliding window in Programming and Algorithms
- API key: a credential
tl_<id>_<secret>whose public id locates a record and whose secret proves possession. Read more: 10.1 API Keys, Scopes, Model Allowlists, and Tenants in Software Engineering - Idempotency key: an identifier stable across attempts (
<workflow id>/<activity id>) that lets an effect detect and skip or resume a duplicate. Read more: 1.2 Design review: the control plane - Idempotency: the property that applying an operation more than once has the same effect as applying it once. Read more: Chapter 3: RPC and Protocols
- Inverted index: a map from each term to the sorted list of documents that contain it. Read more: Chapter 8: Search and Indexing
- TF-IDF: a lexical weighting that scores a term by its frequency in a document times its rarity across the corpus, so distinctive words count more than common ones; BM25 refines it. Read more: Chapter 27: Retrieval and RAG in AI Engineering
- Fan-in / fan-out: the number of inputs feeding one output, and the number of outputs fed by one input. Read more: 13.4 Variance propagation and initialization in Mathematics
- Cross-layer finding: a problem that belongs to no single layer, such as a shared resource or a decision that depends on another layer’s numbers. Read more: 1.3 Design review: the whole system
The chapter has two halves. This opening surveys the building blocks you combine to answer those questions: scaling, databases, distributed-systems primitives, messaging, and API design, followed by the classic design problems that exercise them. The three sections that follow put the building blocks to work on a real system. Each is a design review of the system built across these books, judged against the same eight-line rubric: first the serving platform, then the control plane, then the whole system. Every design starts with requirements (functional and non-functional) and back-of-envelope arithmetic, and the reviews show what that looks like when the arithmetic is wrong by a factor of sixteen.
System design is also the interview topic asked at every Staff+ level, and those interviews test your ability to navigate ambiguity and make justified decisions rather than recall memorized architectures. The same is true of a real design review.
Learning objectives
- Choose between vertical and horizontal scaling, caching layers, and sharding schemes for a given access pattern.
- Explain CAP, replication topologies, partitioning, consensus, and distributed transactions, and when each applies.
- Pick a messaging model, an API style, a rate-limiting scheme, and a pagination strategy with stated tradeoffs.
- Write and review a design document against an eight-line rubric, with capacity and error-budget arithmetic.
- Trace failure modes across layers and back each detection with evidence.
Before you begin
Section titled “Before you begin”The building blocks assume the data structures and analysis from Programming and Algorithms. The design reviews additionally assume you know the system they review: the serving engine from AI Engineering, the gateway and durable control plane from AI Engineering, and the deployment and observability from Systems Engineering and the Observability chapter of this book. Each review lists exactly which sections it draws on.
Scaling
Section titled “Scaling”There are two ways to add capacity. Vertical scaling means a bigger machine: simple, but it has a ceiling, and it is a good fit for databases. Horizontal scaling means more machines: more complex, but effectively unlimited, and a good fit for stateless services. Once you have more than one instance, a load balancer spreads traffic across them, using round-robin, least connections, or consistent hashing, at layer 4 (TCP) or layer 7 (HTTP).
Caching sits at every level of the stack: browser, CDN, reverse proxy, application, database. Adding a cache is easy; cache invalidation is the hard part. A CDN serves static content from edge locations, either pulling from the origin on demand or having content pushed to it, and is controlled by Cache-Control headers. When a single database can no longer hold the data or the write load, you shard it, partitioning horizontally by range, by hash, or through a directory. Resharding later is painful, so the choice deserves care up front.
All of this rests on back-of-envelope numbers you should know cold: QPS for common services (100K to 1M), storage per record (about 1 KB), monthly storage growth, and the read/write ratio.
Databases
Section titled “Databases”SQL databases give you ACID transactions, joins, and strong consistency; PostgreSQL is the default choice. NoSQL stores trade those for BASE semantics, eventual consistency, and horizontal scaling, and come in several shapes: key-value (DynamoDB), document (MongoDB), wide-column (Cassandra), and graph (Neo4j). The rule of thumb is SQL for transactions and complex queries, NoSQL for massive scale, flexible schemas, or specific access patterns.
The CAP theorem frames the central tradeoff: during a network partition, you choose consistency (CP, refuse stale reads) or availability (AP, serve stale reads). You cannot have both. Replication comes in three topologies: single-leader (simple, one write path), multi-leader (for multiple data centers), and leaderless (Dynamo-style quorum reads and writes). Partitioning by range is good for scans but risks hot spots; partitioning by hash spreads load evenly but gives up range queries.
Distributed-systems primitives
Section titled “Distributed-systems primitives”Beyond a single database, a handful of primitives recur. Consensus protocols guarantee agreement among nodes: Raft is the understandable one, Paxos the original, and they underlie etcd and ZooKeeper. Distributed transactions come in two styles: two-phase commit, which blocks and makes its coordinator a single point of failure, and sagas, which use compensating transactions and accept eventual consistency. Vector clocks and Lamport timestamps establish causal ordering without synchronized clocks. Consistent hashing distributes keys across nodes so that adding or removing a node remaps only about of the keys; DynamoDB and Cassandra use it. Gossip protocols propagate state epidemic-style until every node has it, as in Cassandra and SWIM.
Messaging and events
Section titled “Messaging and events”Message queues decouple producers from consumers. Kafka is log-based and ordered, RabbitMQ is a traditional broker with rich routing, and SQS is managed. Two patterns build on them. Event sourcing stores events rather than state and rebuilds state by replaying them, which gives you an audit log and time travel for free. CQRS separates the read and write models: writes go to an event store and are projected into read-optimized views. Exactly-once delivery is impossible in general; you achieve effectively-once processing with idempotent consumers plus deduplication, which is exactly the pattern the control-plane review checks.
API design
Section titled “API design”REST is resource-oriented, uses HTTP verbs, and is stateless; it suits CRUD but suffers from over-fetching and under-fetching. gRPC uses a binary protocol (protobuf) with streaming and strong typing, and suits internal service-to-service calls. GraphQL lets the client specify exactly the data it needs, which helps with complex nested data at the cost of complexity in server-side resolvers.
Three cross-cutting concerns apply to any style. Rate limiting uses a token bucket or a sliding window, keyed per user, per IP, or per API key, and returns 429 with a Retry-After header. Idempotency for POST uses an idempotency key, which is critical for payments: in Stripe’s approach the client generates the key and the server deduplicates. Pagination should be cursor-based, which stays consistent under concurrent writes, rather than offset-based, which skips items when the data changes underneath it; keyset pagination is the database form.
Classic design problems
Section titled “Classic design problems”The classic problems are worth studying two or three a week, each the same way: estimate scale first, then design, then discuss tradeoffs, and practice explaining the result in fifteen minutes. Each one stresses a different combination of the building blocks above.
A URL shortener turns on hash-based versus counter-based ID generation, base62 encoding, read-heavy caching, and an analytics pipeline. A chat system such as WhatsApp needs WebSocket connections, a message queue per user, delivery receipts, a presence service, and end-to-end encryption. A news feed such as Twitter’s uses fan-out on write (push) for users with few followers and fan-out on read (pull) for celebrities, and in practice a hybrid of the two. Video streaming such as YouTube needs an upload pipeline that transcodes to multiple resolutions, a CDN for delivery, and adaptive bitrate streaming (HLS or DASH). A payment system such as Stripe’s depends on idempotency, exactly-once processing, a double-entry ledger, reconciliation, and PCI compliance. A key-value store combines consistent hashing, replication, quorum reads and writes, conflict resolution (last-writer-wins or vector clocks), and compaction. A search engine needs a crawler, an inverted index, ranking (TF-IDF, then PageRank, then learned models), query processing, and caching.
Different companies favor different problems, which tells you where their systems hurt. The following table summarizes the usual pattern.
| Company | Favorite problems | Focus |
|---|---|---|
| Distributed systems, search, Bigtable-style storage | Scale, consistency | |
| Amazon | E-commerce, inventory, microservices | Availability, partition tolerance |
| Meta | Social graph, news feed, real-time messaging | Fan-out, caching |
| Netflix | Streaming, recommendation, resilience | CDN, fault tolerance |
| Stripe | Payment processing, ledger, API design | Correctness, idempotency |
| Anthropic | ML inference serving, API rate limiting | GPU scheduling, latency |
| Uber | Real-time dispatch, geospatial indexing | Low latency, high throughput |
Choosing a building block
Section titled “Choosing a building block”Each building block solves one problem and introduces another. The following table pairs each with when to use it and what it costs.
| Building block | Use when | Tradeoff |
|---|---|---|
| Load balancer | Multiple instances of a service | Added latency, complexity |
| Cache (Redis) | Read-heavy, latency-sensitive | Stale data, cache invalidation |
| Message queue | Async processing, decoupling | Eventual consistency, ordering |
| CDN | Static content, global users | Cache invalidation, cost |
| SQL database | Transactions, complex queries | Scaling writes is hard |
| NoSQL database | Massive scale, simple access patterns | No joins, eventual consistency |
| Consistent hashing | Distributed data partitioning | Uneven load with few nodes |
| Consensus (Raft) | Distributed state machine | Latency, availability during partition |
In this chapter
Section titled “In this chapter”- Design review: the tracer and the serving platform: the eight-line rubric, the capacity and error-budget arithmetic a serving design needs, six worked problems, and a model design document with its review.
- Design review: the control plane: what is persisted before each external effect, traced through a corpus build and an approved agent write, and a review that turns every missing answer into an owned note.
- Design review: the whole system: a request and a model release traced through every layer, with failure modes backed by drills and the cross-layer findings no single review owned.
Key terms
Section titled “Key terms”- Vertical scaling: adding capacity by moving to a bigger machine; simple, but it has a ceiling, and it suits databases.
- Horizontal scaling: adding capacity by adding machines; more complex but effectively unlimited, and it suits stateless services.
- Load balancer: a component that spreads traffic across instances of a service by round-robin, least connections, or consistent hashing, at layer 4 (TCP) or layer 7 (HTTP).
- Content delivery network (CDN): a network of edge servers that serves static content close to users, pulling from or pushed by the origin and controlled by
Cache-Controlheaders. - CAP theorem: during a network partition, a replicated store must choose consistency (refuse possibly stale reads) or availability (serve them); it cannot have both.
- Single-leader / multi-leader / leaderless replication: the three replication topologies: one node accepts all writes; several nodes accept writes, typically one per data center; or any node accepts writes with Dynamo-style quorum reads and writes.
- Consensus protocol: a protocol such as Raft or Paxos that makes a group of nodes agree on one value or log despite failures; it underlies etcd and ZooKeeper.
- Two-phase commit (2PC): a distributed transaction protocol in which a coordinator asks every participant to prepare and then to commit; it blocks if the coordinator fails, which is why sagas are often preferred across services.
- Vector clock: a per-node array of counters attached to events that tells whether one event causally precedes another or the two are concurrent, without synchronized clocks.
- Lamport timestamp: a single logical counter, incremented on each event and advanced past any received timestamp, that gives a total order consistent with causality.
- Gossip protocol: nodes repeatedly exchange state with random peers so information spreads epidemic-style to the whole cluster, as in Cassandra and SWIM membership.
- Command query responsibility segregation (CQRS): separate models for writes and reads, where writes go to an event store or write model and are projected into read-optimized views.
- Cursor-based pagination: paging with an opaque cursor that names the last item seen (keyset pagination in a database), which stays consistent under concurrent writes where offset pagination skips or repeats items.
- Fan-out on write / fan-out on read: two ways to build feeds: push each post into every follower’s feed when it is written, or pull from followed accounts when a feed is read; large systems combine them, pulling for accounts with huge followings.
Further reading
Section titled “Further reading”- Martin Kleppmann, Designing Data-Intensive Applications: the deepest treatment of the building blocks, worth reading cover to cover.
- ByteByteGo System Design, for worked interview-style problems.
- The System Design Primer (free).