Part 01 · Thinking About Scale

Back-of-the-Envelope Estimation

QPSStorageBandwidthPeak vs average

Estimate the shop's QPS, storage and bandwidth at 1M users and during the Eid sale, in five minutes on paper.

⏱ 9 min · Station 8 of 93

The problem

The planning meeting for next year: "We'll have a million users. What do we need?" One person says "a Kubernetes cluster", another "Cassandra", a third "it'll be fine". None of them has done the arithmetic, so none of them can be argued with.

Five minutes on paper turns "a million users" into numbers you can design against: requests per second at the average, at the peak and during the Eid sale; bandwidth; how much data piles up per year. That's back-of-the-envelope estimation: rough, fast, and almost always enough to tell whether you need one server or fifty.

The idea

Start from something you know (users, orders, uploads), multiply by assumptions you can defend, and stop when you have the numbers that decide the design:

  • Requests per second, at average, peak and special events. This sizes servers.
  • Bandwidth: requests per second × bytes per response. This decides whether you need a CDN.
  • Storage: new records per day × bytes per record × retention. This decides whether one database can hold it.
  • Read/write ratio: what share of requests change data. This decides whether caching and read replicas will help.

Three rules keep the arithmetic honest:

  1. Round aggressively. 86,400 seconds in a day is "about 100,000". Being 15% off doesn't change a design; being 10× off does.
  2. Write the assumptions down. "20 requests per user per day" is a guess. Written down, someone can challenge it, and you can change it and re-run.
  3. Design for the peak, not the average. Traffic isn't flat: evenings, salary days and sales concentrate it. The average tells you about cost; the peak tells you about capacity.

How it works

For the shop at 1,000,000 daily users:

  1. Requests per day: 1,000,000 users × 20 API requests each = 20,000,000.
  2. Average rate: 20,000,000 / 86,400 s ≈ 231 requests per second.
  3. Peak: the busiest minute is about 4× the average: ≈ 926 per second.
  4. Eid sale: 20× a normal day's average: ≈ 4,630 per second for a few hours.
  5. Bandwidth: the API sends small JSON (516 bytes measured), so 926/s × 516 B ≈ 4 Mbit/s. Images are another story: if 40% of requests are product views with 6 images of about 100 KB, that's ≈ 1.8 Gbit/s at peak, almost 9 Gbit/s during Eid.
  6. Storage: 2% of users order each day = 20,000 orders × ~1 KB ≈ 20 MB a day, ≈ 7 GB a year.

Now compare with what we measured in this part: one database thread handles about 560 requests per second, two threads about 1,075.

  • 231/s average: one small server is fine today.
  • 926/s peak: past one thread, inside two, with no headroom: needs the index from the bottleneck lesson and/or caching.
  • 4,630/s on Eid: 8× the single-thread capacity. No single change gets there; this is what the rest of the course is for.
  • 1.8 Gbit/s of images: not something the app servers should send. That's a CDN's job (Part 4).
  • 7 GB a year of orders: tiny. Any single PostgreSQL holds decades of it. Sharding orders for size would be premature (Part 6 discusses when you'd shard anyway).
Predict first

1,000,000 daily users, each making 20 requests a day. Which number should you size the servers for?

Visual

1,000,000daily users20,000,000requests a dayx 20 requests each231 /saverage/ 86,400 s926 /sbusiest minutex 4 peak factor4,630 /sEid salex 20 the averageimages: 1.8 Gbit/sat peak, 600 KB per product vieworders: 7 GB/year20,000 a day, ~1 KB eachstart from a number you know, multiply by assumptions you can defendthe average is calm; the peak and the sale are what break serversthen size bandwidth and storage from the same peak: images need a CDN, orders fit anywhere
From a million daily users to the rates that size the shop, one multiplication at a time, then bandwidth and storage from the same peak.

Common misconception

"A million users means a big distributed system." It means about 230 requests per second on average and under a thousand at peak for this shop. That's one well-indexed database with a couple of app servers. What actually forces bigger designs here is the 20× sale and the image bandwidth, which the estimate shows in minutes.

"Estimates are useless because they're not precise." They're for choosing between designs that differ by 10× or more: one server or a fleet, a CDN or not, one database or shards. For that, being within a factor of two is plenty. Precise numbers come later, from load tests.

Try it

Two numbers came from the lab. The size of one product page response, and bytes per stored row (we insert 100,000 orders inside a transaction, measure the table, and roll back so the lab stays unchanged):

cd ../../lab
echo '$ curl -s -o /dev/null -w "%{size_download}" localhost:58080/products/42'
curl -s -o /dev/null -w "%{size_download} bytes\n" localhost:58080/products/42
echo
echo '$ psql: bytes per row, measured by inserting 100,000 orders and rolling back'
docker compose exec -T db psql -U shop -q <<'SQL'
BEGIN;
INSERT INTO orders (user_id, total) SELECT 1 + g % 100000, 500 + g % 9000 FROM generate_series(1, 100000) g;
SELECT 'orders'  AS "table", pg_size_pretty(pg_total_relation_size('orders'))  AS size,
       pg_total_relation_size('orders') / 100000 AS bytes_per_row
UNION ALL
SELECT 'reviews', pg_size_pretty(pg_total_relation_size('reviews')),
       pg_total_relation_size('reviews') / (SELECT count(*) FROM reviews);
ROLLBACK;
SQL
$ curl -s -o /dev/null -w "%{size_download}" localhost:58080/products/42
516 bytes

$ psql: bytes per row, measured by inserting 100,000 orders and rolling back
  table  |  size   | bytes_per_row
---------+---------+---------------
 orders  | 8120 kB |            83
 reviews | 71 MB   |            73
(2 rows)
  • 516 bytes per product page. Small JSON; the API's own bandwidth will never be the problem.
  • 83 bytes per order row, index included. Our lab's orders table has only 4 columns. A real order with its items, address and payment details is several rows across several tables, so the estimate uses 1 KB per order: 12 times what we measured, to be safe.

Everything else is an assumption, so it goes into a script you can change and re-run instead of a spreadsheet nobody trusts:

# Back-of-the-envelope numbers for the shop at 1,000,000 daily users.
# Every input is an assumption you can argue with; change one and re-run.
DAILY_USERS = 1_000_000
REQUESTS_PER_USER = 20      # API requests per user per day (pages, cart, search)
PEAK_FACTOR = 4             # busiest minute vs the daily average
EID_FACTOR = 20             # an Eid sale vs a normal day's average
PAGE_BYTES = 516            # product page JSON, measured in the lab
IMAGE_BYTES = 6 * 100_000   # 6 images of ~100 KB on each product page view
PRODUCT_SHARE = 0.4         # share of requests that are product page views
ORDER_RATE = 0.02           # 2% of daily users place an order
ORDER_BYTES = 1_000         # one order with items, address, payment (our lab row is 83 B)
SECONDS_PER_DAY = 86_400

avg = DAILY_USERS * REQUESTS_PER_USER / SECONDS_PER_DAY
peak = avg * PEAK_FACTOR
eid = avg * EID_FACTOR
api_mbit = peak * PAGE_BYTES * 8 / 1e6
img_mbit = peak * PRODUCT_SHARE * IMAGE_BYTES * 8 / 1e6
orders_day = DAILY_USERS * ORDER_RATE
order_gb_year = orders_day * ORDER_BYTES * 365 / 1e9

rows = [
    ("requests per day", f"{DAILY_USERS * REQUESTS_PER_USER:,.0f}"),
    ("average requests/s", f"{avg:,.0f}"),
    ("peak requests/s (x4)", f"{peak:,.0f}"),
    ("Eid sale requests/s (x20 avg)", f"{eid:,.0f}"),
    ("API bandwidth at peak", f"{api_mbit:,.1f} Mbit/s"),
    ("image bandwidth at peak", f"{img_mbit:,.0f} Mbit/s"),
    ("image bandwidth, Eid", f"{img_mbit * EID_FACTOR / PEAK_FACTOR:,.0f} Mbit/s"),
    ("orders per day", f"{orders_day:,.0f}"),
    ("order data per year", f"{order_gb_year:,.1f} GB"),
]
for name, value in rows:
    print(f"{name:<32}{value:>16}")
requests per day                      20,000,000
average requests/s                           231
peak requests/s (x4)                         926
Eid sale requests/s (x20 avg)              4,630
API bandwidth at peak                 3.8 Mbit/s
image bandwidth at peak             1,778 Mbit/s
image bandwidth, Eid                8,889 Mbit/s
orders per day                            20,000
order data per year                       7.3 GB
EstimateCompared with what we measuredVerdict
231 req/s averageone database thread: ~560/sfine today
926 req/s peaktwo threads: ~1,075/sat the edge: fix the bottleneck first
4,630 req/s Eid8× one threadneeds caching, more capacity, protection
1.8 to 8.9 Gbit/s imagesapp servers send JSON onlyCDN, not app servers
7 GB/year ordersone PostgreSQL holds TBsno sharding for size

Trade-offs

  • Estimates are only as good as their assumptions. "20 requests per user" might be 60 once the app adds infinite scroll. Re-run the numbers when the product changes, and validate them against real traffic once you have it.
  • Peak factors vary a lot. 4× is common for consumer apps; a news site on a big day, a ticket sale or an exam result can be 50×. Look at your own traffic graphs when you have them.
  • Don't stop at requests per second. The image bandwidth was the most surprising number here, and the one that most changes the architecture.
  • Don't over-engineer from estimates. 4,630 per second during Eid is a few hours a year. Pre-scaling and caching for those hours is usually cheaper than running a system that size all year.

Key takeaway

Turn users into rates (average, peak, special events), then into bandwidth and storage, with written-down assumptions and rounded numbers. Compare the results with measured capacity: that comparison tells you what to build, and just as often, what not to build yet.

Practice

Objective: estimate the write load of the Eid sale.

Challenge: the sale lasts 3 hours. 300,000 users place one order each. Most orders arrive in the first hour, at about 3× the sale's average rate. Each order commits one transaction, and the lab measured about 2.3 ms for one fsync. Estimate orders per second (average and peak) and decide whether a single PostgreSQL can commit them.

Expected result: two rates, and a reasoned yes or no with the number that decides it.

Hint 1

3 hours is 10,800 seconds.

Hint 2

If every commit waited for its own fsync one after another, how many commits per second could the disk do?

Hint 3

PostgreSQL doesn't do one fsync per commit when many arrive together: concurrent commits share one flush of the write-ahead log (the Database Internals course shows how).

Solution
  • Average: 300,000 / 10,800 s ≈ 28 orders per second.
  • Peak: about 3 × 28 ≈ 83 orders per second in the first hour.
  • Can one PostgreSQL commit them? Even in the worst case, one commit after another with a 2.3 ms fsync each, the disk manages about 1 / 0.0023 ≈ 430 commits per second, five times the peak. With group commit (many transactions sharing one flush), much more.

So the sale's writes fit easily. The danger is the reads: 4,630 requests per second of browsing, most of it the same few discounted products. That's a caching problem (Part 3), not a database-write problem. Estimating both sides is how you find out which one to worry about.

Interview check

Q: Estimate the storage needed for a photo-sharing app with 10 million daily users, 10% of whom upload 2 photos a day.

Model answer

10M × 10% × 2 = 2M photos a day. At about 2 MB per original plus a few resized copies (say 3 MB total), that's 6 TB a day, about 2.2 PB a year, before replication (×3 is common, so ~6.5 PB). Metadata is tiny in comparison: 2M rows a day at ~500 bytes is 1 GB a day. Conclusion: photos go to object storage (S3-style) with lifecycle rules, metadata to a normal database; the design question is the blob store and the CDN, not the database.

Q: How do you choose a peak factor if the product hasn't launched yet?

Model answer

Borrow from similar products and from the business: when do people use it (evenings, weekends, payday), are there events (sales, launches, push notifications to everyone at once)? Start with a defensible number like 3 to 5× the average for daily patterns, plan events separately with their own factor, and design so capacity can be added quickly. Once real traffic exists, replace the guess with the measured ratio of the busiest minute to the daily average.

Station stamp

Finish the chapter

Done reading? Press Complete to save your progress. XP is Pro-only — on Pro this chapter is worth +10 XP.

Go Pro to earn XP →