Basics

What is a load balancer — and why every visitor suddenly has the same IP

Illustration: one door, several identical rooms behind it

Here's a funny one. You put a load balancer in front of your app — and suddenly every user in your logs arrives from the same address. One IP, a thousand people.

And your "max 10 requests per address" protection decides this is one very pushy visitor. So it bans everyone at once.

That's not a bug. That's how a balancer works — and in five minutes you'll know why.

What it actually does

One server handles some number of requests per second. Past that it slows down, then falls over.

The obvious fix is a second server. But your visitor only knows one address: your domain. Someone has to decide which machine gets a given request.

That someone is the load balancer. It sits in front of all your servers. From the outside there's one of it — your domain points at it. On the inside it spreads requests across machines.

So: one door, several identical rooms behind it. The guest sees the door. Who serves them is the balancer's call.

How it picks a server

Not many options, and they're simple.

  • Round-robin. First request to server A, second to B, third back to A. Usually the default.
  • Least connections. The balancer checks which machine has fewest open connections and sends the request there. Better when requests vary in weight.
  • Sticky session. The same visitor always lands on the same machine. More on this below — it's a crutch, not a feature.

And one more thing any decent balancer does: it keeps knocking on each server asking "still alive?". Stops answering — the balancer just stops sending traffic there.

That's the real value. Not speed — the fact that one machine dying no longer means the site is down.

Trap one: your server can't see who arrived

Back to the opening.

The connection to your app is now opened by the balancer, not by a person. So the address your server sees is the balancer's. The MDN docs put it bluntly: the server only sees the final proxy's IP, "which is often of little use."

What breaks because of that:

  • your rate limit by IP counts everyone as one person;
  • your logs show one line repeated a thousand times instead of distinct visitors;
  • geolocation is certain all your users live in one data centre.

The fix is the X-Forwarded-For header: the balancer puts the visitor's real IP address in there, and your app reads the address from the header instead of from the connection.

Careful, though. Anyone who can send a request can forge that header. You may trust it only when your own balancer set it and nobody can reach your servers directly from outside. MDN warns about this specifically: an unverified value is a way around the very limit you were installing.

Trap two: your app must stop remembering things

While there was one server, you could keep things in process memory. Who's logged in, what's in the cart, which file was just uploaded.

With two servers that falls apart immediately. The visitor logs in — lands on server A, which remembers them. The next request goes to server B, which sees them for the first time and asks them to log in again.

The right fix is to move state outside. Sessions into a shared database or cache, files into separate storage. Then servers have nothing to remember and any of them can serve any request.

A sticky session is the wrong fix, but a fast one: the balancer pins the visitor to one machine. Works until that machine dies. Then the visitor loses everything that lived in its memory.

Where you'll meet one

Sooner than you think. If your app is deployed on cloud hosting with autoscaling, the balancer is already there — you just never saw it.

It usually also holds the certificate: encryption ends at the balancer, and inside the network traffic runs unencrypted. That's why HTTPS is configured on the balancer, not separately on every machine.

And choosing between "buy one machine more power" and "add a second machine" is its own conversation — that's vertical vs horizontal scaling.

Does a balancer make my site faster?

By itself, no. It spreads load, so the site stops crawling under a rush. But a single slow request won't speed up: it's slowed by your code or your database, not by distribution.

Do I need one for a side project?

Probably not. While one machine is enough, a second one only adds fuss with state and logs. But writing code as if there might one day be two servers is worth doing from day one — retrofitting costs more.

How is this different from a CDN?

A CDN serves static files — images, styles, scripts — from the node nearest the person. A balancer splits live application requests across your servers. They often work together and don't get in each other's way.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →