Sub topic 2
What a load balancer does
This chapter assumes you know how HTTP requests and responses work, understand TCP connections, IP addresses and ports, and can edit a configuration file. It describes the load balancer's role and the basic request flow. Later chapters cover algorithms, health checks and the other topics in detail.
The core job
A load balancer is a service that accepts client connections on one address and forwards each one to a server from a pool of backends. Clients connect to a single, stable address, often called a virtual IP (VIP) or a DNS name. They never need to know how many backends exist or where they run.
This indirection gives you three things:
- Capacity. Work is spread over many servers, so you add servers instead of buying a bigger one.
- Availability. If a backend fails, the load balancer sends traffic to the others.
- Freedom to change. You can add, remove or replace backends without clients noticing.
The request path
The load balancer sits between clients and backends. It makes a decision for each connection or request, and the backend's reply returns through it.
For each new connection or request, the load balancer does the following:
- Accepts the connection from the client.
- Chooses a backend from the pool, using a balancing algorithm and the known health of each server.
- Opens or reuses a connection to that backend and forwards the traffic.
- Returns the backend's response to the client.
What it is responsible for
| Responsibility | What it means |
|---|---|
| Distribution | Picks a backend for each connection or request |
| Health awareness | Avoids backends that are down or failing |
| Abstraction | Hides backend addresses and pool size from clients |
| Optional extras | TLS termination, header changes, logging, rate limiting |
Which extras are available depends on whether the balancer works at Layer 4 or Layer 7, which the next chapter explains.
A minimal working example
This NGINX configuration balances traffic across two backends. With no algorithm named, NGINX uses round robin, so requests alternate between the servers.
upstream app {
server 10.0.0.11:8080;
server 10.0.0.12:8080;
}
server {
listen 80;
location / {
proxy_pass http://app;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}The upstream block defines the pool. The proxy_pass line sends requests to that pool instead of to a single address. The two proxy_set_header lines are needed because the backend sees the load balancer's IP as the client. Host preserves the requested hostname, and X-Forwarded-For carries the original client address.
What it does not do for you
A load balancer spreads traffic, but it does not make the application scalable by itself. Several problems remain your responsibility:
- Shared state. If each backend keeps user sessions in its own memory, a user sent to a different server loses the session. Store state in a shared place, or use session persistence (a later chapter).
- Backend capacity. If every backend is overloaded, adding a balancer changes nothing. You need more backends.
- The balancer itself. One load balancer is a single point of failure. A later chapter covers making that tier redundant.
Common mistakes
- Pointing DNS at individual backends and calling it load balancing. DNS answers get cached, so clients keep using a dead server.
- Forgetting forwarded headers, so the application logs and rate limits treat all clients as one address.
- Opening backend ports to the internet. Backends should accept traffic only from the load balancer.
Exercise
A team puts two web servers behind a load balancer. Users are randomly logged out. Each server stores login sessions in its own memory. What is the cause, and name two ways to fix it?
Show answer
Consecutive requests from one user reach different servers, and the second server has no record of the session. Fixes: store sessions in a shared store such as Redis or a database, or configure session persistence so the user always reaches the same backend.