Sub topic 1
Why estimate before you design
Every system design starts with a vague requirement such as "build a photo-sharing service for ten million users". Before you pick databases, caches, or a number of servers, you need a rough idea of how big the problem is. Back-of-the-envelope estimation gives you that idea in a few minutes, using only arithmetic and a short list of stated assumptions.
This chapter explains what estimation is for, what a good estimate looks like, and where estimates go wrong. Later chapters teach the individual calculations.
Prerequisites
This chapter assumes you already have the following:
- Comfortable arithmetic with large numbers and powers of ten, such as 10^6 and 10^9.
- A working picture of client-server architecture: clients send requests, servers answer them.
- A basic idea of what a database, a cache, and a load balancer do.
- Optional: the ability to read short Python snippets. Every snippet here is also explained in words.
You do not need to know the reference figures yet. Powers of two, time conversions, and latency numbers each get their own chapter.
What an estimate is
A back-of-the-envelope estimate is a calculation you could do on the back of an envelope. It uses a few input numbers, rounds them aggressively, and produces an answer that is correct to within a factor of about two to ten. The goal is the right order of magnitude. It is not the right digit.
Order of magnitude means the power of ten. The difference between 1,000 and 10,000 requests per second changes your design. The difference between 1,150 and 1,200 does not.
An estimate is not a forecast, a benchmark, or a capacity plan. A forecast predicts what will happen. A benchmark measures a real system. A capacity plan commits money. An estimate only tells you which design options are plausible.
Why estimate before you design
Estimates matter because system design is a series of trade-offs, and most trade-offs depend on scale. The same requirement leads to very different designs at different sizes.
| Question | If the estimate is small | If the estimate is large |
|---|---|---|
| Do we need more than one database node? | One node plus a replica may be enough | Sharding (splitting data across nodes) is required |
| Do we need a cache? | Probably not for throughput | Likely, to protect the database |
| Can all data fit on one disk? | Yes, simple storage | No, a distributed store is needed |
| Is the network a bottleneck? | No | Possibly, so consider a CDN or compression |
An estimate helps in four specific ways.
- It removes designs that cannot work. If your data grows by 90 TB over five years, a single-server database is out. You learn this in two minutes instead of after building it.
- It avoids over-engineering. If peak traffic is about 2,000 requests per second, a fleet of 50 servers and a sharded database is waste. It adds cost and operational risk.
- It points to the bottleneck. Traffic, storage, bandwidth, and memory each grow differently. The estimate shows which one dominates, so you spend design effort there.
- It makes the discussion concrete. "We need a lot of capacity" cannot be argued with or corrected. "We need about 2,300 requests per second at peak" can be.
The estimate also supports communication. In a design review or interview, your stated numbers let others check your reasoning and correct a wrong input without redoing the whole analysis.
The estimation workflow
Good estimates follow the same loop each time.
The loop has two properties worth noting. First, the assumptions come before the calculation and are written down. Second, the loop closes: once real measurements exist, they replace your guesses.
A first worked example
Suppose the requirement is: "10 million people use the service each day, and each makes about 10 requests." Daily active users (DAU) is the number of distinct users who use the service in a day. The calculation below turns that into queries per second (QPS), the number of requests the system must handle each second.
dau = 10_000_000
requests_per_user_per_day = 10
seconds_per_day = 86_400
peak_factor = 2
avg_qps = dau * requests_per_user_per_day / seconds_per_day
peak_qps = avg_qps * peak_factor
print(round(avg_qps)) # 1157
print(round(peak_qps)) # 2315Each input is an assumption. The peak factor of 2 says that the busiest moment carries about twice the average load. You will refine that idea in a later chapter. For now, notice that the whole calculation is one division and one multiplication.
By hand, you would round this. A day is about 100,000 seconds, so 100 million requests per day is about 1,000 QPS on average, and about 2,000 QPS at peak. That is within 15% of the exact answer, and it takes a few seconds.
Now use the number to decide something. The next snippet compares peak load to an assumed capacity for one database node, and shows how the answer changes as traffic grows.
import math
peak_qps = 2315 # from the previous calculation
node_reads_per_second = 5_000 # assumption: one database node, simple reads
headroom = 0.5 # assumption: plan to use only half the capacity
usable = node_reads_per_second * headroom
for growth in (1, 10, 100):
nodes = math.ceil(peak_qps * growth / usable)
print(f"{growth}x traffic: {nodes} node(s)")
# 1x traffic: 1 node(s)
# 10x traffic: 10 node(s)
# 100x traffic: 93 node(s)At today's load, one node covers the traffic, so a replica for availability is the main concern and sharding is not. At 100 times the traffic, the answer changes to about 93 nodes, and the design needs a different approach: caching, sharding, or both. The estimate does not make this decision for you. It shows you where the decision point lies.
What makes an estimate good
A useful estimate has these properties.
- Assumptions are stated. Every input is written down, so a reviewer can challenge the input instead of the arithmetic.
- Numbers are rounded. Use 86,400 seconds as 100,000, or 500 bytes as 0.5 KB, when that makes the math easier. Rounding errors are small compared to the uncertainty in the inputs.
- Units are carried through. Write "requests per day" and "bytes per request" next to each number.
- The result is checked. You compare it to something you know, such as a known system or a hardware limit.
- The precision matches the inputs. If your input is "about 10 million users", an answer of 1,157.4 QPS claims precision you do not have. Say "about 1,000 to 1,200".
When estimating is not enough
Estimation has limits, and it helps to know them early.
- Inputs may be wrong by a large factor. If you guess 10 requests per user per day and the real number is 200, every later number is off by 20 times. Estimates inherit the errors of their inputs.
- Real systems have behavior that arithmetic ignores. Hot keys (a few very popular items), retry storms, uneven data distribution, and slow dependencies all break average-based reasoning.
- Estimates do not replace testing. For a decision that costs real money, such as buying hardware or signing a capacity commitment, measure with a load test.
A good habit is to ask which assumption, if wrong, would change the design. Spend your checking effort there. If doubling peak traffic would not change your design, the exact peak factor is not worth debating.
Common mistakes in the purpose of estimating
Teams often misuse estimation in a few predictable ways.
- Estimating after deciding. If you pick the architecture first and then find numbers to justify it, the estimate is decoration. Do the estimate first, even if it is rough.
- Estimating everything. Not every number matters. Choose the two or three quantities that most affect the design, usually traffic, storage, and one of bandwidth or memory.
- Hiding assumptions. An estimate that shows only the final number cannot be reviewed.
- Treating the first answer as final. The loop in the diagram returns to assumptions on purpose. Update the estimate when you learn more.
- Stopping at average load. Systems fail at peak, not at the average. The peak-versus-average chapter returns to this.
What comes next
The remaining chapters build the toolkit this chapter motivates. You will learn to state and round assumptions, memorize powers of two, time conversions, and latency orders of magnitude, and then chain them into estimates for traffic, storage, bandwidth, cache memory, server count, and availability. A full URL shortener example brings them together, and the last chapter covers sanity checks and pitfalls.
Practice
Exercise
A teammate says, "We expect about 20 million daily active users, each making 5 requests per day. Let's shard the database from day one." Using a day of about 100,000 seconds, estimate the average QPS. Then state what else you need to know before agreeing that sharding is justified.
Show answer
Total requests per day are 20 million x 5 = 100 million. Divided by about 100,000 seconds, the average is about 1,000 QPS. Peak might be about twice that, around 2,000 QPS. Before agreeing to shard, you need the capacity of one database node for this kind of query (an assumed figure, such as several thousand simple reads per second), the share of requests that are reads versus writes, the expected storage size over the retention period, and the growth rate. At about 2,000 peak QPS, a single node with replicas is plausible, so sharding is likely premature unless storage or write volume is much larger than the request count suggests.
Exercise
Name two reasons an estimate can be badly wrong even when the arithmetic is correct, and say what you would do about each.
Show answer
First, an input assumption may be wrong by a large factor, such as requests per user per day. Write the assumption down, find the one that would change the design if wrong, and check it against data from a similar system or a pilot. Second, average-based arithmetic can miss real behavior such as peak traffic, hot keys, or retries. Apply a peak factor, consider uneven distribution, and confirm important decisions with a load test.