Everyone Can Learn Program
中文 Start learning
Specialty · System designLesson 1 of 5Free to read

Back-of-the-envelope estimates · System Design

How many servers does an app with a million users need?

Course contents

Why you need it

Every system design starts with sizes. One database or fifty? Is the data a few gigabytes or a few hundred terabytes? You can't measure a system that doesn't exist yet, but a quick estimate usually gets the order of magnitude right, and that's enough to rule out designs that would never work. Interviews ask for it too.

The intuition

Work from what you know to what you need, one multiplication at a time:

  • requests per day = daily users × requests per user
  • average per second = requests per day / 86,400 (the seconds in a day)
  • peak per second = average × a peak factor. Traffic isn't flat; use a factor measured from similar traffic if you have one, or pick one and write it down
  • servers = peak / what one server handles, rounded up. "What one server handles" comes from a load test, never from a guess
  • one spare so you survive a server failing (often written N + 1)

Storage works the same way: items per day × size × days kept × copies.

Keep the numbers round. 86,400 is close to 100,000, so a million requests a day is roughly 10 a second. The point is to be about right, quickly, and to show your assumptions so someone can correct them.

Definition

QPS

Queries (requests) per second: the usual unit of load.

Peak factor

How much busier the busiest time is than the average.

N + 1

Running one more server than the load needs, so one failure doesn't overload the rest.

Examples

From users to servers

Try it yourself

A million daily users, 20 requests each, peak factor 3, and a load test said one server handles 200 per second.

import math

daily = 1_000_000 * 20
average = daily / 86_400
peak = average * 3
servers = math.ceil(peak / 200) + 1
print(round(average), round(peak), servers)
Output
231 694 5
Step by step
  1. L320 million requests a day. Underscores just make big numbers readable.
  2. L4About 231 per second on average.
  3. L5About 694 per second at the busiest time.
  4. L6694 / 200 = 3.47 → 4 servers, plus 1 spare.5

Storage for photos

Try it yourself

Two million photos a day, 2 MB each, kept for a year, stored 3 times.

photos_per_day = 2_000_000
mb_each = 2
days = 365
copies = 3
tb = photos_per_day * mb_each * days * copies / 1_000_000
print(tb, "TB")
Output
4380.0 TB
Step by step
  1. L5MB → TB is a division by a million (in decimal units).
  2. L6Thousands of terabytes: one database server won't do; this needs object storage.

Common mistakes

Designing for the average

Traffic has peaks. A system sized for the average falls over every evening.

Guessing a server's capacity

"One server does 10,000 per second" depends entirely on the work per request. Load-test your own service.

Forgetting copies and growth

Backups, replicas and next year's growth often multiply storage several times over.

Quick quiz

1. What does this print?

per_day = 8_640_000
print(per_day / 86_400)
  1. A
    100.0
  2. B
    1000.0
  3. C
    10.0
Show the answer

Answer: A

8,640,000 / 86,400 = 100 per second.

2. Why add one server beyond what the peak needs?

  1. ASo the service survives one server failing
  2. BBecause servers only come in odd numbers
  3. CTo make the load balancer faster
Show the answer

Answer: A

N + 1: losing any one server still leaves enough capacity.

Practice

3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.

  1. PredictRound numbers

    Type exactly what this prints.

  2. ModifyCount every copy

    storage_tb forgets that each photo is stored copies times. Fix it.

  3. WriteHow many servers?

    Write servers_needed(daily_users, per_user, peak_factor, per_server):

    1. peak per second = daily_users × per_user / 86,400 × peak_factor
    2. servers for the load = peak / per_server, rounded up, but at least 1
    3. return that plus 1 spare
    servers_needed(1_000_000, 20, 3, 200)   # 5

Practise this lesson

Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.

Terms in this lesson

TermChineseDefinition
QPS每秒请求数Requests per second.
Peak factor峰值系数How much busier the peak is than the average.
N + 1N + 1 冗余One spare server beyond what the load needs.