System Design Patterns: From Fundamentals to Real Systems
Vote

0% completed

​

Back-of-Envelope Math

Why Rough Numbers Are Enough

Calculation 1: Rates and Sizes

The Latency Table

Calculation 2: Requests in Flight

Calculation 3: Availability and Nines

Calculation 4: Hit Ratio

Using Numbers to Leave Parts Out

Doing the Math in an Interview

Where This Leads

Check Yourself

Back-of-Envelope Math for System Design: Numbers to Know, Availability Chains, Cache Hit Ratios, and Sizing (Video)
Back-of-the-Envelope Math
Back-of-the-Envelope Math

The store receives 200 orders per second on a normal day. How many orders is that per day? A day has 86,400 seconds, so the exact answer is 200 × 86,400 = 17,280,000 orders.

Now round the day to 100,000 seconds. The answer becomes 200 × 100,000 = 20 million orders, and you can work it out in your head. The rounded answer is about 16 percent too high. Rounding up is the safe direction, because a design built for slightly more traffic than it receives keeps working.

Both answers lead to the same design decisions, and only one of them needs a calculator. This lesson teaches four calculations of that kind: rates and sizes, requests in flight, availability, and hit ratio. It also gives two tables of reference numbers and shows how to use the results in a design interview.

Why Rough Numbers Are Enough

A quick estimate written on any piece of paper is called a back-of-the-envelope calculation. The name comes from the habit of doing the math on the back of an old envelope. The goal is the right order of magnitude, which means the right power of ten: tens, thousands, or millions.

Design choices change at powers of ten, not at small differences. A database that handles 300 writes per second and one that handles 340 need the same design. A database that handles 30,000 writes per second needs a different one. So round every number to one or two digits, keep the units, and round in the direction that makes the system look busier.

Calculation 1: Rates and Sizes

Per day to per second. Divide by 100,000. The store's product pages receive about 200 million views a day, and 200 million ÷ 100,000 = 2,000 views per second. Going the other way, multiply a rate per second by 100,000 to get a day.

Average to peak. Traffic is not flat across a day. Evenings are busier than nights, so the busiest hour receives about 3 to 5 times the average. For product views, 2,000 per second on average becomes 6,000 to 10,000 per second at the busiest hour. Each view reads the store database once. This is why the database receives 8,000 reads per second at the busiest hour.

The 3 to 5 times rule describes ordinary daily traffic and does not cover planned events. The store's flash sale brings 5,000 orders per second, which is 25 times the normal 200, far outside the rule. A planned event needs its own arithmetic. Over the sale's two minutes, 5,000 × 120 = 600,000 orders arrive, while the checkout chain completes 400 × 120 = 48,000. The Message Queue lesson shows what the store does about that gap.

One more check prevents a common mistake. Checkout receives 300 requests per second, but the store places 200 orders per second. The other 100 requests are cart updates and coupon checks, so the two numbers measure different things.

Count times size. Storage is the number of items multiplied by the size of one item. Each order event, the message that records a placed order, is about 400 bytes. With the rounded day, 20 million × 400 bytes = 8 GB per day, and keeping events for 90 days needs 720 GB. The exact figures are 6.9 GB per day and about 620 GB. Either way, the total is under 1 TB, so one machine can store it.

These sizes are worth remembering:

ItemTypical size
One character of plain text1 byte
An ID16 to 36 bytes
A database rowabout 1 KB
An image100 KB to 1 MB
One thread's memoryabout 1 MB

A million 1 KB rows is 1 GB, and a billion is 1 TB.

The same rule of multiplication applies to anything that exists once per server. A checkout server with 200 threads at 1 MB each uses about 200 MB for threads alone. Ten such servers use 2 GB. The rule also applies to the calls that one request makes to other services, which is called fan-out. Each order calls four services, so 200 orders per second become 800 internal calls per second. Multiply every cost that repeats, per server or per request, by the number of servers or requests before you decide that it is small.

The Latency Table

Time has its own reference numbers. Each one is a rough value, and each row is slower than the row above, often by ten times or more.

How long things take
How long things take

Look at how far apart the bars are. Each grid line is ten times the one before it, so a memory read and a trip between regions differ by a factor of a million. The orange line marks the point where a response stops feeling instant to a person.

OperationRough time
Read from memory100 ns
Read from an SSD100 µs
A call inside one datacenter, including a cache read0.5 to 1 ms
A simple database query5 to 15 ms (use 10)
A disk seek or a slow query10 to 100 ms
A trip between regionsabout 100 ms, up to 200 ms between continents
What a person feels as instantunder 100 to 200 ms

One more number helps with large data: one machine reads about 100 MB per second from disk. At that speed, scanning 1 TB takes 10,000 seconds, nearly three hours.

Calculation 2: Requests in Flight

How many requests is checkout working on at any moment? The answer comes from one multiplication, known as Little's law: requests in flight equal the arrival rate multiplied by the time each request takes. "In flight" means started and not yet finished. Some lessons call the same number concurrency.

Checkout receives 300 requests per second, and each request takes 0.25 seconds. So 300 × 0.25 = 75 requests are in flight. Each one holds a thread, and checkout has 200 threads, so 75 are busy and 125 are free.

Now change one number. Payment takes 150 ms of the 250 ms, which is 60 percent of every request. If payment slows to 2 seconds, each request takes about 2.1 seconds, and 300 × 2.1 = 630 threads are needed. The formula works for anything that a request holds while it runs: threads, database connections, locks, or open sockets.

The same law also gives the most a pool can finish: the number of threads divided by the time each request takes. At 0.25 seconds, 200 threads could finish 200 ÷ 0.25 = 800 orders per second. Under heavy load, however, the payment and database calls slow down, and each order takes about 0.5 seconds. That is why the checkout chain completes only 200 ÷ 0.5 = 400 orders per second.

Calculation 3: Availability and Nines

Availability is the share of time a service answers correctly. Engineers describe it by the number of nines: 99.9 percent is "three nines". A 30-day month has 43,200 minutes, so each level allows a fixed amount of downtime.

AvailabilityDown per 30-day month
99 percentabout 7 hours
99.9 percentabout 43 minutes
99.99 percentabout 4 minutes
99.999 percentabout 26 seconds

Each extra nine costs about 10 times more in hardware, engineering, and operations. So choose the number of nines the business needs, not the highest number you can name.

When a request needs several services, their availabilities multiply.

Availability multiplies along a chain
Availability multiplies along a chain

The top row shows the checkout chain, and checkout works only when all four services work. Multiply the four values, and the result is about 99.6 percent. The bars below turn that into time: one service is down 43 minutes a month, but the chain of four is down about 173 minutes.

A quicker estimate adds the failure rates. Each service fails 0.1 percent of the time, so four fail about 0.4 percent, which gives 99.6 percent again. Three services give 99.7 percent, and five give 99.5 percent. Every service added to a request path lowers the availability of the whole path.

Calculation 4: Hit Ratio

A cache is a fast copy of data that is read often. The hit ratio is the share of reads that the cache answers. A read the cache answers is a hit, and a read it cannot answer is a miss. The store has no cache today, but the calculation shows what adding one would change.

Assume a hit takes 1 ms. A miss checks the cache first, then reads the database, so it takes 1 + 10 = 11 ms.

What a cache would change
What a cache would change

Compare the two panels. At 90 percent, 800 of the 8,000 reads reach the database, and the average read takes 2.0 ms. At 99 percent, only 80 reach it, and the average read takes 1.1 ms. The difference between 90 and 99 percent looks small, but 90 percent sends 10 times as many reads to the database.

Two points follow from these numbers. First, only misses reach the database, so count the misses to size it, not the hits. Second, each miss costs 11 times as much as a hit, so at 90 percent the misses cause more than half of the 2.0 ms average. The slower the database, the more each percent of hit ratio matters.

Using Numbers to Leave Parts Out

The math also tells you which parts to leave out. The store's database receives 300 writes per second, and one database handles that easily. Splitting the data across several machines, called sharding, would add work and risk with no benefit. The right answer is a short sentence: "We do not need that yet."

The same arithmetic finds the real problem. The database receives 8,000 reads per second at the busiest hour, against a capacity of 5,000. Reads need a pattern, and writes do not. Without the numbers, both look equally urgent.

Doing the Math in an Interview

In a design interview, do the arithmetic aloud, early, and briefly. Two or three minutes is enough. Write each result on the board where you can point to it later.

  • Say the rounding. "A day is about 100,000 seconds, so 20 million orders a day is 200 per second."
  • Write peak next to average. "200 million views a day is 2,000 per second, so I plan for 6,000 to 10,000."
  • Use each number to decide something. "300 writes per second fits one database, so I will not shard." A number that decides nothing does not belong on the board.
  • Check every part you draw. Ask how many requests it receives, how long each takes, and what fails if it fails.

Interviewers rarely check whether your answer is exact. They check whether it is the right order of magnitude and whether it changed your design.

Where This Leads

These numbers describe the store before any pattern is added. Checkout holds 75 of its 200 threads on a normal day, and every one of those threads waits for four calls in a row. The Request-Response lesson shows what happens to those threads when one of the calls slows down.

Check Yourself

Q1. The store wants to record every product-page view, at about 200 bytes per record, and keep the records for 30 days. Use the store's 200 million views a day. What write rate should the log plan for, how much storage do 30 days need, and how long does one machine take to read the whole log once?

<details> <summary>Show answer</summary>

The average is 200 million ÷ 100,000 = 2,000 writes per second. With the peak rule of 3 to 5 times, plan for 6,000 to 10,000 writes per second. Each record is only added at the end of the log, so even 10,000 records per second is only 2 MB per second of writing.

Storage is 200 million × 200 bytes = 40 GB per day, and 30 days need 40 × 30 = 1,200 GB, or 1.2 TB. At 100 MB per second, one machine reads 1.2 TB (1,200,000 MB) in 12,000 seconds, about 3.3 hours. The read time matters only if someone needs reports more often than every few hours. Ask that question before you add any part.

</details>

Q2. A team proposes a fifth service in the checkout chain, a fraud check that is available 99.9 percent of the time. What happens to checkout's availability, and how many minutes of downtime per month does that mean?

<details> <summary>Show answer</summary>

Five services in a chain give 0.999⁵, about 99.5 percent. Adding the failure rates gives the same estimate: 5 × 0.1 = 0.5 percent. Of a 43,200-minute month, 0.5 percent is about 216 minutes, compared with about 173 minutes for the chain of four. The fraud check adds about 43 minutes of downtime per month, so the team should ask whether checkout must wait for it.

</details>

Q3 (incident). The analytics team schedules a job that reads all 90 days of order events every hour to rebuild a dashboard. A week later, the machine is slow all day, and the dashboard is hours behind. Each new run starts before the last one ends. Use the numbers to explain why, and suggest a fix.

<details> <summary>Show answer</summary>

Ninety days of order events are about 620 GB (720 GB with the rounded day). At 100 MB per second, one full read takes 6,200 to 7,200 seconds, close to two hours. A job that needs about two hours cannot finish every hour, so the runs overlap, and each one slows the others.

The fix is to read only what changed. One hour of new events is 200 × 3,600 = 720,000 orders × 400 bytes, about 290 MB, which takes about 3 seconds to read. The dashboard can add each hour's new events to the totals it already has.

</details>

💡 In the interview: Do the arithmetic aloud in the first few minutes, and use each result to decide something. A strong answer sounds like this: "200 orders per second is about 20 million a day. At 400 bytes each, 90 days of order events is under 1 TB, so one machine holds it. 300 writes per second fits one database, so we do not need sharding yet." Expect the follow-up "When would you shard?" Answer with the number that would change the decision: the write rate or data size that one machine can no longer handle.

Key takeaway: Four calculations size most designs. Divide a daily count by 100,000 for a rate, and plan for 3 to 5 times the average at peak. Multiply a rate by a duration for requests in flight, multiply availabilities along a chain, and count cache misses, not hits. Round toward the busier answer, multiply every cost that repeats per server or per request, and use each result to decide something, including "We do not need that yet."

Reading Progress

0%


Vote for new content

On This Page

Why Rough Numbers Are Enough

Calculation 1: Rates and Sizes

The Latency Table

Calculation 2: Requests in Flight

Calculation 3: Availability and Nines

Calculation 4: Hit Ratio

Using Numbers to Leave Parts Out

Doing the Math in an Interview

Where This Leads

Check Yourself