Load-Balancing Algorithms & Health Checks
Definition
Load-balancing algorithm
- In plain English
- A load-balancing algorithm is the rule a load balancer uses to choose the server for the next request.
- Think of it like
- It is like the rule a host uses to pick the next table.
Health check
- In plain English
- A health check is a regular test request a load balancer sends to see whether a server is working.
- Think of it like
- It is like a teacher taking the register before handing out work.
Why it exists
One podcast conversion finishes in two seconds; another takes ten minutes. Equal request counts leave some servers busy while others sit idle.
A broken server also needs to stop receiving work. A routing rule and regular checks solve different parts of this problem.
How it works
Round robin
- In plain English
- Round robin sends requests to servers in turn, one after another.
- Think of it like
- Like dealing cards around a table.
Least connections
- In plain English
- Least connections sends each request to the server currently handling the fewest.
- Think of it like
- Like joining the shortest checkout queue.
Weighted balancing
- In plain English
- Weighted balancing sends more requests to stronger servers.
- Think of it like
- Like giving the fastest chef more orders.
| Rule | How it picks | Good when | Weak when |
|---|---|---|---|
| Round robin | Take turns | Requests are similar | Long requests pile up |
| Weighted round robin | Take turns in proportion to configured weights | Machines have measured capacity differences | Weights become outdated |
| Least connections | Choose fewest active connections | One busy connection represents one long request | Connections carry very different amounts of work |
| Least response time | Prefer the lowest measured response time | Recent times reflect current load | Measurements are noisy |
| Hash-based | Turn a user identifier into a stable server choice | A user should return to the same server | A few busy users overload one server |
- Ask regularly
Request /healthz every five seconds, with a short response deadline.
- Remove a failing copy
After three failures in a row, stop sending new work to it.
- Restore cautiously
After two successful checks, return the server to service.
The load balancer checks Server 2 three times without a reply, removes it, and sends user requests to servers 1 and 3.
- Active checks send dedicated test requests to each server.
- Passive checks observe failures in real user requests.
- Check local ability to serve requests without turning a shared outage into removal of every server.
Examples
Spread long conversions
Converting podcast audio into another format takes two seconds to ten minutes. Three long requests landed on one server under round robin.
In this setup, each conversion holds one connection until it finishes. Least connections therefore steers new requests toward servers with fewer conversions.
- Count active work rather than requests already completed.
- For work continuing after a connection closes, measure unfinished jobs instead.
The insight
Choose a rule whose measurement reflects the work still running.
Weight unequal servers
Two new servers have 32 processing cores each. Two older servers have eight cores each.
Measured capacity supports weights of 4, 4, 1 and 1. In ten turns, each new server receives four requests and each old server receives one.
new-a: weight 4
new-b: weight 4
old-a: weight 1
old-b: weight 1The insight
Weights help unequal machines share work in proportion to capacity.
Remove a locally broken copy
One gate-update server fills its disk and returns HTTP 503 from /healthz. The balancer removes it after three failed checks.
With immediate failures and five-second intervals, detection takes about 10–15 seconds. Other servers need spare capacity to keep the gates working.
The load balancer detects the full-disk server, removes it from rotation, and sends gate traffic to a healthy server.
The insight
Keep checks focused on local readiness and consider shared failures separately.
Trade-offs
Pros
- Match routing to uneven work.
- Remove broken servers automatically.
Cons
- Collect more measurements.
- Debug more complex routing.
- Poor checks remove healthy application copies.
Use when
- Use weights for measured hardware differences.
- Use active-work measurements for long requests.
Avoid when
- Avoid complex rules for short, similar requests.
- Avoid checks that remove all copies during a recoverable shared outage.
Quick check
Takeaways
- Round robin suits even work; connection counts or weights help with uneven work.
- Health checks stop new traffic reaching broken servers.
- A shared dependency failure must not blindly empty the server pool.