What one server can do
After this: Tell latency from throughput, work out 400 requests per second, and follow the waiting line when 500 arrive.
On this page · 4 sections · 3 checks
So far one person opened the shop's page. A real shop has thousands of visitors, and on a sale day many of them arrive in the same second. How many requests can one server handle, and what happens when more arrive than that?
Two different questions
Think of a coffee bar. "How long does one coffee take?" is one question. "How many coffees does the bar make in an hour?" is another. A second barista does not make your coffee any faster, but the bar makes twice as many.
A server has the same two numbers. Lesson 4 measured the first one, latency: how long one request takes. The second is : how many requests the server finishes per second.
Workers, by hand
A server program handles several requests at the same time. Each part of it that handles one request at a time is called a . Take a server with 8 workers, where one request keeps a worker busy for 20 ms: the 16 ms of database queries and 4 ms of work from lesson 4.
- One worker finishes one request every 20 ms. A second is 1,000 ms, so one worker finishes 1,000 ÷ 20 = 50 requests per second.
- Eight workers do this side by side: 8 × 50 = 400 requests per second.
The rule: requests per second = workers × (1,000 ÷ ms per request). Two more workers would raise the throughput, but each request would still take 20 ms.
When more arrive than it can finish
On sale day, 500 requests arrive every second, and the server finishes 400. The other 100 wait in a line for a free worker.
| Second | Arrive | Finished | Waiting at the end |
|---|---|---|---|
| 1 | 500 | 400 | 100 |
| 2 | 500 | 400 | 200 |
| 3 | 500 | 400 | 300 |
| 4 | 500 | 400 | 400 |
| 5 | 500 | 400 | 500 |
After 5 seconds, 500 requests are waiting. A request that arrives now has all 500 ahead of it. The server works through 400 per second, so it waits 500 ÷ 400 = 1.25 seconds before a worker even starts on it, on top of everything lesson 4 added up.
Press Run it and step through, one second per step. Watch the line under the server grow.
- Queue: 5 seconds of arrivalsRun it and step through, one second per step. Read how many requests are waiting after each second.
Press Run it, then step through and watch the requests move.
The counters end with 2,000 requests done in 5 seconds, the server's limit of 400 per second, and the line at its longest so far, 500. The line grows by 100 every second the burst goes on. It does not settle down: a server that receives more than it can finish falls further behind every second.
What can be done? Making each request hold a worker for less time helps: at 10 ms instead of 20, the same 8 workers would finish 800 per second. But a request can only get so fast, and a busy day can bring more visitors than that. The bigger answer is more servers sharing the requests, which is where the next tutorial starts.
Check yourself
A smaller server has 4 workers, and each request takes a worker 50 ms. How many requests can the server finish per second?
The server from the lesson finishes 400 requests per second. For 4 seconds, 450 arrive every second. How many are waiting at the end of the 4 seconds?
The server gets 16 workers instead of 8. Each request still takes a worker 20 ms. What changes?
- Tell latency (how long one request takes) from throughput (how many requests are finished per second).
- Work out one server's limit: 8 workers at 20 ms each finish 400 requests per second.
- Follow the waiting line when 500 arrive per second: 100 more each second, 500 after 5 seconds, 1.25 s of extra wait.
- Say that a server receiving more than it can finish falls further behind every second, which is why the next tutorial adds servers.