Bitpath

System Design

Learn to turn "build us X" into requirements, numbers and a sketch of boxes that survives real traffic. You will estimate, pick caches, databases and queues for a reason, keep things up when parts fail, and finish by designing three real systems.

  • Advanced
  • 4 units, 12 lessons, 155 exercises
  • Diagrams, estimates
  • Builds on Networking, Databases, Distributed Systems

Unit 01 · 3 lessons

Before you draw boxes

Know what you are building and how big, before choosing any technology

  1. > start01Ask first
  2. 02Back-of-envelope
  3. BOSSThe standard web stack
Unit prize
  1. 01
    Ask first

    Turn a vague ask into requirements and targets

  2. 02
    Back-of-envelope

    Turn users into requests per second and bytes per year

  3. BOSS
    The standard web stack

    Know every box between the browser and the database

Read the unit guide

A design starts with questions, not boxes. Functional requirements say what the system does: post a photo, follow a user, read a feed. Non-functional requirements say how well: how fast (latency), how often it is up (availability), how fresh the data must be (consistency), how much it must carry (scale).

Two questions shape almost everything. Is it read-heavy or write-heavy? A system with 100 reads per write wants caches and replicas; one with mostly writes wants queues and partitions. And which data must be exactly right? A like count can be a few seconds stale. A payment cannot.

Then put numbers on it. A day has 86,400 seconds: call it 10⁵. So a million requests a day is about 10 per second, and 100 million a day is about 1,000 per second. Storage is items × size × time. Bandwidth is requests per second × response size. Plan for the peak, which is often several times the average, and leave headroom.

Keep a few latencies in your head: memory is nanoseconds, an SSD read is microseconds, a round trip inside a datacenter is about half a millisecond, a round trip across an ocean is about 100 milliseconds. Distance and round trips are what make systems slow.

The standard web stack is the starting sketch for almost any design: DNS finds the address, a CDN serves static files near the user, a load balancer spreads requests over stateless app servers, a cache holds hot answers, the database holds the truth, a queue holds work for later, and object storage holds files. Know what each box is for, and add one only when a number says you need it.

Unit 02 · 3 lessons

The data layer

Make reads fast, make the database last, and take slow work off the request

  1. 01Caching
  2. 02Scaling the database
  3. BOSSDon't make the user wait
Unit prize
  1. 01
    Caching

    Serve hot reads from memory without serving lies

  2. 02
    Scaling the database

    Indexes, a bigger box, replicas, then shards, in that order

  3. BOSS
    Don't make the user wait

    Move slow work to a queue, and survive doing it twice

Read the unit guide

Most designs succeed or fall over at the data layer. The app servers are easy to multiply; the data is not.

A cache keeps answers in memory so the database is not asked again. The common pattern is cache-aside: look in the cache, on a miss read the database and store the result. Every entry needs a way to die: a TTL as a safety net, and a delete whenever the data is written. Decide per kind of data how stale is acceptable. When the cache is full it evicts, usually the least recently used entry. When a hot entry expires, let one request rebuild it, not five thousand.

Scale the database in order of pain. First fix queries and add indexes. Then buy a bigger machine. Then add read replicas, and remember they lag: a user may not see their own write unless you read it from the leader. Only then partition the data across machines, by a key that keeps related rows together and spreads the load. After that, any query that crosses partitions is expensive.

Pick the store per workload. Money and anything with relationships wants a relational database with transactions. Lookups by one key want a key-value store. Loosely shaped records want a document store.

A queue takes slow work off the request: save the minimum, put a job on the queue, reply, and let workers do the rest. It also absorbs spikes. Queues deliver at least once, so a worker must be safe to run twice on the same job. Jobs that keep failing go to a dead-letter queue. Watch the depth: a queue that only grows is hiding a capacity problem.

Unit 03 · 3 lessons

Staying up

Spread load, survive failures, and know what the system is doing

  1. 01Load balancing
  2. 02Counting nines
  3. BOSSThe 2 a.m. page
Unit prize
  1. 01
    Load balancing

    Spread requests, skip dead servers, keep servers stateless

  2. 02
    Counting nines

    Do the arithmetic of uptime, then design for failure

  3. BOSS
    The 2 a.m. page

    Follow an incident from the alert to the cause

Read the unit guide

Everything fails eventually: a server, a disk, a whole datacenter. Reliable systems are built so that one failure is an event in a log, not an outage.

A load balancer spreads requests over servers and stops sending to the ones that fail their health checks. Round robin takes turns; least connections sends to the least busy server when requests differ in cost; hashing sends the same key to the same server. A layer 4 balancer sees only addresses and ports. A layer 7 balancer reads the HTTP request and can route by path. Keep app servers stateless, so any of them can be removed, replaced or added by autoscaling without anyone noticing.

Availability is counted in nines. 99.9% allows about 9 hours of downtime a year; 99.99% about 53 minutes. Parts that a request needs one after another multiply, so three 99.9% parts give about 99.7%. Parts in parallel, where any one will do, multiply their failure chances instead: two independent 99% servers give 99.99%. Hunt for single points of failure: any box with no spare.

When a part is failing, protect the rest. Rate limits stop one client from starving the others. Short timeouts and fallbacks let a page load without its optional parts. Run with headroom, so that the survivors can carry a failed server's load.

You cannot fix what you cannot see. Metrics say that something is wrong, traces say where the time went, logs say why. Watch percentiles, not averages: the p99 is what your unluckiest users feel. Write the target down as an SLO, treat the gap to 100% as an error budget, and page a human only for symptoms users can feel. In an incident, restore service first and find the root cause second.

Unit 04 · 3 lessons

Three designs

Run the same four steps on three real systems

  1. 01Design a URL shortener
  2. 02Design a news feed
  3. BOSSDesign a chat app
Unit prize
  1. 01
    Design a URL shortener

    Short codes, a fast redirect, and ids that do not collide

  2. 02
    Design a news feed

    Push or pull posts, tame the star account, page with cursors

  3. BOSS
    Design a chat app

    Open connections, ordered messages, and offline users

Read the unit guide

Every design question gets the same four steps: requirements, estimates, a sketch, then the bottlenecks. The three systems here are classics because each one is dominated by a different force.

A URL shortener is a map from a short code to a long URL, read far more often than written. Codes come from a counter written in base 62 (compact, but guessable) or from random characters checked against a unique constraint (unguessable, with a rare retry). The redirect path is cache first, database second. A 302 keeps every click visible to you; a 301 lets browsers skip you.

A news feed is about fan-out. Fan-out on read builds the feed when it is opened: cheap posts, slow reads. Fan-out on write pushes each post into every follower's stored feed: fast reads, and a star with millions of followers becomes a flood. Real systems mix them: push for ordinary accounts, pull the stars at read time and merge. Page with a cursor ("older than this id"), not an offset, so new posts cannot shift the pages.

Chat is about connections and order. Clients hold a WebSocket to a gateway server, and a shared registry records which gateway holds whom. The server stores a message before pushing it, numbers messages per conversation so that order never depends on clocks, and keeps them for anyone offline. A client that reconnects asks for everything after the last number it saw.

In all three, ask what breaks first as the numbers grow: the hot key, the star account, the gateway that dies with a hundred thousand sockets. Then fix that, and ask again.

Learn System Design on your phone.

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy