02. Scaling / Lesson 1 of 5
Vertical vs Horizontal
Make one machine bigger, or run many machines side by side.
- 8 min
- Beginner
- 9 steps
- CPU load50% (good)
- Max users2,000
- Cost a month$150
- Request
- Failed request
Step 1 of 9: Start with one small server
Read every step as text
Step 1: Start with one small server
Our app runs on one server with 4 CPU cores. Each core handles about 100 requests a second, and an active user sends one request every 5 seconds (0.2 a second).
So the server can serve
4 × 100 / 0.2 = 2,000users at once. With 1,000 users it is half busy and costs $150 a month.Step 2: The app grows and the server fills up
Now 1,900 users are active. That is 380 requests a second against a capacity of 400, so the CPU sits at 95%.
Near full load, requests queue and every page gets slow. One more busy hour and the server fails. We must add capacity. There are two ways.
Do not wait for 100%. Plan to add capacity when you pass about 70% at the peak.
Step 3: Option 1: scale up (vertical)
Scaling up means replacing the server with a bigger one: 16 cores and more memory. Capacity rises to 8,000 users, and the same 1,900 users now use only 24% of the CPU.
It is the simplest fix: no code changes, because it is still one machine. The cost is $600 a month, and the move usually needs a short restart, so there is a little downtime.
For a small app, a bigger machine is often the best first step. Keep it simple while you can.
Step 4: But there is a ceiling
Growth continues to 30,000 users. We keep scaling up until we reach the biggest machine we can rent, 64 cores in our story (real top sizes are larger, but every provider has one). It holds 32,000 users and costs $2,400 a month.
At 30,000 users the CPU is at 94% and there is no bigger machine to move to. Also, the largest sizes usually cost more per core, and one failure takes everything down.
Vertical scaling is fast and simple, but it has a hard upper limit, a rising price and one big machine to lose.
Step 5: Option 2: scale out (horizontal)
Scaling out means adding more machines of the same small kind. 20 servers with 4 cores each give 40,000 users of capacity. The 30,000 users now load them to 75%.
The cost is $3,000 a month, a little more than the one big machine here, but you add or remove $150 at a time and there is no ceiling. Losing one server loses only 1/20 of the capacity. (We draw three of the 20.)
Large web systems scale out: cheap standard machines, added as needed.
Step 6: Who gets the request?
Users know one address, not twenty. Something must receive each request and pick a server. That is a load balancer: it spreads requests so each server gets about 1,500 users, or 300 requests a second, which is 75% of its capacity.
The next lesson, Load Balancing, covers how it chooses. Here it is enough to see that scaling out needs this extra piece, which scaling up did not.
Scaling out adds a new part to the system: the thing that spreads the work.
Step 7: Many servers must share state
Suppose server 1 keeps your login session in its own memory. Your next request is sent to server 2, which has never heard of you, and you are asked to log in again.
With one machine this could not happen. Scaling out forces you to keep state somewhere all servers can reach, such as a shared store or the database. The Stateless Services lesson shows how.
Anything an app server remembers in its own memory or disk is lost to the other servers.
Step 8: Watch for any single point of failure
If one of the 20 app servers dies, the other 19 carry on at 79% CPU. That is the gain of scaling out. But the load balancer is still just one machine: if it dies, every user is cut off, however many servers are healthy.
A single point of failure (SPOF) is any part whose loss stops the whole system. The cure is redundancy: a second load balancer ready to take over, and the same idea for the database.
Count the parts that exist only once. Each one is a risk, and each one needs a spare.
Step 9: Scaling up versus scaling out
Scale up (vertical): a bigger machine. Simple, no code change, but it has a ceiling (32,000 users here), costs more per core at the top, and is one thing to lose.
Scale out (horizontal): more machines. It needs a load balancer, shared state and redundancy for the parts that exist once, but it has no ceiling. Here 20 small servers carry 30,000 users at 75% CPU.