The whole design
After this: Draw the whole design, say what each part is for, and name where it runs out of room first.
On this page · 4 sections · 2 checks
You now have every piece: the numbers, the two endpoints, the table, the codes and the cache. In an interview you would end by drawing the whole thing once more and saying what each part is for. What does that look like, and which part gives way first when the service grows?
The diagram
Press Run it, then step through and watch the requests move.
Each part, the job it does, and the number that sizes it:
| Part | Its job | The number |
|---|---|---|
| Load balancer | hands each request to the next app server | about 3,900 requests a second |
| App servers | run the two endpoints; each holds its own block of counter values | 10 servers at the average, about 20 at a peak of twice that; 400 requests a second each |
| Cache | code → long URL for the busiest links | 5 GB, hit ratio 0.9 |
| Database (primary) | the links table; takes every write | 38.6 writes a second, 3 TB over five years |
| Read replicas | answer the redirects the cache misses | about 386 reads a second at 0.9 |
The server count comes from the first tutorial of this learning path, where one server handled 400 requests a second. 3,858 redirects and 38.6 creates make about 3,897 requests a second, and 3,897 / 400 is about 9.7, so 10 app servers for the average. Servers have to cope with the peak, as the Estimating tutorial showed. With its assumption of a peak at twice the average, that is about 7,800 requests a second, and 7,800 / 400 = 19.5, so about 20 app servers. The requests through the load balancer and the reads that reach the replicas are averages too, and double at that peak; the cache size and the storage do not change. The 400 depends on the work each request does, so treat the count as an order of size.
Where it runs out of room first
The part that fills up first and limits the whole service is the . Go through the design part by part:
- Without the cache, the database. Every redirect would read it: 3,858 times a second, a hundred times the writes. This is why the cache came first.
- With the cache, the misses. About 386 reads a second still reach the database. Read replicas share them.
- A single counter. Every new link would ask the same machine for a number. Blocks of 1,000 numbers mean a server asks once per thousand links.
- A single cache server. If it stops, every redirect falls through to the database at once. Run two cache servers, so one can take over.
Storage grows slowly: 3 TB over five years can sit on one database server with large disks. If the numbers grew ten times, the table could be split by code across several databases, the sharding from Databases at scale.
Check yourself
Take the design without its cache. As traffic grows, which part runs out of room first?
The owner wants fewer requests to reach the service and does not need to count clicks. Which change does that?
Honesty notes
- Draw the whole design: load balancer, app servers, cache, primary database and read replicas, each with its number.
- Work out the app servers: about 3,900 requests a second at 400 each is about 10 servers on average, and about 20 at a peak of twice the average.
- Name the bottleneck part by part: the database without a cache, the misses with one, a single counter, a single cache server.
- Talk through a design in order, and say each trade-off out loud with its number.