Design a URL shortener
medium 45 minTurn 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
Link storage for five years
Code length
All the numbers
| Quantity | Working | Result |
|---|---|---|
| Write QPS, average and peak | 100M / 86,400, then 3x | 1,150 and 3,500 |
| Read QPS, average and peak | 10B / 86,400, then 3x | 115,000 and 350,000 |
| Click event | code 7, time 8, IP hash 16, country 2, referrer 50, agent hash 8 | about 100 B |
| Raw clicks | 10B × 100 B | 1 TB/day, 12 MB/s |
| Rolled-up analytics | hourly aggregates | about 100 GB/day (10x less) |
| Redirect bandwidth | 115K × 500 B | 58 MB/s, 175 MB/s peak, 5 TB/day |
| Redis hot set | 1B hottest links × 500 B | 500 GB plus overhead |
| CDN offload target | 65% or more |
Latency budget (p99 under 100 ms)
| Step | Budget |
|---|---|
| DNS (cached) | 10 ms |
| TLS (session resumption) | 20 ms |
| CDN hit, or route to origin | 5 to 20 ms |
| Redis lookup | 1 to 5 ms |
| 302 response | 5 ms |
| Buffer | 35 ms |
So what?
The decision each number makes
| Number | Decision | Would change if |
|---|---|---|
| 115,000 reads/s, 350,000 at peak | Cache in front of the store, many redirect instances, edge for the hottest | Under about 10,000/s one database with replicas is fine |
| 1,150 writes/s, 3,500 at peak | Nothing: one good database takes this | Ten times more writes |
| 183 TB in five years, 550 TB with copies | Partition the links by code | At 3 TB keep one relational database |
| 182.5B codes against | Seven characters | Under about 50 billion links, six would do |
| 1 TB of clicks a day | A stream and rollups, not the link database | Under about 1 GB a day, a table and a nightly job |
| 100 ms p99 worldwide | Serve near the user: edge and regions | One-country users: one region |