Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Five more, worked end to end

Five classic whole-system questions, each worked end to end in its own lesson: a crawler that has to be polite, clicks that are counted live and billed exactly, a booking rush where demand is two thousand times supply, a scheduler that must not run a job twice, and a monitoring system that a single label can sink. The trap in every one is drawing boxes before saying the number that decides the design.

Questions

Answer each one yourself before you open it.

  1. 01Design a web crawler that fetches a billion pages a month without overloading any site.

    A billion pages a month is about 386 pages a second. Keep a URL frontier with one queue per host, and a politeness gate that allows one connection per host with a delay between requests. Cache robots.txt and DNS per host, check new links against a Bloom filter, hash page bodies to catch copies, and give each page a recrawl time that follows how often it changes.

    Politeness makes the number of hosts ready at once the limit, not fetcher speed: at one request per host per second you need at least 386 hosts in flight. A Bloom filter holds the URL-seen set for 10 billion URLs in about 12 GB at a 1% false-positive rate, where an exact set of 8-byte hashes is 80 GB. The cost is that about one new page in a hundred is wrongly skipped. Caps on URL length, depth and pages per host stop crawler traps.

    Follow-up How do you scale the frontier, and how would you recrawl news sites within minutes?

    Partition the frontier by hash(host) mod N, so one host always lives on one machine and politeness is a local decision with no cross-machine coordination. For news, add a second lane ordered by due time: 10,000 news pages every 5 minutes is about 33 pages a second, roughly 9% of the budget. Use If-Modified-Since, feeds and sitemaps to skip waste, and keep the same per-host delay.

    Learn more Design a web crawler · Google Search case study

  2. 02Design a system that counts ad clicks per ad per minute in real time, and also gives the top 100 ads in the last minute.

    Write every click to a log partitioned by ad id and give each click a unique click id. A stream job counts per ad in one-minute tumbling windows, closes a window when the watermark passes it, and skips repeat click ids. The dashboard reads that fast path. Billing comes from a nightly batch job that reads the same log and counts exactly. For top 100, each partition keeps a heap of its best 100 and one merger combines them.

    The log is the source of truth, so the fast path may be approximate and any bug is fixed by replay. Partitioning by ad id means each ad is complete in one lane, so merging the per-partition top 100 lists is exact. Dedup by click id turns at-least-once delivery into a correct count without needing exactly-once. Reconciling fast against exact tells you how good the fast path is.

    Follow-up One ad gets 2% of all clicks and its partition falls behind. What do you do?

    Spread that ad across several lanes by adding a small random suffix to its key, count each lane separately, then add the counts, since a count is a sum. At 34,722 clicks a second peak, 2% is 694 a second, more than the 543 a second an average partition carries. For ad by country by device, where keys multiply, use a count-min sketch for the live view and keep the exact count in batch.

    Learn more Counting clicks in real time

  3. 03Design the booking system for a flash sale: a hundred seats on one train, two hundred thousand people arriving in the first minute. How do you sell every seat exactly once?

    Put a virtual waiting room at the edge that hands out tokens and admits people at the rate the booking core can take. Count inventory in one place per show and sell with a conditional decrement, take one away only if seats_left is above zero, never read then write. Take a reservation hold with a five-minute expiry while the buyer pays, and send payment with an idempotency key so a retry never charges twice.

    The crowd is thousands of times the supply, so the job is to finish bookings fairly, not to let everyone in. Two requests that read the same count both sell the last seat, so the check and the change must be one atomic step in the database. Overselling breaks a promise to a customer, so it is worse than briefly underselling.

    Follow-up One show is one hot shard. How do you scale the inventory without risking an oversell?

    Split that show's seats into blocks, each its own row with its own conditional decrement, and route each request to a block by hashing its token. Ten blocks of ten seats take 500 admissions a second down to about 50 per row, and total sales stay bounded by the sum of the blocks. Serve the seat map from a stale cache, and keep only the book button on the strict write path.

    Learn more The ten o'clock rush · Two people, one seat

  4. 04Design a distributed job scheduler. Jobs run at cron times, and a run must not be missed or duplicated when a machine dies.

    Keep a job definition store and a run log. A fleet of pollers claims due runs from the run log with a lease, one statement that sets an owner and an expiry where the run is due and no live lease exists. Claimed runs go to a queue for workers. Delivery is at-least-once, so every run has an id and the job is idempotent. Failed runs retry with exponential backoff and jitter, and after N tries they go to a dead-letter queue.

    A lease expires by itself, so a frozen or dead poller never blocks a run and no failure detector is needed. A worker can finish and die before reporting, which looks the same as never starting, so a retry is unavoidable and the unique run id is what makes it safe. The real load is a wall at round times, for example 3M jobs in the second 09:00:00, so one poller is not enough. Partition job ids by hash across pollers, and add random delay to jobs that can tolerate it.

    Follow-up A job must run exactly once per day, even across a failover. How?

    Exactly-once does not exist, so build at-least-once plus a unique key. Make the run id the job id plus the date and make it the primary key of the run log, so a second scheduler cannot create a second run. The job writes its side effect under the same id, and a fencing token that grows with each lease stops a frozen worker that wakes late.

    Learn more Run this at nine · Who holds the pen

  5. 05Design a metrics and monitoring system for 10,000 hosts. Then someone adds a user_id label to a metric. What happens?

    Each host exposes counters, gauges and histograms. A collector scrapes them every 10 seconds into a time series database, which appends to a write-ahead log and an in-memory head, then flushes compressed blocks and downsamples with age. Alerts fire on error budget burn rate, not on causes like CPU. A user_id label multiplies series by the number of users and runs the database out of memory: keep labels bounded and put ids in logs and traces.

    10,000 hosts at 500 series each is 5 million series and 500,000 samples a second. At about 1.37 bytes a sample (Facebook's Gorilla paper, 2015) that is roughly 59 GB a day, and downsampling to 1-minute after 7 days and 1-hour after 30 brings a year to under a terabyte. Cardinality, not sample volume, is what kills a time series database: 20 label combinations times 1 million users is 20 million new series. Say the numbers before the product names.

    Follow-up The database is falling over from cardinality and the fleet is growing 10x. How do you scale it?

    Cap series per target and drop labels outside an allow-list at ingestion, and alert on the series count itself. Then shard by series hash so each series lives on one server and appends stay local. Add a federation tier where regional servers keep full detail and a global server pulls only rolled-up series, such as failed payments summed by service.

    Learn more Is it up? · One request, forty services