Load balancer
How I spread traffic across app instances: strategies, health checks, and why the app tier has to stay stateless.
One cashier can't serve a crowd. Open more counters.
That is the whole idea. A load balancer is the person at the door who points each customer to a counter that is open and not busy.
How it works
Without a balancer, one server takes every request. Past some point it slows down, and then it falls over. With a balancer, the same traffic is spread across several copies of the app. That is horizontal scaling: add machines to share the load, instead of making one machine bigger.
┌── Server 1
│
Users → LB ───────┼── Server 2
│
└── Server 3
The balancer does more than split traffic. It usually also handles:
- health checks, so it routes away from an instance that stopped answering
- TLS termination
- routing by path or header
- zero-downtime deploys: roll one instance at a time while the others serve
Strategies
- Round robin. Requests rotate across servers in order. It is simple, and it works when requests cost about the same.
- Least connections. Traffic goes to the server with the fewest active connections. I reach for this when some requests take much longer than others.
- IP hash. The client's address picks the server. It gives you session affinity, but sticky sessions make scaling and failover harder.
Health checks
Each instance exposes a cheap endpoint, often GET /health. The balancer polls it.
Server 1 → healthy
Server 2 → healthy
Server 3 → failed (traffic goes to 1 and 2)
Decide what "healthy" means before you ship. A check that only proves the process is alive will keep sending users to an instance whose database connection is gone.
The requirement nobody mentions first
Servers behind a balancer have to be mostly stateless. If one instance keeps a session in memory, the next request lands on another instance and the session is missing. So state moves out: into the database, a shared store such as Redis, or a signed token. That is the real cost of scaling out, and it is usually bigger than the cost of the balancer itself.
Where I used it
I haven't shipped this in a public project yet.
The projects on this site make a different choice at a different layer. IMS is a modular monolith on one Postgres database, chosen so that stock, reservations and documents commit atomically. That is a decision about the data tier. It says nothing about how many copies of the app sit in front of it, and I won't claim a balancer I haven't run.
What I do carry from this note is the habit it teaches: keep the app tier stateless, so adding a counter later is a configuration change and not a rewrite.
Trade-offs
- It is another thing to run. The balancer itself can fail. Put one behind a managed service, or accept it as a single point of failure and write that down.
- State has to go somewhere. Statelessness pushes sessions and counters into a shared store. That store now needs its own care.
- Debugging spreads out. Logs and traces for one user's session can sit on three machines. You need request ids and centralised logs before you need the balancer.
- Uneven requests break round robin. A few slow requests can pile onto one server while the others idle. Least connections helps, but it is not free.
- Do not add one early. For a prototype or a single-instance project, a balancer adds cost and complexity for no benefit yet. I add it when I have more than one instance, or when I want to deploy without downtime.
The rule I use: write down the requirement first. If it is "survive one server dying" or "deploy without a gap", a balancer earns its place. If it is "it feels more serious", it does not.