2. Back-of-Envelope Estimation
Practice: five levels
Assignments
Five levels. Do them in order. Answers are in "Solutions" at the end, so try first.
Level 1: Think (no code)
A. For each system, which number is most likely to drive the design: read requests, write requests, storage, or bandwidth? Say why in one line.
- A URL shortener.
- A video streaming app.
- A service collecting logs from 1,000 servers, each sending 100 lines a second.
- An online exam that 2 lakh (200,000) students start at exactly 10:00 am.
- A photo backup app where people upload all their phone photos and rarely look at them again.
B. Each estimate below has a problem. Find it.
- "We get 1 billion requests a month. That's 1 billion ÷ 86,400 ≈ 11,600 requests a second."
- "Each video stream is 5 MB a second, so 1,000 viewers need a 5 Gbps link."
- "We get 300 requests a second on average, so 3 servers at 100 a second each is enough."
Level 2: Calculate
A video app (assumed numbers):
- 5 million daily active users, each watching 30 minutes a day.
- Video streams at 5 Mbps (megabits per second).
- 50,000 new videos uploaded a day, 500 MB each.
- Total video data sent to viewers per day, in PB.
- Average egress bandwidth, in Gbps. And at a peak factor of 3?
- Storage for one year of uploads (one copy), in PB.
- Which of these three numbers would you raise first with the interviewer, and why?
Level 3: Build
Write a small Python function estimate(...) that takes:
- daily active users, reads per user, writes per user,
- size of each write (bytes), size of each read (bytes),
- peak factor (default 3), years (default 1), replicas (default 1), cache fraction (default 0.2),
and returns: average read and write requests per second, peak reads per second, read/write ratio, total storage in bytes, egress in Gbps, and cache size in bytes. Add servers_needed(peak_rps, rps_per_server, target_utilisation=0.6, spare=1).
Check that for PicShare (10M users, 50 reads and 0.2 writes each, 2 MB writes, 200 KB reads) you get:
- About 5,787 reads and 23 writes a second, about 17,361 at peak, ratio 250.
- 1.46 PB a year for one copy, 4.38 PB with 3 replicas.
- About 9.26 Gbps egress.
- 30 servers at 1,000 requests a second each.
Level 4: Debug
A teammate's capacity script says PicShare needs only about 1.2 Gbps of egress. The network team orders links for that, and on launch day the photos crawl.
- Find the bug. Explain step by step why the links were too small, and by how much.
- Fix it.
- Name one more thing the script ignores that would make the links too small even after the fix.
Level 5: Design
Estimate for "Design a chat app", with these agreed assumptions:
- 50 million daily active users, each sending 40 messages a day.
- A text message is about 100 bytes. 5% of messages carry a photo of about 200 KB.
- Peak factor 3. Keep messages for 1 year.
- One chat server handles about 5,000 messages a second (assumed). Run at 60%, keep 1 spare.
Deliver:
- Messages per second, average and peak.
- Text storage for a year.
- Media storage for a year.
- Ingress bandwidth for media, in Gbps (average).
- Number of chat servers.
- Which number drives the design most, and what it points you to.