AlgoViz

Design an image hosting service (like Imgur)

medium 50 min

Upload 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

10M86,400≈116 uploads/s,5B86,400≈57,870 views/s\frac{10\text{M}}{86{,}400} \approx 116\ \text{uploads/s}, \qquad \frac{5\text{B}}{86{,}400} \approx 57{,}870\ \text{views/s}
At a 3x peak: 347 uploads/s and 173,600 views/s. 500 views for each upload.

Traffic to viewers

5B×300 KB86,400 s≈17.4 GB/s≈139 Gbit/s,×3≈417 Gbit/s\frac{5\text{B}\times 300\text{ KB}}{86{,}400\text{ s}} \approx 17.4\ \text{GB/s} \approx 139\ \text{Gbit/s}, \qquad \times 3 \approx 417\ \text{Gbit/s}
Far more than one site should send. This number alone asks for a CDN.

What reaches storage

(1−0.95)×(1−0.80)=0.01,173,600×0.01≈1,740 reads/s at peak(1-0.95)\times(1-0.80) = 0.01, \qquad 173{,}600 \times 0.01 \approx 1{,}740\ \text{reads/s at peak}
Assumed hit rates: 95% at the edge, 80% of misses at the shield. Traffic from storage is about 0.5 GB/s at peak.

All the numbers

QuantityWorkingResult
New bytes a day10M × (1 MB + 0.4 MB)14 TB
After duplicates14 TB × 0.912.6 TB
Three years12.6 TB × 1,09513.8 PB
Records, three years10M × 1,09511B
Record storage11B × 500 B5.5 TB, about 11 TB with indexes
Upload traffic10 TB / 86,400116 MB/s, 350 MB/s peak
Workers347/s × 0.5 s / 60% busyabout 290
Album and page callsassumed 10,000/s peak, 1,000/s a server, half busy20 servers
Hot records in Redis100M × 1 KB100 GB before copies
View events5B × 100 B500 GB a day
One famous picture50,000/s × 300 KB15 GB/s, 120 Gbit/s

Time from upload to ready (assumed)

StepBudget
Confirm original, write record, queue job0.3 s: link returned
Wait in queue1 to 5 s
Fetch and decode0.5 s
Safety checks0.5 s, longer if reviewed
Make six files0.5 s of CPU
Store, publish record0.5 s
Totalabout 4 to 8 s

So what?

The decision each number makes

NumberDecisionWould change if
417 Gbit/s at peakCDN, files on their own host nameUnder 5 Gbit/s, a few servers
500 to 1Prepare on write, cache on readMost uploads never viewed
1,740 storage reads/sOne store behind a shield is enoughNo shield: 8,700/s
290 workersQueue and autoscaling, apart from the API10 uploads/s: two machines
13.8 PB in three yearsObject storage, classes, no duplicatesLinks expire in a week
11 TB of recordsShard by image ID, 16 shards of 700 GBUnder 1 TB, one database
500 GB of events a dayEdge logs and rollupsCounts not shown