Notes
All notes

What is System Design? (System Design #1)

“Design Twitter.” Three words, a whiteboard, and forty-five minutes. Most candidates freeze not because they don’t know what a load balancer is, but because there is no obvious first move. The question has no single right answer, only trade-offs — and that is exactly the point.

System design is the discipline of choosing components and how they talk to each other so that a set of requirements is met at a target scale. This chapter gives you the opening move that every later chapter builds on: turn a vague prompt into requirements, estimate the load, sketch the parts, then go deep on the hard parts. Along the way you will learn the numbers that anchor every estimate.

What system design actually is

A system design answer is not a diagram. It is a chain of decisions, each justified by a requirement:

\[ \text{design} = f(\text{functional requirements},\ \text{non-functional requirements},\ \text{constraints}) \]

Notice the asymmetry. Functional requirements are usually easy to satisfy; non-functional requirements are where the design is won or lost. A URL shortener that serves 10 requests/second needs one box. The same shortener at 100,000 requests/second is a completely different system. Same function, different design — because the scale changed.

\begin{tikzpicture}[font=\rmfamily, >=stealth,
  b/.style={draw, thick, rounded corners=4pt, minimum width=2.4cm, minimum height=0.8cm, align=center, font=\rmfamily\small},
  a/.style={->, thick}]
  \node[b, fill=blue!10] (f) at (-3.4,0) {functional\\what it does};
  \node[b, fill=orange!16] (n) at (0,0) {non-functional\\how well};
  \node[b, fill=green!12] (c) at (3.4,0) {constraints\\team, budget};
  \node[b, fill=gray!12] (d) at (0,-1.9) {a design that satisfies all three};
  \draw[a] (f) -- (d); \draw[a] (n) -- (d); \draw[a] (c) -- (d);
\end{tikzpicture}

The approach: a repeatable opening move

Every design question, from “design a chat app” to “design a rate limiter”, follows the same five steps. Do them in order, out loud.

  1. Clarify requirements. Ask functional and non-functional questions. “How many users? Read-heavy or write-heavy? Is eventual consistency acceptable? What’s the latency budget?” A design that ignores these is a guess.
  2. Estimate scale. Turn users into QPS, bytes, and servers. This tells you which problems are real.
  3. Sketch the high-level design. Draw clients, an entry point (load balancer/DNS), stateless app servers, data stores, and caches. Five to seven boxes.
  4. Deep-dive the bottlenecks. The database is usually the bottleneck; so are hot keys, big writes, and fan-out. Go deep where the estimate says the pain is.
  5. State trade-offs. “I chose X over Y because Z; if the requirement changed to W, I would switch.” This is the actual job.
\begin{tikzpicture}[font=\rmfamily, >=stealth,
  b/.style={draw, thick, rounded corners=4pt, minimum width=2.1cm, minimum height=0.72cm, align=center, font=\rmfamily\small},
  a/.style={->, thick}]
  \node[b, fill=blue!10] (r) at (-2.4,1.3) {1. requirements};
  \node[b, fill=cyan!15] (e) at (-2.4,0) {2. estimation};
  \node[b, fill=orange!16] (h) at (-2.4,-1.3) {3. high-level};
  \node[b, fill=green!12] (b2) at (1.6,0) {4. deep-dive};
  \node[b, fill=yellow!22] (t) at (1.6,-1.3) {5. trade-offs};
  \draw[a] (r) -- (e); \draw[a] (e) -- (h); \draw[a] (h) -- (b2); \draw[a] (b2) -- (t);
\end{tikzpicture}

Know your numbers

Back-of-envelope estimation is arithmetic on a handful of constants. Memorize the orders of magnitude, not the exact digits:

Operation Latency Note
L1 cache reference ~1 ns
Main memory reference ~100 ns 100× L1
SSD random read ~150 µs
Disk seek (spinning) ~10 ms 100,000× memory
Same-datacenter round trip ~0.5 ms
Cross-region round trip ~50–150 ms geography is fate

Three consequences you will reuse constantly. A disk seek costs ~100,000 memory accesses — this is why caches, B-trees, and append-only logs exist. Cross-region calls dominate latency — this is why CDNs and multi-region reads exist. The network is not free — batching and connection reuse matter.

The second set of constants is throughput and size:

Quantity Order of magnitude
One SSD ~500 MB/s, ~100K IOPS
One modern server ~10K–100K simple QPS
One Postgres primary ~5K–20K writes/s (varies hugely)
Redis ~100K+ ops/s
1 million requests/day ~12 requests/s average
1 billion requests/day ~12,000 requests/s average

Note the last two: divide daily counts by ~86,400 (round to 100,000) to get average QPS, then multiply by 2–5 for peak. A “million users” app is often only a few hundred QPS.

# back-of-envelope helper — the first code you write in any design
def estimate(dau, actions_per_user=10, avg_obj_bytes=500, retention_days=365*3):
    qps_avg = dau * actions_per_user / 86_400
    qps_peak = qps_avg * 3
    storage = dau * actions_per_user * avg_obj_bytes * retention_days
    return {
        "qps_avg": round(qps_avg, 1),
        "qps_peak": round(qps_peak, 1),
        "storage_TB": round(storage / 1e12, 2),
    }

print(estimate(10_000_000))     # 10M DAU, 10 actions: ~1157 QPS avg, ~55 TB over 3 years
print(estimate(100_000, 20))    # small app: tens of QPS, ~11 TB

Run it and the mystery evaporates: most “web-scale” problems are a few thousand QPS and a few terabytes, which is one well-chosen database plus a cache. Only when a number crosses ~100K QPS or ~100 TB do sharding and multi-region become mandatory.

The shape of every large system

Different products converge on the same skeleton. Learn it once; adapt it forever:

client → DNS → CDN/edge → load balancer → stateless app servers → cache → database
                                                  ↓
                                            queue → workers → external services
\begin{tikzpicture}[font=\rmfamily, >=stealth,
  b/.style={draw, thick, rounded corners=3pt, minimum width=1.5cm, minimum height=0.68cm, align=center, font=\rmfamily\small},
  a/.style={->, thick}]
  \node[b, fill=gray!10] (c) at (-5.4,0) {client};
  \node[b, fill=gray!10] (dns) at (-3.6,0) {DNS};
  \node[b, fill=blue!10] (cdn) at (-1.8,0) {CDN};
  \node[b, fill=orange!16] (lb) at (0.1,0) {LB};
  \node[b, fill=green!12] (app) at (2.2,0) {app ×N};
  \node[b, fill=cyan!15] (cache) at (4.3,0.9) {cache};
  \node[b, fill=yellow!22] (db) at (4.3,-0.9) {database};
  \node[b, fill=red!10] (q) at (4.3,-2.3) {queue};
  \draw[a] (c) -- (dns); \draw[a] (dns) -- (cdn); \draw[a] (cdn) -- (lb); \draw[a] (lb) -- (app);
  \draw[a] (app) -- (cache); \draw[a] (app) -- (db); \draw[a] (app) -- (q);
\end{tikzpicture}

Every later chapter zooms into one box: DNS/CDN (#7), load balancing (#8), app layer (#9), databases (#10–11), cache (#12), queues (#13), communication (#14), monitoring (#15).

A worked mini-example

Prompt: “Design a service that lets users upload photos and view a feed.”

  1. Requirements. Functional: upload, follow, feed. Non-functional: feed reads ≫ uploads; reads should be fast globally; losing a photo is worse than a briefly stale feed.
  2. Estimate. 5M DAU, 1 upload/week each → ~8 uploads/s average (peak ~25). 50 feed views/day each → ~2,900 reads/s (peak ~9,000). Photos ~2 MB → ~500 TB over 3 years. Reads dominate ~360:1.
  3. High-level. CDN for photo bytes, stateless app servers behind an LB, object store for blobs, database for metadata, cache for hot feeds.
  4. Deep-dive. Feed generation is the hard part: fan-out on write (precompute each follower’s feed) versus fan-out on read (query at request time). For 5M users, fan-out on write with a queue is the standard choice, with a hybrid for celebrity accounts.
  5. Trade-offs. Chose eventual consistency for the feed (fast, available) and strong consistency for upload confirmation (don’t lose data). CDN cost purchased latency.

That is a complete design conversation in five moves — no framework, just requirements driving decisions.

How this course is organized

Eighteen chapters, mirroring roadmap.sh/system-design:

Each chapter ends with pitfalls and a self-check, and points forward.

Pitfalls

QQuestionSelf-check

What are the five steps of the approach? Why do non-functional requirements drive the design more than functional ones?

ASolution

Clarify requirements, estimate scale, sketch the high-level design, deep-dive the bottlenecks, state trade-offs. Functional requirements are usually easy to satisfy; non-functional ones (scale, latency, availability, consistency, cost) force the hard architectural choices, and they change with the numbers.

Next: Scalability, Performance, Latency and Throughput (System Design #2) — the words we have been using need precise definitions.