The last lesson ended with two app servers and Siti suddenly logged out. During a ticket war, TicketTune runs three. Users still know only one address. So who picks the server for each request?
The one who picks is the load balancer: the front door that spreads requests over several servers so that no single server is overwhelmed, while steering requests away from servers that are sick. It works like the queue attendant at a station with three ticket windows: they send each person to a window, keep an eye on which window has a long line, and stop sending people to a window whose clerk has disappeared.
TicketTune behind a load balancer
One application server isn't enough for a ticket war, so TicketTune runs three: app-1, app-2, app-3. Users only know one address, tickettune.test, which points at the load balancer lb. lb picks the server for each request.
The example model in this lesson: 24 requests arrive one every 50 ms from 8 users (budi, siti, dewi, maya, rina, tono, joko, bayu). Every fourth request is a checkout (400 ms of work); the rest are "view concert" (100 ms). Each server works on 2 requests at once; the rest wait in a queue.
Round robin
Round robin takes turns: request 1 to app-1, 2 to app-2, 3 to app-3, 4 back to app-1, and so on. It doesn't need to know anything about the servers, so it's simple and fast. When all three servers are equally healthy, the result is neat: each server gets 8 requests, and the slowest request takes 400 ms, which is a checkout.
A slow server: least connections
Now app-2's disk acts up, and all its work takes 4 times longer. Round robin doesn't care. app-2 still gets 8 requests, its queue piles up, and maya's checkout that arrived at 950 ms only finishes at 3,400 ms.
Least connections picks the server with the fewest active connections at that moment, meaning requests sent but not yet answered. A slow server holds its connections longer, so it automatically gets fewer.
| app-2 slow ×4 | Requests to app-1 / app-2 / app-3 | Average | Slowest |
|---|---|---|---|
| Round robin | 8 / 8 / 8 | 473 ms | 2,450 ms |
| Least connections | 8 / 5 / 11 | 238 ms | 400 ms |
So one slow server pushes the slowest request under round robin from 400 ms to 2,450 ms. Least connections fits when request durations vary or a server may slow down, for example checkouts, uploads or WebSockets.
Health checks: dropping a sick server
Worse than slow: the process on app-3 hangs. It still accepts connections but never answers. lb waits until the timeout server 1s limit, then answers the user with 504 Gateway Timeout. Round robin without checks keeps sending a third of the requests to app-3: 8 of 24 requests fail.
A health check is lb asking every server regularly, for example GET /health every 200 ms. Here's the HAProxy config:
backend tickettune_app
balance roundrobin
option httpchk GET /health
timeout server 1s
server app-1 10.0.1.11:8000 check inter 200ms fall 2 rise 2
server app-2 10.0.1.12:8000 check inter 200ms fall 2 rise 2
server app-3 10.0.1.13:8000 check inter 200ms fall 2 rise 2
balance roundrobinpicks the algorithm. The alternative:balance leastconn.option httpchk GET /healthmakes the check a real HTTP request, not just "the port is open".check inter 200mschecks every 200 ms.fall 2: after 2 failures in a row, the server is marked DOWN and leaves the pool.rise 2: after 2 successes, it comes back.timeout server 1s: a server that doesn't answer within 1 second counts as failed, and the user gets a 504.
In this model app-3 is marked DOWN at 400 ms, so only 2 requests get a 504, not 8.
Hash: the same user to the same server
Sometimes you want the same user to always reach the same server, for example when sessions still live in a server's memory. That's called a sticky session. The simplest way: IP hash, which is hash(client IP) % number of servers. A hash is a function that turns text into a large number that is always the same for the same text. In the Lab the key is the user ID, because many phone users change IP and a whole office can share one IP.
| User | hash | % 3 (3 servers) |
% 2 (app-3 removed) |
|---|---|---|---|
| budi | 2,387,817,240 | 0 → app-1 | 0 → app-1 |
| siti | 2,885,968,911 | 0 → app-1 | 1 → app-2, moves |
| dewi | 3,025,318,762 | 1 → app-2 | 0 → app-1, moves |
| tono | 2,267,695,823 | 2 → app-3 | 1 → app-2, moves |
The problem: as soon as the number of servers changes, the modulo changes and nearly every user moves: of the Lab's 8 users, 6 change server when app-3 is removed, and their sessions are lost. Consistent hashing places servers and users on one circle of numbers (each server gets 100 points); each user lands on the next server point clockwise. When app-3 is removed, only the users that were on app-3 move: 2 of 8. In HAProxy: balance hdr(X-User-Id) plus hash-type consistent.
Sticky sessions patch a stateful server, they don't cure it: if that server dies, the session is still lost. Better: stateless servers with sessions in Redis.
Layer 4 and Layer 7
- Layer 4 (transport):
lbonly sees the TCP IP and port. It doesn't read the request, so it's very fast. - Layer 7 (application):
lbreads the HTTP: URL, headers and cookies. It can send/api/*to API servers and/img/*to image servers, but it's a bit heavier because it has to parse HTTP.
🎯 Choosing an algorithm
| Situation | Algorithm |
|---|---|
| Equally strong servers, uniform requests | round robin |
app-1 has 8 CPUs, app-2 has 4 |
weighted round robin (a weight per server) |
| Request durations vary, WebSockets, servers may slow down | least connections |
| Users must stay on the same server | hash (IP or user ID), consistent |
⚠️ When the load balancer becomes the problem
- Only one load balancer. It becomes a single point of failure. Run two (active and standby) or use a managed load balancer from your cloud.
- No health checks. A hung server keeps getting a third of the traffic, and its users get 504s.
- A
/healththat always answers 200. If that endpoint checks nothing, a broken server still looks healthy. Check what is on that server itself: the process answers, its disk and memory are enough. - A health check that also checks the database. The database stalls for a few seconds, every server fails the check at the same time, and
lbtakes the whole pool out: every request gets a 503 error. Report the database's state through a separate endpoint (for example/ready) or through metrics. Some cloud load balancers, for example the AWS Application Load Balancer, fail open: if every server fails the check, traffic still goes to all of them. - Modulo hashing when the number of servers changes. Nearly every user changes server. Use consistent hashing, or better, stateless servers.
TL;DR: A load balancer spreads requests over several servers and steers them away from sick ones. Round robin takes turns and fits uniform servers and requests. Least connections picks the server with the fewest active connections, so a slow server gets fewer: in the TicketTune model the slowest request drops from 2,450 ms to 400 ms. Health checks (GET /health every 200 ms, fall 2) take a hung server out of the pool, so 504s drop from 8 to 2. IP hash sends the same client to the same server (sticky sessions); consistent hashing makes only the removed server's users move. Layer 4 looks at IP and port, Layer 7 reads URL, headers and cookies. The load balancer itself must be redundant.
⚡ One front door
Meaning: A load balancer spreads requests over several servers so that none is overloaded, while steering requests away from sick servers. Users only need one address, tickettune.test.
⚡ Least connections
Guess first: app-2's disk acts up and its work takes 4 times longer. With round robin, how long does the slowest request take?
2,450 ms, while with all servers healthy it was only 400 ms. With least connections it stays at 400 ms.
Meaning: Least connections picks the server with the fewest active connections. A slow server holds its connections longer, so it automatically gets fewer.
⚡ Health checks
Write:
option httpchk GET /health
server app-3 10.0.1.13:8000 check inter 200ms fall 2 rise 2
Meaning: lb checks every 200 ms. After 2 failures in a row the server is marked DOWN and leaves the pool. After 2 successes it comes back. In this model, 504s drop from 8 to 2.
⚡ Modulo hashing moves many users
Meaning: IP hash (hash % number of servers) sends the same user to the same server. As soon as app-3 is removed, 6 of 8 users change server. With consistent hashing only app-3's users move: 2 of 8.