Skip to content
Theory · Requirements and estimates · lesson 5 of 8

Requests per second

Picture the checkouts in a supermarket. On average 400 shoppers come in an hour. At six in the evening, when everyone is on the way home from work, 1,200 come. A cashier serves 60 people an hour. By the average, 7 checkouts would do, but at rush hour you need 20, or the queue stretches to the door.

A service is the same. The number of users a day sets the scale, while machines are counted by requests a second at the busiest moment. The analogy breaks at scale: a service has hundreds of "checkouts", and new ones start in minutes.

From users to requests

Start with the interviewer's answers about the photo feed: 100 million users a day, each opening the feed 10 times on average.

  1. 100 million × 10 = 1 billion feed opens a day.
  2. A day has 86,400 seconds, roughly 10⁵.
  3. 1 billion ÷ 10⁵ = 10,000, or more precisely about 12,000 opens a second.

This is the average. Rounding to 10⁵ is safe: the error is about 15%, and an estimate needs only the order of magnitude.

The peak

The load is uneven: at night the feed is barely opened, and in the evening everyone looks at once. The interviewer usually names the peak load. If not, state your assumption out loud.

In this course the peak is taken as 10 times the average. An ordinary evening peak is 2–3 times the average, and the margin up to 10 covers spikes such as a holiday or news everyone discusses. For the photo feed the peak is about 120,000 opens a second.

Machines are counted by the peak, not the average. A service sized for 12,000 requests a second fails nine requests out of ten at the peak.

Reads and writes

Posts are counted the same way:

  1. 10 million posts a day ÷ 86,400 ≈ 120 a second.
  2. At peak, about 1,200.

There are 100 times more feed reads than posts. The ratio shows which path is loaded: almost all the work is reads. One database handles 1,200 writes a second, but not 120,000 reads.

So reads and writes go along different paths. The feed gets its own service and its own copies of the data, while publishing goes to the primary database and does not queue up behind reads.

How many machines

The last step turns requests into machines. For that you need an assumption about what one service instance handles. A stateless service keeps nothing of its own between requests. For a simple one, say about 2,000 requests a second: the exact figure depends on the code, but the order is right.

  1. 120,000 ÷ 2,000 = 60 feed service instances at peak.
  2. Add about a third as a margin for failed machines and rolling out a new version: 80.
  3. Publishing needs one instance at peak, and two or three are run for reliability.

The number 80 itself does not matter to the interviewer. The conclusion does: the feed service needs dozens of instances behind a load balancer, the post service only a few.

Carry this conclusion over to the diagram.

Sketch

Feed opens: 12,000 a second on average, 120,000 at peak; new posts: 120 and 1,200. One service instance handles about 2,000 requests a second. The feed already has its own service, but it reads the same database the posts are written to. Fix the diagram.

A read replica is a copy of the primary database that serves only reads. How it catches up with the primary is covered in the databases course, in the lesson Streaming replication, and why it does not help with writes in the lesson Sharding. In an interview, name the cost: a replica lags by fractions of a second, so a new post does not show in the feed at once. The requirements allow it: a few seconds of delay are fine.

Common mistake

Counting machines by the average. The candidate gets 12,000 requests a second, divides by 2,000 and runs 6 instances. At the peak 120,000 requests arrive, and nine out of ten get an error. The number of machines is taken from the peak, with a margin on top.

Key points

  • Users a day × actions per user ÷ 86,400 = the average requests a second.
  • In this course the peak is 10 times the average unless the interviewer names another number; machines are counted by the peak.
  • The read-to-write ratio shows the loaded path: the photo feed has 100 times more reads.
  • Peak ÷ requests per instance + margin = the number of instances.