Design an image hosting service (like Imgur)
medium 50 minUpload a picture, get a link at once, and let the whole world look at it.
Numbers that decide
Work out a number when the answer changes what you build.
Capacity
Scale estimates
Inputs are the interviewer's figures plus assumptions: 1 MB an original on average, 300 KB a view on average, 400 KB for all stored variants, 10% exact duplicates. They are not the real company's numbers.
Requests
Traffic to viewers
What reaches storage
All the numbers
| Quantity | Working | Result |
|---|---|---|
| New bytes a day | 10M × (1 MB + 0.4 MB) | 14 TB |
| After duplicates | 14 TB × 0.9 | 12.6 TB |
| Three years | 12.6 TB × 1,095 | 13.8 PB |
| Records, three years | 10M × 1,095 | 11B |
| Record storage | 11B × 500 B | 5.5 TB, about 11 TB with indexes |
| Upload traffic | 10 TB / 86,400 | 116 MB/s, 350 MB/s peak |
| Workers | 347/s × 0.5 s / 60% busy | about 290 |
| Album and page calls | assumed 10,000/s peak, 1,000/s a server, half busy | 20 servers |
| Hot records in Redis | 100M × 1 KB | 100 GB before copies |
| View events | 5B × 100 B | 500 GB a day |
| One famous picture | 50,000/s × 300 KB | 15 GB/s, 120 Gbit/s |
Time from upload to ready (assumed)
| Step | Budget |
|---|---|
| Confirm original, write record, queue job | 0.3 s: link returned |
| Wait in queue | 1 to 5 s |
| Fetch and decode | 0.5 s |
| Safety checks | 0.5 s, longer if reviewed |
| Make six files | 0.5 s of CPU |
| Store, publish record | 0.5 s |
| Total | about 4 to 8 s |
So what?
The decision each number makes
| Number | Decision | Would change if |
|---|---|---|
| 417 Gbit/s at peak | CDN, files on their own host name | Under 5 Gbit/s, a few servers |
| 500 to 1 | Prepare on write, cache on read | Most uploads never viewed |
| 1,740 storage reads/s | One store behind a shield is enough | No shield: 8,700/s |
| 290 workers | Queue and autoscaling, apart from the API | 10 uploads/s: two machines |
| 13.8 PB in three years | Object storage, classes, no duplicates | Links expire in a week |
| 11 TB of records | Shard by image ID, 16 shards of 700 GB | Under 1 TB, one database |
| 500 GB of events a day | Edge logs and rollups | Counts not shown |