What Is a Load Balancer?
Why Use a Load Balancer?
- High Availability: If one server fails, traffic can be redirected to others.
- Better Performance: Requests are shared among multiple servers.
- Scalability: Easy to add/remove servers based on traffic demand.
How It Works
Imagine 3 servers handling web requests. Without a load balancer, all users hit Server-1 → slow and unstable.
With a load balancer:
The load balancer decides which server handles each request.
Example
Imagine 3 web servers:
Server | Current Connections |
---|---|
Server-1 | 5 |
Server-2 | 2 |
Server-3 | 3 |
Least Connections algorithm → next request goes to Server-2.
Health Checks
Load balancers often check server health automatically.
- If a server fails → traffic is redirected to healthy servers.
Load Balancing Algorithms
A load balancer can use different algorithms to decide where each request goes. Here are the most common ones:
1. Round Robin
How it works:
- Requests are sent to servers in order, one after another.
- After the last server, it starts again from the first.
Example:
3 servers: S1, S2, S3 Incoming requests: R1, R2, R3, R4, R5
Request | Server Assigned |
---|---|
R1 | S1 |
R2 | S2 |
R3 | S3 |
R4 | S1 |
R5 | S2 |
✅ Simple and fair for servers with similar capacity.
2. Least Connections
How it works:
- The server with the fewest active connections gets the next request.
- Good for servers where request processing time varies.
Example:
Server | Active Connections | Next Request Goes To |
---|---|---|
S1 | 5 | |
S2 | 2 | R1 → S2 |
S3 | 3 |
✅ Balances load dynamically based on current server usage.
3. IP Hash
How it works:
- Uses the client’s IP address to decide which server handles the request.
- Same client always goes to the same server (sticky session).
Example:
Client IP → Hash → Server
Client IP | Server Assigned | |
---|---|---|
192.168.1.10 | S2 | |
192.168.1.11 | S3 | |
192.168.1.10 | S2 | (same as before) |
✅ Useful for session persistence, where users need to stay connected to the same server.
Summary
Algorithm | How it Works | When to Use |
---|---|---|
Round Robin | Requests in order | Servers have similar capacity |
Least Connections | Fewest active connections | Uneven request load / heavy tasks |
IP Hash | Client IP decides server | Session persistence / sticky sessions |
Key Benefits of Load Balancers:
- Distributes traffic to multiple servers.
- Prevents single-server overload.
- Improves performance, availability, and reliability.
- Performs health checks to avoid sending requests to failed servers.