From daily users to requests per second
After this: Turn users and actions per day into average and peak requests per second, and split them into reads and writes.
On this page · 6 sections · 3 checks
"Ten million users" counts people. A server never sees people. It sees requests arriving, a few at a time, every second. In lesson 1 the school site needed one sum to go from one to the other. This lesson does the same sum carefully, for a bigger service.
From a day to a second
A is one message from a user's device asking a server to do something, such as "send me this photo". Take a warm-up service: 10 million daily active users, each making 20 requests a day.
- Requests per day: 10,000,000 × 20 = 200,000,000.
- Spread over the 86,400 seconds of a day: 200,000,000 ÷ 86,400 = 2,314.8. That is about 2,315 per second.
- The quick way: divide by 10⁵ instead, by removing five zeros. 200,000,000 becomes 2,000. It has four digits too, so the quick answer is in the right order of size.
The number you get is the service's . In interviews and in documentation you will meet it as QPS, short for "queries per second". This tutorial says requests per second.
Average and peak
The sum above assumes that people use the app as much at 3 in the morning as at 8 in the evening. They do not. Most people use an app at the same busy hours, and far fewer at night.
So 2,315 is the : the day's total shared out evenly. The busiest moment of the day is the . Servers have to cope with the peak. If they are planned for the average, they fall behind every evening, and a waiting line builds up as in tutorial 1.
How much higher is the peak? Without a running service to measure, you have to guess. This tutorial assumes the peak is 2 × the average. That is an assumption, not a rule: some services see five times their average or more. For the warm-up service, the peak is 2 × 2,314.8 = 4,629.6, about 4,630 requests per second.
The calculator
The same steps as a program. Change users or per_userperUser and run it again.
The lines for the users, the requests per user and the requests per day hold the assumptions and the first step. The next line divides by 86,400 for the average per second. The two print lines show the average and the peak, each rounded to a whole number: 2315 and 4630.
Snapshot: reads and writes
Snapshot's users do two different things. Most requests only look at data: show me this photo. That is a . A few add or change data: save my new photo. That is a . Count them apart, because different parts of a system handle them.
Reads, from the table in lesson 1 (50 views each):
- Views per day: 10,000,000 × 50 = 500,000,000.
- Per second: 500,000,000 ÷ 86,400 = 5,787 on average.
- At the peak, 2 × average: 11,574.
Writes (0.1 uploads each):
- Uploads per day: 10,000,000 × 0.1 = 1,000,000.
- Per second: 1,000,000 ÷ 86,400 = 11.6 on average.
For every upload there are 500 views: per user, 50 ÷ 0.1 = 500. This is the read to write ratio, 500 : 1. A service where reads outnumber writes this much is called . The tutorials on caching and on copies of the database are about making reads cheap, for exactly this kind of service.
Try 100 views per user instead of 50. Every view number doubles, and the ratio becomes 1,000 : 1.
Check yourself
A service has 1 million daily active users, and each makes 40 requests a day. About how many requests per second does it get on average?
Complete the line that turns the requests per day into the average requests per second, so that the program prints 2315 and then 4630. Keep the name per_second: the lines below use it.Make it a double named perSecond: the lines below use it.
users = 10_000_000per_user = 20per_day = users * per_user_______________________________ # ← requests per second, on averageprint(round(per_second))print(round(per_second * 2))public class Main { public static void main(String[] args) { long users = 10_000_000; long perUser = 20; long perDay = users * perUser; _______________________________ // ← requests per second, on average System.out.println(Math.round(perSecond)); System.out.println(Math.round(perSecond * 2)); }}Snapshot's users still view 50 photos a day, but now each uploads 0.5 photos a day instead of 0.1. What is the read to write ratio now?
Interview lens and honesty
- Turn users and requests per user into requests per day, then divide by 86,400 for the average per second: 200 million a day is about 2,315 per second.
- Plan servers for the peak, not the average, and state the multiplier you assume: here 2 × average.
- Split requests into reads and writes: Snapshot has 5,787 views and 11.6 uploads per second, a ratio of 500 : 1.
- In Java, divide by 86_400.0 rather than 86_400, or the fraction is dropped.