AlgoViz

Design a URL shortener

medium 45 min

Turn a long URL into a short code and redirect anyone who opens it.

Numbers that decide

Work out a number when the answer changes what you build.

Capacity

Scale estimates

Traffic

100M86,400≈1,150 writes/s100M×10086,400≈115,000 reads/s\frac{100\text{M}}{86{,}400} \approx 1{,}150\ \text{writes/s} \qquad \frac{100\text{M}\times 100}{86{,}400} \approx 115{,}000\ \text{reads/s}
At a 3x peak that is about 3,500 writes/s and 350,000 reads/s. Writes are easy; reads are the challenge.

Link storage for five years

182.5B links×1 KB≈183 TB,×3 copies≈550 TB182.5\text{B links}\times 1\,\text{KB} \approx 183\,\text{TB},\quad \times 3 \text{ copies} \approx 550\,\text{TB}
100M × 365 × 5 = 182.5 billion links. A record is code 7 + URL 500 + owner 8 + two timestamps 16 + metadata 100, about 640 bytes; round up to 1 KB.

Code length

626≈56.8B<182.5B<627≈3.5T,3.5T100M/day≈96 years62^6 \approx 56.8\text{B} < 182.5\text{B} < 62^7 \approx 3.5\text{T},\qquad \frac{3.5\text{T}}{100\text{M/day}} \approx 96\ \text{years}
Six characters are not enough, seven are plenty, and eight (628≈21862^8 \approx 218 trillion) are overkill.

All the numbers

QuantityWorkingResult
Write QPS, average and peak100M / 86,400, then 3x1,150 and 3,500
Read QPS, average and peak10B / 86,400, then 3x115,000 and 350,000
Click eventcode 7, time 8, IP hash 16, country 2, referrer 50, agent hash 8about 100 B
Raw clicks10B × 100 B1 TB/day, 12 MB/s
Rolled-up analyticshourly aggregatesabout 100 GB/day (10x less)
Redirect bandwidth115K × 500 B58 MB/s, 175 MB/s peak, 5 TB/day
Redis hot set1B hottest links × 500 B500 GB plus overhead
CDN offload target65% or more

Latency budget (p99 under 100 ms)

StepBudget
DNS (cached)10 ms
TLS (session resumption)20 ms
CDN hit, or route to origin5 to 20 ms
Redis lookup1 to 5 ms
302 response5 ms
Buffer35 ms
About 50 to 80 ms when a cache answers. No relational database is ever on this path.

So what?

The decision each number makes

NumberDecisionWould change if
115,000 reads/s, 350,000 at peakCache in front of the store, many redirect instances, edge for the hottestUnder about 10,000/s one database with replicas is fine
1,150 writes/s, 3,500 at peakNothing: one good database takes thisTen times more writes
183 TB in five years, 550 TB with copiesPartition the links by codeAt 3 TB keep one relational database
182.5B codes against 62662^6Seven charactersUnder about 50 billion links, six would do
1 TB of clicks a dayA stream and rollups, not the link databaseUnder about 1 GB a day, a table and a nightly job
100 ms p99 worldwideServe near the user: edge and regionsOne-country users: one region
Thresholds are rules of thumb for simple key lookups. Say them as estimates and offer to measure.