AlgoViz

Numbers that decide

easy 35 min

An estimate is worth doing when its answer changes what you draw.

Video lesson · Chapter 1 of 5

Twenty million views and one decision

0:00 / 3:27

Four sums, one minute each

1:25Reel coming soon

Theory

Read or listen first. About 1 min.

What it is

An estimate is a quick calculation with rounded numbers that is good enough to make a decision. In design it is often called back-of-the-envelope arithmetic.

The aim is the right order of magnitude. Knowing whether you face a thousand or a million requests a second matters. The third digit does not.

Why it exists

Parts are added to a design because something is too big or too fast for what you have. Only a number can tell you that.

It works the other way too. A number often shows that one machine is enough, which saves everyone work.

A short history

  1. 1945

    Enrico Fermi estimates the strength of the first atomic test by dropping scraps of paper, a famous example of useful rough reckoning.

  2. 1984

    Jon Bentley writes about back-of-the-envelope calculation for programmers in Communications of the ACM.

  3. About 2009

    Jeff Dean's talks at Google spread a table of latency numbers every engineer should know.

  4. 2018

    Google's SRE Workbook includes a chapter on designing with real quantities.

Words to know

Order of magnitude
A power of ten.
QPS
Queries, or requests, per second.
Peak
The busiest moment; we use three times the average.
Throughput
How much work per second.
Latency
How long one piece of work takes.

How it works

Step through the idea, or press Play.

Step 1 of 3

Ask. Which decision is waiting for a number?

Understand it

Start here

One van or a lorry?

Moving house, you do not weigh every spoon. You count the boxes, because the count decides what to rent. For one lamp you count nothing. Estimation in an interview is the same: find the sum that decides, and leave the others.

Counting boxes, and knowing when not to
Get a lorry!The guess

When

Calculate, or say no

Is this sum worth a minute?

Which sum decides what

SumDecidesSkip when
Peak reads a secondCache, replicas, edgeA few hundred a second
Peak writes a secondOne database, batching, queue, splitA few hundred a second
Storage over the yearsOne database or partitions, object storage, deletionClearly under 1 TB
Open connectionsNumber of gateways, memoryNothing is pushed
Bytes out a secondCDN, image sizesResponses are tiny
Size of the hot setCache memoryYou have no cache

Sum 1

Per day to per second, then peak

The only conversion you need

per second≈per day100,000,peak≈3×average\text{per second} \approx \frac{\text{per day}}{100{,}000}, \qquad \text{peak} \approx 3 \times \text{average}
A day has 86,400 seconds, so the shortcut is 14% low. Estimates are read in factors of ten, so that is fine.

Recipe pages

1M users×20100,000=200/s,peak=600/s\frac{1\text{M users} \times 20}{100{,}000} = 200/\text{s}, \qquad \text{peak} = 600/\text{s}
Exactly 231 and 694. Decision: one database can serve this. Add an index, not a cache.

Three times is for an ordinary day with a busy evening. When people arrive because a clock says so, count the people instead.

The 9:00 rush on library seats

20M map views/day100,000×3=600/sbut20,000 readers4 s=5,000/s\frac{20\text{M map views/day}}{100{,}000} \times 3 = 600/\text{s} \quad\text{but}\quad \frac{20{,}000\ \text{readers}}{4\ \text{s}} = 5{,}000/\text{s}
The daily average hides the rush by a factor of eight. Decision: cache the seat map.

Sum 2

Storage over time

Items a day, times size, times 365, times years, times copies. Do text and media separately, because they live in different places.

Recipe photos for five years

10,000×4×500 KB=20 GB/day,20×365×5=36.5 TB10{,}000 \times 4 \times 500\,\text{KB} = 20\,\text{GB/day}, \quad 20 \times 365 \times 5 = 36.5\,\text{TB}
Three copies would be about 110 TB. Object storage keeps its own copies, so use 36.5 TB there. Text at 5 KB a recipe is 50 MB a day, about 91 GB in five years. Decision: photos in object storage, text in one database.

Sum 3

Connections and work in progress

Work in progress

in progress=rate×time each takes,3,000/s×0.005 s=15\text{in progress} = \text{rate} \times \text{time each takes}, \qquad 3{,}000/\text{s} \times 0.005\,\text{s} = 15
3,000 queries a second of 5 ms each keep 15 connections busy on average. PostgreSQL allows 100 by default, so this fits. If a query slows to 50 ms it becomes 150 and no longer does.

Live connections for the bus tracker

100,000×20 KB=2 GB,100,0005 s=20,000 messages/s100{,}000 \times 20\,\text{KB} = 2\,\text{GB}, \qquad \frac{100{,}000}{5\,\text{s}} = 20{,}000\ \text{messages/s}
20 KB a connection is an assumption. Each parent gets one position every 5 seconds. Decision: memory is not the limit; use two or three gateways so one can fail.

Sum 4

One machine or many

Machines

n=⌈peakcapacity×0.7⌉+1,⌈3,5001,000×0.7⌉+1=5+1=6n = \left\lceil \frac{\text{peak}}{\text{capacity} \times 0.7} \right\rceil + 1, \qquad \left\lceil \frac{3{,}500}{1{,}000 \times 0.7} \right\rceil + 1 = 5 + 1 = 6
Run at about 70% so that a bump does not hurt, and keep one spare so that one can fail. Capacity must come from a test; 1,000 a second is an assumption.

Reading the result

Planning lines (rules of thumb)

Load on one relational databaseSuggestsWhy it is only a hint
Under about 10,000 simple reads a secondEnough as it isA heavy query can use a whole core
Over about 10,000 simple reads a secondCache or replicasWell-tuned machines go further
Over about 10,000 small writes a secondBatch, queue or splitDepends on disks, indexes and durability
Over about 30 TBPartition the dataBackups and restores become slow first
Say them as estimates and offer to measure. One PostgreSQL table is limited to 32 TB by default, and can be partitioned.

Head maths

Shortcuts worth knowing

  • Million a day

    About 10 a second (11.6).

  • Billion a day

    About 10,000 a second (11,600).

  • One a second

    About 100,000 a day (86,400), 30 million a year (31.5).

  • Thousands

    KB × thousand = MB, × million = GB, × billion = TB.

  • Bits and bytes

    Divide by 8. A 10 Gb/s link moves about 1.25 GB a second.

  • A year

    Use 400 days instead of 365. It is 10% high, a safe side for storage.

Doing it aloud

  1. 1

    Round first

    86,400 becomes 100,000. 365 becomes 400. 487 KB becomes 500 KB.

  2. 2

    Split off the zeros

    20 million is 2 and seven zeros. Subtract five zeros for 100,000: 2 and two zeros, 200.

  3. 3

    Keep the unit

    Say "200 reads a second", never "200".

  4. 4

    Sense check

    One million users cannot make a billion clicks a day unless each clicks 1,000 times.

  5. 5

    End with so

    "So one database is enough."

Sizes

Latency and throughput, by order of magnitude

These are rules of thumb. Hardware and settings move them by two to ten times. Use them to see which part of a request is slow, then measure.

Latency

ActionOrder of magnitudeBasis
Read from memory100 nsRule of thumb
Small read from a local SSD100 µsRule of thumb
Round trip inside one zoneunder 1 msRule of thumb
Redis lookup over the networkabout 1 msRule of thumb, mostly network
Cloud disk (AWS gp3)single-digit msAWS documentation
Indexed database lookup1 to 5 msRule of thumb
Round trip between regions10 to 250 ms by distanceMicrosoft publishes a table for Azure
First byte from S3, small object100 to 200 msAWS documentation

Throughput and limits

ThingOrder of magnitudeBasis
One application serverhundreds to thousands of requests/sRule of thumb, depends on the work
One relational database, simple lookups10,000/s to plan withRule of thumb
One Redis server, simple commands100,000/sRedis docs show a benchmark near 180,000
AWS gp3 disk3,000 operations/s included, up to 80,000AWS documentation
PostgreSQL connections100 by defaultPostgreSQL documentation
Network card at 10 Gb/s1.25 GB/sArithmetic
Published limits change. Check the page before quoting one at work.

Worked example 1

School bus tracker, word for word

Three sums, three decisions

  1. Interviewer

    2,000 buses, 100,000 parents. Do we need anything special?

  2. You

    Three sums will tell. Writes: 2,000 buses, one position every 5 seconds, is 400 a second. So one database could take the writes.

  3. You

    Reads: 100,000 parents polling every 10 seconds is 10,000 a second, all at 7:45. I will not use the daily average, because they come together. So I keep the newest position in a cache.

  4. You

    Storage: 400 a second for 4 hours is 5.8 million rows a day. At 100 bytes that is about 0.6 GB a day, 115 GB in a 200-day school year. So history stays in one database.

  5. Interviewer

    And bandwidth?

  6. You

    A position is about 100 bytes, so 10,000 a second is 1 MB a second before headers. Nothing depends on it. I will skip it.

Check the storage

400×4×3,600=5.76M rows,×100 B=576 MB,×200=115 GB400 \times 4 \times 3{,}600 = 5.76\text{M rows}, \quad \times 100\,\text{B} = 576\,\text{MB}, \quad \times 200 = 115\,\text{GB}

Worked example 2

Recipe photos: the number that forces a CDN

600 recipe pages a second at peak, four photos of 500 KB on each.

Bytes out

600×4×500 KB=1.2 GB/s≈10 Gb/s600 \times 4 \times 500\,\text{KB} = 1.2\,\text{GB/s} \approx 10\,\text{Gb/s}
That would fill a 10 Gb/s network card. Photos are public and never change, so a CDN can hold them. Decision: CDN, and smaller images for phones.

A number that decides

  • "10 Gb/s of photos, so a CDN."
  • "91 GB of text, so one database."
  • "5,000 a second at 9:00, so a cache."
  • "115 GB a year, so no new store."

A number that decorates

  • "That is 1,728,000,000 a month."
  • "Headers are 412 bytes..."
  • "We need 17.36 servers."
  • "Big. So it must scale."
Python
import mathdef machines(peak, capacity, use=0.7, spare=1):    return math.ceil(peak / (capacity * use)) + sparereads = 1_000_000 * 20 / 100_000print("recipe reads/s:", reads, "peak:", reads * 3)print("photo bytes/s at peak:", reads * 3 * 4 * 500_000 / 1e9, "GB")print("bus writes/s:", 2_000 / 5, "poll reads/s:", 100_000 / 10)print("machines for 3,500/s at 1,000 each:", machines(3500, 1000))
The same sums as code, each with its decision. Try changing the inputs.
Edit and run it in your browser

Summary

The whole topic in one minute.

Do a sum when its answer changes the design, use round numbers and end with the decision.

Per day divided by 100,000 gives per second, times three for the peak. If nothing hangs on a number, skip it.

  • No decision, no sum.
  • Divide a day by 100,000; three times for an ordinary peak.
  • A clock-driven rush: count people, not days.
  • Round hard, keep units, end with "so".
  • Limits are rules of thumb until measured.

Sources