Load estimates
An estimate on paper answers one question: what scale of system is in front of you. The interviewer does not expect exact numbers, only the order of magnitude: tens of requests a second or tens of thousands, gigabytes or petabytes. The order decides whether everything fits on one machine and whether you need a cache.
Imagine a library that lends a book 100 times more often than it receives a new one. Its lending desk and its receiving dock are planned separately: lending has many windows, receiving has one. The analogy stops working at speed: a reader waits a minute, a redirect waits 50 ms.
The interviewer has already given the starting numbers:
- 100 million new links a month;
- each link is opened about 100 times on average;
- 20% of links get 80% of the redirects, and one link can go viral at up to 10,000 redirects a second;
- links are kept for 5 years;
- a long URL is up to 2,048 characters, about 100 on average.
Two reference values
Numbers per month and per day are awkward to compare with requests per second, so remember two values:
- a day has 86,400 seconds, roughly 10⁵;
- a month has about 2.6 million seconds.
Take the peak as 10 times the average, unless the interviewer gives another number.
Writes
- 100 million links a month divided by 2.6 million seconds is about 38, which we round to 40 new links a second.
- At peak, 10 times more: about 400 a second.
One small database handles 40 writes a second. Writes are not the hard part of this prompt.
Reads
- Each link is opened about 100 times, so there are 100 times more redirects than new links: 40 × 100 = 4,000 a second on average.
- At peak, 10 times more: 40,000 redirects a second.
- One viral link can bring up to 10,000 redirects a second on its own.
What the ratio tells you
Reads outnumber writes 100 to 1, and a redirect must fit within 50 ms. So almost all of the load and the strictest target fall on redirects. This is the main conclusion of the prompt:
- redirects get their own stateless service, and you add copies of it for the peak without touching link creation;
- a cache of popular codes sits in front of the store, or every redirect would go to the store;
- slow link creation does not delay a redirect, because they go through different services.
Store replicas do not replace the cache. Without it, each of the 40,000 redirects a second waits for a database read, and the 50 ms target is harder to meet.
Try the first conclusion on a diagram.
About 40 links are created a second, while redirects run at 4,000 on average and 40,000 at peak, each within 50 ms. Right now everything goes through the link service. Add what separates redirects from link creation, and connect the blocks.
Storage over 5 years
- 100 million links × 12 months × 5 years = 6 billion links.
- One row holds a 100-character URL, a seven-character code, the owner, two dates and a deleted flag. With overhead, that is about 200 bytes.
- 6 billion × 200 bytes = 1.2 × 10¹² bytes, about 1.2 TB.
- With three replicas, about 3.6 TB.
The row is sized by the average 100-character URL, not by the 2,048 limit: the limit is rare. Redirects are not stored in this table: each link clicked on a given day gets one counter row for that day in a separate table.
Cache size
- A day has 4,000 × 86,400 ≈ 350 million redirects.
- Most of them go to the popular 20% of links, so a day touches tens of millions of distinct links, not billions.
- If you keep in memory the links clicked in the last day, that comes to, for example, 20 million × 200 bytes = 4 GB.
A few gigabytes fit in one machine's memory, and such a cache serves most redirects without touching the store.
Code length
In base62 each character of a code is one of 62 options: 10 digits, 26 lowercase and 26 uppercase Latin letters. The options for each position multiply:
- Seven characters give 62⁷ ≈ 3.5 × 10¹² codes.
- Five years need 6 × 10⁹ codes.
- The margin is about 580 times.
Six characters would give 62⁶ ≈ 5.7 × 10¹⁰, a margin of less than 10 times. The requirements allow seven, and with seven the codes will not run out.
Key points
- About 40 writes a second, 4,000 redirects on average and 40,000 at peak: reads outnumber writes 100 to 1.
- So redirects get their own stateless service and a cache in front of the store.
- 6 billion links × 200 bytes ≈ 1.2 TB, about 3.6 TB with three replicas.
- Seven base62 characters give 3.5 × 10¹² codes, hundreds of times more than needed.