Horizontal scaling
easy 25 minHorizontal scaling, or scaling out, means adding machines and sharing the load between them.
See it run
Many ordinary machines behind one address: how the fleet grows, follows the load, loses a member and shares its data.
Operations
Grow the fleet
From one server to six. Each can do 100 requests a second at its limit; the price is an example.
Step 1 of 5
One server
- CPU each
- 70%
capacity
100 req/s
cost (assumed)
$0.05/h
70 requests a second on a server that can do 100. There is no second server, so a failure stops everyone.
Step 1: One server
def servers_needed(peak, per_server=100, use=0.7, spare=1): return ceil(peak / (per_server * use)) + spareservers_needed(420) # 6 + 1 = 7- Downtime
- None
- App must
- Be stateless
- Scale up instead
- 2-10 min restart
Do not plan for 100% use. At 70% a server still has room for its neighbour's share when one fails.
In the real world
The same horizontal scaling doing real jobs. Pick one and step through it.
A food ordering site triples its traffic every day at noon.
Step 1 of 4
11:30
- CPU each
- 70%
capacity
200 req/s
cost (assumed)
$0.10/h
140 requests a second on two servers. 70% each.
Step 1: 11:30
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Scaling out with state in memory
criticalSessions, uploads and counters kept in one server vanish for every request that lands elsewhere.
Wrongsessions = {} # in this processsessions[sid] = userRightredis.set(f"session:{sid}", user, ex=3600)Running with no spare
criticalTwo servers at 80% cannot lose one: the survivor would need 160%. Both fall.
Wrongn = ceil(peak / per_server)Rightn = ceil(peak / (per_server * 0.7)) + 1Scaling the wrong tier
majorIf servers wait on the database, more of them only add connections and locks.
Wrongif slow: add_web_servers(10)Rightif waiting_on == "database": fix_queries_or_add_replicas()else: add_web_servers(2)Killing a server without draining
minorRequests in progress fail on every scale-in and every release.
Wrongterminate(server)Rightbalancer.deregister(server)wait_until_idle(server, at_most=30)terminate(server)
Interview tips
How to explain it
- Scaling out: more machines, one address, shared load.
- It survives failures and grows online, if servers are stateless.
- It moves the limit to the parts they share.
Memory trick: OUT
- O is for Out: add more machines.
- U is for Unlimited, almost: coordination sets the limit.
- T is for Together: they work as one cluster.
Reach for it when
- A single failure must be invisible.
- Traffic moves through the day.
- The largest sensible machine is in sight.
Check first
- Where do sessions, files and jobs live?
- Can the database take the connections?
- Are retries safe?
- Who spares the load balancer?