09. Whole Systems: cheatsheets

4 lessons, one page each when printed.

System Design · 09. Whole Systems · 10 min · Intermediate

HLD & LLD

Turn a vague brief into boxes, numbers, then classes and tables.

In short

Ask for requirements, estimate (1,000,000 pastes a day → 384 requests a second at peak → 3.65 TB a year → 2 servers), draw the high-level design, dive into the risky parts, then show the inside of one part as an API, a class and a table.

The next three lessons do exactly this for a URL shortener, a news feed and a chat system.

Key ideas

  • Good design starts with questions, not boxes. (step 1)
  • The numbers decide the design: this is small enough for one database; ten times the reads would not be. (step 3)
  • HLD answers "which parts?". LLD answers "what exactly is inside one part?" (step 6)

Good to know

  • Two minutes of questions can remove an hour of wrong design. (step 2)
  • Name each box's one job: balance, run the logic, remember hot data, keep the truth. (step 4)
  • Use the form: "I chose X because of Y; if Z grows, I would move to W." (step 7)

Watch out

  • Say why you picked these parts. The reason shows your thinking more than the answer does. (step 5)
  • The common failure is spending 30 minutes on one clever box and never drawing the whole system. (step 8)
UsersLoad balancerApp serverCacheExpiry jobsApp server 2Database

Key numbers

Peak requests
384/s
Storage a year
3.65 TB
App servers
2

Terms

HLD (high-level design)
The big picture: the main parts of a system and how they talk to each other.
LLD (low-level design)
The inside of one part: its classes, API and tables.
Functional requirements
What the system does, such as creating and reading a paste.
Non-functional requirements
How well it must do it: speed, uptime, size.
Scope
The list of things you decide not to build, so the problem stays small.
Back-of-the-envelope estimate
Quick maths from users to requests, storage and servers to size a design.
Deep dive
A closer look at the riskiest part of a design.
Trade-off
What you give up to get something else, such as freshness for speed.

System Design · 09. Whole Systems · 14 min · Intermediate

Design a URL Shortener

Turn long links into short codes, and redirect billions of clicks fast.

In short

100,000,000 links a month means 39 writes and 11,700 reads a second at peak. A scrambled base62 counter gives 3.5 trillion unique codes; a cache cuts store lookups to 1,170 a second and redirects to 11.5 ms.

Shards hold the 3 TB, a queue counts clicks off the hot path, and a checker and rate limit stop abuse.

Key ideas

  • Requirements: unique short codes, fast redirects, links never silently change, and click counts. (step 1)
  • Unique is not the same as unguessable. Do not depend on a secret code alone for private data. (step 6)
  • Move work that the user does not wait for out of the request, onto a queue. (step 10)

Good to know

  • 7 characters is short, never collides, and has room for a thousand times more links. (step 5)
  • Read-heavy and rarely changed is the best case for a cache. (step 7)
  • Shard by the code: a random code spreads the rows evenly. (step 9)

Watch out

  • The read peak, not the storage, is the first problem. (step 2)
  • It works on a laptop and fails at the first busy hour. (step 3)
  • A truncated hash is short, but it is not unique. Collisions arrive much sooner than people expect. (step 4)
  • Browsers may cache a 301 for a very long time, so a wrong target is hard to fix later. (step 8)
  • Abuse protection belongs on the create path, which has only about 39 writes a second, not on the busy read path. (step 11)
UsersLoad balancerShortener serviceCacheClick logLink storeClick eventsShard 2Shard 3

Key numbers

Redirect time
11.5 ms
Store lookups/s
1,170/s
Short codes
3.5 T
Links kept
6 billion

Terms

Base62
Writing a number with 62 symbols (0–9, a–z, A–Z) so it takes few characters.
Hash collision
Two different inputs that give the same hash, so they would get the same short code.
Read-heavy workload
A system where reads far outnumber writes, so the read path is sized and cached first.
Guessable codes
Sequential codes let a stranger walk through other people's links; scramble or randomise them.
301 redirect
A permanent redirect that browsers may remember, so repeat clicks skip the server.
302 redirect
A temporary redirect: every click reaches the server, so it can be counted.
Click events queue
A queue that takes click records off the redirect path so counting never slows it.
Link checker
A check of each new long URL against known bad sites before a short link is made.

System Design · 09. Whole Systems · 14 min · Advanced

Design a News Feed

Build each person's home feed from the people they follow, fast.

In short

5,787 feed loads and 116 posts a second. Pull makes reads heavy (120 ms); push makes writes heavy and breaks on celebrities; the hybrid pushes normal accounts and pulls celebrities.

A feed and post cache, a small ranker and a CDN for photos bring the feed load to about 28 ms. Cursors keep scrolling stable.

Key ideas

  • Requirements: a fresh feed, fast to load, even for people with huge audiences. (step 1)
  • Pick the method per account: push where the audience is small, pull where it is huge. (step 7)
  • Metadata travels through our system; the heavy bytes travel through the CDN. (step 10)

Good to know

  • Feeds are read-heavy, so make the read path cheap. (step 2)
  • Always say the simple version first; then show where it breaks. (step 3)
  • Do the work once at write time instead of many times at read time. (step 5)
  • Cache the feed for people who visit often; rebuild it for people who come back after a year. (step 8)
  • Cursor pagination also stays fast deep in the list; big offsets make the database skip many rows. (step 11)

Watch out

  • The work happens at the worst moment: while the user waits. (step 4)
  • Fan-out size varies enormously: an average of 200 hides a few accounts with millions. (step 6)
  • Keep ranking cheap and bounded: score a few hundred candidates, never the whole feed. (step 9)
CDNUsersRankerFeed APIPost storeNew-post queueFan-out workersFeed + post cacheFeed lists

Key numbers

Feed load
28 ms
Writes/s
23,000/s
Fan-out size
200; celebrities 0

Terms

Fan-out
Spreading one action, such as a post, out to many places, such as every follower's feed.
Fan-out on read (pull)
Build a feed when it is opened by looking at each account the user follows.
Fan-out on write (push)
When a post is made, add it to every follower's ready-made feed list.
Celebrity problem
An account with millions of followers makes one post cause millions of feed writes.
Hybrid fan-out
Push posts of normal accounts, but pull the posts of celebrities when the feed is read.
Feed cache
Recent feed lists and popular posts kept in memory for fast reads.
Feed ranking
Scoring candidate posts and returning the best, rather than only the newest.
Cursor pagination
Asking for the next page by the last item seen, so the list does not shift as new posts arrive.

System Design · 09. Whole Systems · 14 min · Advanced

Design a Chat System

Deliver messages in milliseconds to people who come and go.

In short

23,148 messages a second and 10 million open connections: that needs 100 WebSocket gateways, a sessions store to find people, and a message store sharded by chat (73 TB a year). Delivery takes about 62 ms.

Receipts are small acks, groups store once and deliver many, presence uses expiring heartbeats, offline users get a push and then sync by last id, and order is kept per chat.

Key ideas

  • Requirements: fast delivery, no lost messages, correct order, groups, history, offline users. (step 1)
  • The sessions map is small (one entry per online user) and hot, so keep it in memory. (step 5)
  • Store once, deliver many. The delivery rate, not the send rate, sizes the gateways. (step 8)
  • Ordering is per chat, not global. A global order across all chats is expensive and not needed. (step 11)

Good to know

  • Messages are small. Holding millions of idle connections is the real job. (step 2)
  • An idle open connection is cheap; thousands of repeated requests are not. (step 4)
  • Shard by chat, not by message: reading a chat then needs only one shard. (step 6)
  • The store is the source of truth; delivery over the connection and push are only ways to tell Bob sooner. (step 10)

Watch out

  • Polling puts the cost on the quiet times and still delivers late. (step 3)
  • If the ack is lost, the server resends; the app must drop duplicates by message id. (step 7)
  • Presence is the cheapest feature to build and the easiest one to make expensive. (step 9)
AliceLoad balancerGateway 1BobMessage storeGateway 2Sessions (Redis)Offline queuePush service

Key numbers

Connections
10 million
Messages/s
23,148/s
Delivery time
62 ms

Terms

Polling
A client asking the server again and again whether anything is new.
WebSocket
A connection that stays open so the server can push messages to the client at any time.
Gateway server
A server that holds many open connections and passes messages in and out.
Sessions store
A fast map from a user to the gateway they are connected to.
Message store
Durable storage of every message, keyed by chat and message id.
Delivery receipt
A small acknowledgement showing a message was stored, delivered or read.
Group fan-out
Storing a group message once and delivering it to every member.
Heartbeat
A tiny periodic signal that shows a device is still online.
Push notification
A message sent through the phone's notification system to an offline user.
Sync after id
Asking for all messages newer than the last id a device has, to catch up.