Picture a grocery store with one checkout lane. On a quiet morning it works fine. On a Saturday afternoon the line stretches to the back of the store, and if that cashier steps away, nobody can pay at all. Open more lanes and put someone at the front directing shoppers to the shortest line, and the problem disappears.
That person at the front is a load balancer. In this guide we will cover what load balancing is, how load balancers decide where to send traffic, and when a website or app actually needs one.
What is load balancing?
Load balancing is the practice of distributing incoming requests across a group of servers, often called a pool or backend. Instead of visitors connecting to one web server directly, they connect to the load balancer, which forwards each request to one of several servers behind it.
This gives you two big benefits:
- Capacity. More servers can handle more visitors than one. When traffic grows, you add servers to the pool.
- Availability. If one server fails or is taken down for maintenance, the load balancer stops sending it traffic and the site stays online.
Where load balancers sit
A typical setup looks like this:
- A visitor's browser looks up your domain and connects to the load balancer's IP address.
- The load balancer picks a healthy backend server.
- The backend generates the page and sends it back through the load balancer to the visitor.
Load balancers come in a few forms:
- Software you run on your own server, such as Nginx, HAProxy or Traefik
- Managed load balancers offered by cloud and hosting providers
- Hardware appliances used in large data centers
- DNS-based and global load balancing, which directs visitors to different data centers or regions
Layer 4 vs Layer 7
You will often see these two terms, which refer to layers of the networking model.
- Layer 4 (transport) load balancers route traffic based on IP address and port. They are fast and work with any TCP or UDP protocol, but they do not look inside the request.
- Layer 7 (application) load balancers understand HTTP. They can route based on the URL, host name, headers or cookies. For example, they can send
/api/requests to one group of servers and everything else to another.
Most website setups use Layer 7 load balancing because of that flexibility.
Common load balancing algorithms
The algorithm decides which server gets the next request.
Round robin
Requests go to each server in turn: one, two, three, one, two, three. Simple and effective when servers are similar and requests are roughly equal.
Weighted round robin
Servers get a share of traffic proportional to a weight you set. A server with twice the CPU might get a weight of 2.
Least connections
Each new request goes to the server with the fewest active connections. Useful when some requests take much longer than others.
IP hash
The visitor's IP address determines which server they reach, so the same visitor usually lands on the same server. This is one way to keep sessions "sticky."
Health checks
A load balancer is only useful if it knows which servers are working. Health checks solve this. The load balancer regularly requests a URL on each server, such as /health, or simply tries to open a connection. If a server stops responding correctly, it is removed from the pool until it recovers.
Good health checks test something meaningful. A page that confirms the app can reach its database is a better signal than a static file that loads even when the app is broken.
The things that trip people up
Sessions and logins
If a user logs in on server A and their next request goes to server B, server B may not know who they are. Solutions include:
- Sticky sessions, where the load balancer keeps each visitor on the same server
- Shared session storage, such as Redis or the database, so every server can read every session (generally the more robust option)
Uploaded files
If a visitor uploads an image to server A, server B does not have it. Store uploads on shared storage, object storage or a CDN instead of each server's local disk.
Databases
Adding web servers is easy. The database is often the shared component everything depends on. It may need its own replication or a managed database service to avoid becoming the new single point of failure.
SSL certificates
The load balancer usually handles HTTPS, a setup called SSL termination. It decrypts traffic, then forwards it to the backends. This keeps certificate management in one place. Make sure your application knows the original request was HTTPS, usually through the X-Forwarded-Proto header, or you may see redirect loops.
Visitor IP addresses
Backends see the load balancer's IP instead of the visitor's. Configure your web server and app to read the real IP from the X-Forwarded-For header, or logs and security tools will be misleading.
The load balancer itself
A single load balancer can become a single point of failure. Production setups often run two in an active-passive pair with a floating IP, or use a managed load balancer that the provider keeps redundant.
A simple example with HAProxy
HAProxy is a popular open-source load balancer. A minimal configuration that spreads HTTP traffic across two app servers, with active health checks, looks like this:
frontend web
bind *:80
default_backend app_servers
backend app_servers
balance leastconn
option httpchk GET /health
server app1 10.0.0.11:80 check
server app2 10.0.0.12:80 check
The balance leastconn line chooses the least connections algorithm, option httpchk requests /health on each server, and check turns on health checking for each one. Nginx can do the same job with a short block of configuration and is a common choice if you already use it as a web server or reverse proxy.
Do you need a load balancer?
Probably not yet if:
- You run a small business site, blog or portfolio
- One well-sized server handles your traffic comfortably
- Brief downtime during maintenance is acceptable
Often, caching and a CDN will handle traffic growth far more cheaply than adding servers. Our guide to what a CDN is explains how that works.
It is worth considering when:
- Your traffic regularly pushes one server to its limits
- Downtime is costly, such as for a busy store or a SaaS product
- You want to update servers one at a time without taking the site down
- You are already running multiple application servers
Wrap-up
A load balancer spreads requests across several servers, removes failed servers automatically and lets you grow by adding capacity instead of replacing it. Pick an algorithm that fits your traffic, set up meaningful health checks, and plan for sessions, uploads, SSL and the database. For many smaller sites, a single solid server plus caching is enough. When uptime and scale start to matter, load balancing is the next step.