Chuyển đến nội dung

Bài 63 · Practice and Interviews

Back-of-Envelope Estimation Practice

Practice system design estimation: powers of two, latency numbers, the seconds-per-day trick, and five worked problems for QPS, storage, bandwidth and servers, with Python.

Back-of-envelope estimation means getting a rough answer fast, using simple arithmetic and a few memorized numbers. In a design interview, and in real planning, it tells you whether you are building something that fits on one machine or something that needs a fleet. The goal is the right order of magnitude (is it 100 or 100,000?), not the exact digit.

This lesson gives you the numbers to memorize, a method, and five worked problems with Python checks. Do each problem on paper first, then compare.

Numbers to memorize

Powers of two

Power Approximate value Name
2^10 1 thousand 1 KB
2^20 1 million 1 MB
2^30 1 billion 1 GB
2^40 1 trillion 1 TB
2^50 1 quadrillion 1 PB

Latencies (rough, modern hardware)

Operation Time
CPU cache or memory read 1 to 100 ns
SSD random read about 100 microseconds
Read 1 MB from memory about 100 microseconds
Round trip within a data center about 0.5 ms
Read 1 MB from SSD about 1 ms
Disk seek (spinning) about 10 ms
Round trip across continents about 100 to 150 ms

The lesson of the table: memory is fast, the network inside a data center is cheap, and crossing oceans is expensive. Avoid chatty cross-region calls.

Time: a day has 86,400 seconds, which you can round to 100,000 (a 15 percent error is fine). A month is about 2.5 million seconds; a year is about 30 million.

Availability as downtime per year: 99% is 3.65 days; 99.9% is 8.8 hours; 99.99% is 53 minutes; 99.999% is 5 minutes.

Python Check the memorized tables

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The method

  1. State assumptions out loud (users, actions, sizes). Round them.
  2. Convert to per-second by dividing daily totals by 100,000.
  3. Multiply for peak (2 to 5 times) and for sizes.
  4. Sanity check against known ranges: one server, one database, one data center.
  5. Write units next to every number, and say the answer in a sentence.

Problem 1: tweets per second and storage

A service has 300 million daily users; each posts 0.5 tweets per day on average, each tweet is 300 bytes of text plus metadata. How many posts per second and how much storage per year?

Python Problem 1: posting traffic and text storage

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

About 1,700 writes per second (5,000 at peak) is easy for a sharded database. Text alone is only about 27 TB per year. Media (images, video) will be the real storage problem.

Problem 2: image storage and CDN bandwidth

Users upload 10 million photos per day, average 2 MB after processing, plus three resized copies of 100 KB total. Each photo is viewed 50 times on average. Storage per year, and egress bandwidth?

Python Problem 2: photos

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

About 8 PB per year and tens of Gbps of egress. The answer is: object storage with tiering, resized derivatives (serve small versions most of the time), and a CDN. No single data center link should carry this alone.

Problem 3: how many servers?

A web tier must handle 20,000 requests per second at peak. Each request uses 20 ms of CPU. A server has 16 cores and should run at 60 percent CPU. How many servers?

Python Problem 3: server count

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

400 cores of work, 10 usable cores per server, so 42 servers. Multiplying CPU time by QPS gives core-seconds per second, which is just the number of busy cores.

Problem 4: cache memory

A site has 100 million items; each cached record is 1 KB. The hottest 10 percent of items serve 90 percent of the reads. How much memory to cache them, and how many 64 GB nodes?

Python Problem 4: cache sizing

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

About 10 GB: it fits on a single node, so you add a second for availability rather than for capacity. Always check whether the "big" number is actually small.

Problem 5: Little's law and connections

A chat service has 50 million concurrent users, each holding one WebSocket. A chat server can hold 200,000 connections. How many servers? If each user sends 1 message per minute averaging 200 bytes, what is the inbound message rate?

Python Problem 5: connections and messages

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

250 servers just to hold the connections, and 830,000 messages per second: connection count, not bandwidth, is the first constraint. This matches the shape of the chat system design.

Tips

  • Round aggressively: 86,400 becomes 100,000; 2.3 becomes 2.
  • Write the units each time (bytes versus bits is a classic slip; network speeds are bits).
  • Be consistent about decimal versus binary prefixes; the difference (about 7 percent at GB scale) rarely matters.
  • Estimate in layers: users, then actions, then requests, then bytes.
  • Compare to known capacity: if the result fits on one big machine, say so.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Roughly how many seconds are in a day, for quick math?

    86,400 rounds to 100,000, which makes daily-to-per-second conversion easy.
  2. 2. Which is faster: a round trip inside a data center or across continents?

    About 0.5 ms versus 100 to 150 ms.
  3. 3. A request needs 20 ms of CPU and arrives 1,000 times per second. How many cores are busy?

    1,000 x 0.02 s = 20 core-seconds per second, so 20 cores.
  4. 4. 99.99% availability allows about how much downtime per year?

    0.01 percent of 525,600 minutes is about 53 minutes.

Summary

  • Memorize powers of two, the main latency numbers, 100,000 seconds per day and availability tables.
  • Method: state assumptions, convert to per-second, apply peak factors, sanity check, write units.
  • Core-seconds, bytes per second and connections per server cover most interview estimates.
  • Always ask whether the large-looking number is actually small for modern hardware.

Next: System Design Trade-offs Cheat Sheet

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.