AlgoViz

Back-of-the-envelope calculations

easy 25 min

A back-of-the-envelope calculation is a quick, rounded sum that tells you whether a design is feasible.

Video lesson · Chapter 1 of 5

Start from the quantity to decide

0:00 / 4:05

Four steps, one estimate

1:20Reel coming soon

Theory

Read or listen first. About 2 min.

What it is

A back-of-the-envelope calculation (often shortened to BOTE) is a rough sum done with rounded numbers, small enough to fit on the back of an envelope. It answers one kind of question: is this roughly possible, and roughly how much do I need?

It is an estimate, so it is meant to be slightly wrong. What matters is the order of magnitude: the number of zeros. Whether a service gets ten or ten thousand requests a second changes the design. Whether it gets 11,574 or 12,000 does not.

Why it exists

A design can fail before anyone writes code, because the load does not fit the machine, the disk or the network. A one-minute sum finds that out for free.

It also stops you from building too much. Many systems that sound large fit on one database once you do the division. And because every assumption is said out loud, other people can correct the input instead of arguing about the result.

A short history

  1. 1945

    At the Trinity test Enrico Fermi drops scraps of paper as the blast passes and estimates its strength from how far they move.

  2. 1961

    John Little publishes a proof that the average number of items in a system is the arrival rate times the average time each stays. Many capacity sums rest on it.

  3. 1984

    Jon Bentley's Programming Pearls column in Communications of the ACM is titled "The Back of the Envelope" and shows programmers how to use such sums.

  4. 2018

    Google's Site Reliability Workbook publishes a chapter on non-abstract large system design: designs that carry real quantities and are revised when the numbers do not fit.

Words to know

BOTE
Back of the envelope: quick and rounded.
Order of magnitude
A power of ten. 400 and 700 share one; 400 and 4,000 do not.
DAU
Daily active users: people who use the service on a given day.
QPS
Queries, or requests, per second.
Retention
How long data is kept.
Headroom
Capacity kept free, so that a bump or a failed machine does no harm.
Sanity check
Comparing a result with something you know to see whether it can be true.

How it works

Step through the idea, or press Play.

Step 1 of 3

Clarify and assume. Ask what is known, then say the rest as assumptions

Understand it

Start here

The pizza party

Twenty friends are coming. You do not know exactly how hungry each one is, and you do not need to. A few seconds of rounded arithmetic tells you how many pizzas to get.

How many pizzas?
20 friendsHow many?

Nobody counted exact appetites. The estimate only had to be good enough to place the order.

The idea

Precision or speed

Compare two ways of doing one multiplication. The exact one takes a calculator. The rough one takes a second.

Exact and rough

1,234,567×3.14159≈3,878,503and1M×3=3M1{,}234{,}567 \times 3.14159 \approx 3{,}878{,}503 \qquad\text{and}\qquad 1\text{M} \times 3 = 3\text{M}
The rough answer is 23% low. If 3 million and 3.9 million lead to the same design, the exact answer bought nothing.

Why bother

  • In interviews

    It shows that you think about scale, are practical, check your design and find bottlenecks early.

  • Sanity checks at work

    Is this bill, this log volume or this graph even possible?

  • Cost and capacity

    Roughly how many machines and how many terabytes, before anything is ordered.

  • Architecture choices

    One database or many, with or without a cache: a number decides.

Which estimates are worth a minute at all, and which to refuse, is the subject of Numbers that decide. This page is about how to do one well.

The method

CAFE: clarify, assume, figure, evaluate

One estimate from start to finish

If the last step says the answer cannot be true, go back to the assumptions, not to the arithmetic.
  1. 1

    Clarify the requirements

    Ask what is being estimated and what limits exist. "How many users? What is the read to write ratio? How long do we keep the data?"

  2. 2

    Assume, explicitly

    Fill the gaps with reasonable values and say them, so they can be corrected. "Let us assume 100 million daily users with 10 requests each."

  3. 3

    Figure

    Calculate step by step with rounded numbers and units. 100 million × 10 is 1 billion a day; divided by 100,000 that is 10,000 a second.

  4. 4

    Evaluate

    Is the order of magnitude believable, and what does it decide? "About 10,000 a second, 30,000 at peak: one server cannot, so several behind a balancer."

When a question looks too big, split it into parts you can count. How many bricks does a toy castle need? Nobody knows, but it has 4 walls of about 100 bricks.

Split, then multiply

4 walls×100 brickswall=400 bricks500M users×10 viewsuser=5B views4\ \text{walls} \times 100\ \tfrac{\text{bricks}}{\text{wall}} = 400\ \text{bricks} \qquad 500\text{M users} \times 10\ \tfrac{\text{views}}{\text{user}} = 5\text{B views}
Write the unit in every factor. If the units do not cancel to what you want, the sum is wrong.

Head maths

Round before you multiply

Numbers to round

ExactIn your headWhatError
86,400100,000seconds in a dayrates come out 14% low
2,592,0002.5 millionseconds in 30 daysrates 4% high
31,536,00030 millionseconds in a yearrates 5% high
1,073,741,8241 billionbytes in a gibibyte (GiB)7% low
1,000,000,0001 billionbytes in a gigabyte (GB)exact
365400days in a yearstorage 10% high
Roundings that err on the safe side are the most comfortable ones. 1,024 or 1,000? See numbers in perspective.

Is my estimate good enough?

The sums

Five sums and where each one lands

Each number belongs to a part

Users send requests to a load balancer (sum 1, traffic) and receive bytes (sum 3, bandwidth). Behind it application servers (sum 4, machines) read a cache (sum 5, cache size) and a database (sum 2, storage).
Traffic at the door, bytes on the wire, machines in the middle, memory in the cache, terabytes in the store.

1. Traffic

QPS=DAU×requests per user86,400,10M×5086,400≈5,800/s\text{QPS} = \frac{\text{DAU} \times \text{requests per user}}{86{,}400}, \qquad \frac{10\text{M} \times 50}{86{,}400} \approx 5{,}800/\text{s}
500 million a day. With the 100,000 shortcut it is 5,000, and 15,000 at a peak of three times. Decides: is one service instance enough?

2. Storage

storage=new items×size×retention,10M/day×500 KB×365≈1.8 PB a year\text{storage} = \text{new items} \times \text{size} \times \text{retention}, \qquad 10\text{M/day} \times 500\,\text{KB} \times 365 \approx 1.8\,\text{PB a year}
5 TB a day. Count only what is written; a read stores nothing. Say whether copies are included. Decides: images in object storage, not in the database.

3. Bandwidth

bandwidth=QPS×response size,100,000 viewers×5 Mb/s=500 Gb/s\text{bandwidth} = \text{QPS} \times \text{response size}, \qquad 100{,}000\ \text{viewers} \times 5\,\text{Mb/s} = 500\,\text{Gb/s}
Storage is in bytes and networks in bits, so multiply bytes by 8. Decides: 500 Gb/s is fifty 10 Gb/s cards, so a content delivery network.

4. Machines

n=⌈peakcapacity×0.7⌉+1,⌈50,0001,000×0.7⌉+1=72+1=73n = \left\lceil \frac{\text{peak}}{\text{capacity} \times 0.7} \right\rceil + 1, \qquad \left\lceil \frac{50{,}000}{1{,}000 \times 0.7} \right\rceil + 1 = 72 + 1 = 73
Dividing alone gives 50 machines at 100%, with none to spare. Use 70% and one spare. Capacity per machine is an assumption until a load test replaces it.

5. Cache

cache=hot items×size,1B×20%×1 KB=200 GB\text{cache} = \text{hot items} \times \text{size}, \qquad 1\text{B} \times 20\% \times 1\,\text{KB} = 200\,\text{GB}
That 20% of records get most reads is an assumption; check it. Decides: a few cache nodes, not one. See caching.
Python
import mathDAY = 86_400dau, per_user = 10_000_000, 50avg = dau * per_user / DAYpeak = avg * 3print(f"traffic:   {avg:,.0f}/s, peak {peak:,.0f}/s")print(f"storage:   {10_000_000 * 500_000 * 365 / 1e15:.2f} PB a year")print(f"bandwidth: {100_000 * 5 / 1000:,.0f} Gb/s")print(f"machines:  {math.ceil(50_000 / (1_000 * 0.7)) + 1}")print(f"cache:     {1_000_000_000 * 0.20 * 1_000 / 1e9:,.0f} GB")
The five sums. Change an input and see which answers move.
Edit and run it in your browser

Busy hours

Average, peak, plan

From a daily total to a capacity

A system built for the average fails every evening. If people arrive because a clock says so, count the people instead.

Two ideas are easy to mix up. The peak factor describes the users. The 70% describes your machines. Use each once. How a peak is found is in QPS.

Practice 1

A timeline service

Given: 500 million daily users, 10 timeline views each, 100 posts per view, about 1 KB per post, and a server that can do 10,000 such reads a second.

Worked aloud

  1. Interviewer

    Requests a second, bandwidth and servers, please.

  2. You

    500 million times 10 is 5 billion views a day. Divided by 100,000 that is 50,000 a second; exactly it is about 58,000. At three times, roughly 175,000. So this is a fleet, not a machine.

  3. You

    One view is 100 posts of 1 KB, so 100 KB. 58,000 times 100 KB is 5.8 GB a second, about 46 Gb/s on average and 140 at peak. So text alone is many network cards; I would compress it and send fewer posts first.

  4. You

    Servers: 175,000 over 10,000 times 0.7 is 25, plus one spare is 26. So about 26, if the 10,000 survives a load test. It looks generous for 100 posts a view.

  5. Interviewer

    Some would say 12.

  6. You

    That is 58,000 over 10,000, doubled. It uses the average, and a peak of three times would overload it.

Practice 2

A URL shortener

100 million new links a month, 100 reads per write, 500 bytes, 5 years

SumWorkingResultSo
Writes100M / 2.5M sabout 40/s (38.6)any database
Reads40 × 1004,000/s, 12,000 at peakjust past one database's planning line: add a cache
Links100M × 12 × 56 billioncodes of 6 letters or more
Storage6B × 500 B3 TB, or 9 TB with 3 copiesno need to split the data yet
The full design, at a hundred million a day instead, is in URL shortener.

Practice 3

A video service

Given: 1 million viewers at once at peak, 5 Mb/s each, 1,000 new videos a day of 1 GB, kept forever.

SumWorkingResultSo
Bytes out1M × 5 Mb/s5 Tb/s500 cards of 10 Gb/s: serve from a content network
Storage1,000 × 1 GB × 365365 TB a yearobject storage, with a rule for rarely watched videos
Bytes in1,000 GB / 86,40011.6 MB/s, about 93 Mb/suploads are not the problem
The same system needs 5,000,000 Mb/s out and 93 Mb/s in. The sum tells you where to spend your time.

Doing it well

Habits and slips

Five habits

HabitWhySounds like
State assumptionsOthers can correct the input"Let us assume 100M daily users, 20 requests each."
Round hardSaves time, keeps the zeros right"86,400 is about 100,000."
Show the stepsA slip is easy to find"500M × 10 is 5B; over 100,000 is 50,000."
Sanity checkCatches a lost factor of 1,000"50,000 a second is a lot, but 500 million people is too."
Use the peakAverages hide the evening"10,000 average, 30,000 peak, so capacity for 43,000."

Do

  • Think out loud
  • Say 1.2 million
  • Say the unit: per second, GB, Gb/s
  • Use the peak, three times average
  • Base guesses on something, and say what
  • Stop at the order of magnitude

Avoid

  • Calculating in silence
  • Saying 1,234,567
  • Bare numbers without a unit
  • Only the average
  • Numbers out of thin air
  • Two decimals on a guess

Sanity check in three questions

Summary

The whole topic in one minute.

Clarify what is asked, assume and say the inputs aloud, figure with rounded numbers, then evaluate: does the answer make sense, and what does it decide?

Five sums cover most designs: traffic, storage, bandwidth, machines and cache. A day is 86,400 seconds (divide by 100,000 in your head), an ordinary peak is three times the average, and a machine is planned at about 70% of what it can do. Being off by two rarely matters; being off by ten does.

  • Clarify, assume, figure, evaluate.
  • Round first, keep units, show the steps.
  • Traffic, storage, bandwidth, machines, cache.
  • Peak is three times average; plan machines at 70% plus one spare.
  • Off by two is fine, off by ten is another design.

Sources