When one server is not enough
After this: Choose between a bigger machine and more machines, and work out how many servers a load needs, with one spare.
On this page · 4 sections · 3 checks
A small shop runs its website on one . On a normal day it is fine. Then the shop announces a sale, and the site gets slow, then very slow. What can the shop do?
Why one server falls behind
In "What happens when you open a website" you met the server that finishes 400 requests per second. When 500 arrive every second, 100 more come in than go out. Those 100 wait in a line, and the line grows every second.
- Queue: 5 seconds of arrivalsRun it and step through the five seconds. Watch the number waiting in line after each second.
Press Run it, then step through and watch the requests move.
After five seconds, 500 requests are waiting. The last one to arrive waits about 1.25 seconds before its turn comes, on top of the normal time. The server is not broken. It is full.
Two ways to grow
There are two ways to give the shop more room:
- : move to one bigger machine, with more processors and more memory.
- : add more machines of the same kind, and share the requests between them.
Scaling up is often tried first, because the code does not have to change. It has two limits. The biggest machine you can buy is only so big, and when that one machine stops, the whole site stops with it.
Scaling out goes much further: when the sale grows, you add another server. In time some part that every server shares, such as the database, becomes the limit instead; later tutorials deal with that. Scaling out needs one new piece. Browsers know one address for the shop, not three. Something has to stand at that address, receive every request, and pass each one on to a server. That piece is a .
Press Run it, then step through and watch the requests move.
Each server behind the load balancer runs the same code. Any of them should be able to answer any request, so the load balancer is free to choose. Lesson 2 shows how it chooses, and lesson 4 shows what "any of them" takes.
How many servers?
The sale brings 1,000 requests per second. Each server handles 400. Work it out one step at a time:
- Divide: 1,000 ÷ 400 = 2.5 servers.
- You cannot run half a server, so round up: 3 servers carry the load.
- Now suppose one of the three dies. The two left handle 2 × 400 = 800 a second, so 200 more arrive than leave each second, and a line builds up, as in the picture above.
- Add one spare: 4 servers. If one dies, the three left handle 3 × 400 = 1,200 a second, which is more than 1,000.
The rule: divide the requests per second by what one server handles, round up, then add one spare. Engineers call this N+1: N servers for the load, plus one.
Check yourself
A shop's website runs on one server, and the server is full. Which change is scaling out?
A sale brings 1,800 requests per second. Each server handles 400 per second. How many servers do you run, with one spare?
Three servers share 1,000 requests per second, at most 400 each. One of them dies. What happens next?
- Say why one server falls behind: when more requests arrive each second than it can finish, a line builds up and keeps growing.
- Tell scaling up (one bigger machine) from scaling out (more machines of the same kind), and say the two limits of scaling up.
- Say what a load balancer does: it receives every request for the site and passes each one to one of the servers.
- Work out how many servers a load needs: divide, round up, then add one spare (N+1). 1,000 a second at 400 each needs 3, and 4 with the spare.