
When a website becomes popular, a single server can't serve all requests; it slows or crashes. The solution is usually to run several servers and spread incoming traffic among them. The component that does this is the load balancer. This guide covers how it works, the basic algorithms and its contribution to high availability.
What is a load balancer?
A load balancer is the component that sits between users and servers and routes requests to a suitable server. The user connects to one address and doesn't know how many servers are behind it. For scaling logic see our cloud computing and CDN guides.
- 1
The user connects to the site address
- 2
DNS points to the load balancer's address
- 3
The load balancer identifies healthy servers
- 4
It picks a server according to the algorithm
- 5
The request goes to that server and the reply returns to the user
Why is it needed?
- Performance: load is spread across servers.
- High availability: if one server crashes traffic goes to the others.
- Scalability: adding and removing servers gets easier.
- Easier maintenance: you can update a server without downtime.
- A security layer: SSL termination and filtering can be done (see our SSL/HTTPS guide).
Load-distribution algorithms
| Algorithm | How it works |
|---|---|
| Round robin | Distributes requests to servers in turn |
| Weighted round robin | Gives more requests to stronger servers |
| Least connections | Sends to the one with the fewest active connections |
| IP hash | Always routes the same client to the same server |
| Least response time | Picks the server answering fastest |
L4 and L7 load balancing
| Type | Trait |
|---|---|
| L4 (transport layer) | Routes by IP and port; fast, doesn't look at content |
| L7 (application layer) | Smart routing by URL, headers and cookies |
L7 can, for example, send "/api" requests to one server group and images to another.
Health checks
The load balancer checks servers regularly (for example by requesting an address). A server that doesn't answer is removed from the pool and re-added once it recovers. This way users aren't routed to a faulty server. For monitoring see our uptime monitoring guide.
Session stickiness
In some apps the user's session is kept on one server; then the same user must always be sent to the same server. A better solution is to keep session data in a shared store and make the app stateless; this makes scaling easier.
High availability and redundancy
A single load balancer becomes a single point of failure if it fails itself. Managed load balancers in the cloud usually run redundantly across several zones. For disasters see our disaster recovery plan guide.
Load balancers in the cloud
Large cloud providers offer managed load balancing services, reducing setup and maintenance load (see our cloud providers guide). On your own server open source software (e.g. Nginx) can be used (see our VPS guide). In container environments Kubernetes also distributes traffic (see our Kubernetes guide).
Common mistakes
- Leaving the load balancer as a single point of failure.
- Not configuring health checks.
- Keeping session data on a local server and making scaling hard.
- Not planning capacity.
- Not managing the SSL certificate.
- Not setting up monitoring and alerts.
Frequently asked questions
Is it necessary for a small site? Generally no; a single server may be enough.
How does it differ from a CDN? A CDN distributes static content close to users; a load balancer distributes dynamic requests among servers.
Which algorithm is best? It depends on the workload; in many cases least connections is a good choice.
Summary
A load balancer improves performance and availability by distributing traffic across several servers. Plan the algorithm, health checks and redundancy properly.
Serkan Akyürek is the founder and author of toolrota; he writes step-by-step guides on Excel, AI, the cloud, personal finance and digital security.