Sub topic 1
Why Estimate Before You Design
Prerequisites
This chapter assumes you can do basic arithmetic with large numbers and exponents. It also assumes you know what a client, a server, a database, a cache and a load balancer do. Reading short Python snippets helps but is optional.
What a back-of-the-envelope estimate is
A back-of-the-envelope estimate is a rough calculation that gets you to the right order of magnitude. An order of magnitude is a power of ten. Knowing that a system needs about 1,000 requests per second, and not 100 or 100,000, is usually enough to start designing.
You do not need exact figures. You need figures that are close enough to tell designs apart.
Why estimate first
Design choices depend on scale. The same feature can need very different systems at different sizes.
- At 10 requests per second, one server and one database are enough.
- At 10,000 requests per second, you need several servers, a load balancer and probably a cache.
- At 1,000,000 requests per second, you need to split the data across many machines and think about regions.
An estimate tells you which of these cases you are in. Without one, you may build too little and fail under load. You may also build too much and pay for machines that sit idle.
Estimates also help in three other ways:
- They expose the bottleneck. Storage, network, memory or CPU will hit its limit first, and the estimate shows which.
- They let you reject ideas early. If keeping every record in memory needs 90 TB of RAM, you can drop that idea in a minute.
- They give a team a shared set of numbers to argue about. A disagreement over an assumption is easier to settle than a disagreement over a feeling.
The estimation workflow
Most estimates follow the same steps. Later chapters cover each one.
A first example
Suppose a product manager says, "We expect 10 million daily active users, and each one makes about 10 requests a day." Is that a big system or a small one?
Convert it to requests per second. A day has about 86,400 seconds.
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)) # 2315QPS means queries per second. The average is about 1,200 QPS, and the peak is about 2,300 QPS if you assume traffic at its busiest is twice the average. That is a modest load. A few application servers behind a load balancer can handle it. You have just ruled out a very complex design, and it took one short calculation.
What estimates can and cannot do
An estimate is only as good as its assumptions. If your assumption about requests per user is off by 10 times, your answer is off by 10 times. That is acceptable when you only need the order of magnitude. It is a problem if you treat the result as exact.
Use estimates to choose a direction, then replace them with measurements once you have real traffic.
Exercise
A service has 1 million daily active users, and each makes 20 requests per day. Using 86,400 seconds per day, roughly what is the average QPS, and would a single server likely be enough?
Show answer
1,000,000 × 20 = 20,000,000 requests per day. Dividing by 86,400 gives about 231 QPS, or roughly 200 to 250. A single modern server can often handle this, though you would still want a second one for availability.