What is a Load Balancer — Simple Explanation with Examples
A load balancer distributes incoming network traffic across multiple servers to ensure reliability, availability, and optimal resource utilization, preventing any single server from becoming overwhelmed.
In this guide, you’ll learn how load balancers work, the different types and algorithms, and how to configure one for your own applications. Load balancers are essential for any system that needs high availability and scalability.
Why Load Balancers Exist — The Problem They Solve
A single web server has limits:
- Capacity: A typical server handles 10,000–50,000 concurrent connections before slowing down.
- Reliability: If the server crashes, the entire application goes down.
- Maintenance: Updating software requires downtime — no server to serve requests.
Adding more servers solves these problems, but creates a new one: which server should handle each request? A load balancer sits in front of multiple servers and decides where each request goes.
The Single Point of Failure Problem
Without a load balancer:
User ─────► Web Server (single) ─────► Database
└── If this fails, the entire app is down ──With a load balancer:
┌────► Web Server 1 ─────┐
User ─────► Load Balancer ────► Web Server 2 ────► Database
└────► Web Server 3 ─────┘
(If one fails, traffic goes to the others)The Analogy — Airline Check-In Counters
Imagine an airport with a single check-in counter. All passengers queue at that one counter. If the counter closes (server crash), no one checks in. If a rush of passengers arrives, the queue grows endlessly.
Now imagine 20 check-in counters with a queue manager (load balancer) who directs each passenger to the shortest queue. If one counter closes, the manager sends passengers to the other 19. If all counters are busy, passengers wait, but the system keeps working.
The load balancer is that queue manager — it ensures no counter is overloaded and every passenger gets served.
Layer 4 vs Layer 7 Load Balancing
| Feature | Layer 4 (Transport) | Layer 7 (Application) |
|---|---|---|
| OSI Layer | Transport (TCP/UDP) | Application (HTTP/HTTPS) |
| Routing decision | IP + port | URL, headers, cookies |
| Performance | Very fast (less processing) | Slightly slower (parses packets) |
| Features | Basic distribution | Content-based routing, SSL termination |
| Example | AWS NLB, HAProxy (TCP mode) | AWS ALB, NGINX, HAProxy (HTTP mode) |
Layer 4 Example
Directs all TCP traffic on port 80 to a pool of servers — no inspection of HTTP content.
Layer 7 Example
Routes based on URL path:
/api/* → API servers (backend pool)
/images/* → Image servers (optimized pool)
/* → Web servers (general pool)Load Balancing Algorithms
| Algorithm | How It Works | Best For |
|---|---|---|
| Round Robin | Requests go to each server in rotation | Equal-capacity servers, similar workloads |
| Least Connections | Sends to server with fewest active connections | Variable-length requests (e.g., API calls) |
| IP Hash | Hash of client IP determines server | Session persistence (sticky sessions) |
| Weighted Round Robin | Servers with higher weight get more traffic | Heterogeneous server capacity |
| Random | Randomly selects a server | Simple distribution, testing |
Health Checks
Load balancers periodically check if servers are healthy:
# NGINX — health check upstream servers
upstream backend {
server 10.0.0.1:3000 max_fails=3 fail_timeout=30s;
server 10.0.0.2:3000 max_fails=3 fail_timeout=30s;
server 10.0.0.3:3000 backup; # Only used if others fail
}
location / {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Host $host;
}If a server fails 3 health checks in 30 seconds, it’s removed from the pool until it recovers.
Example NGINX Load Balancer Config
http {
upstream app_servers {
least_conn; # Least connections algorithm
server 10.0.0.1:3000 weight=3;
server 10.0.0.2:3000 weight=2;
server 10.0.0.3:3000;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://app_servers;
proxy_set_header X-Real-IP $remote_addr;
}
}
}Expected behavior: Requests arrive at port 80, NGINX distributes them using least-connections algorithm, and servers with higher weight get proportionally more traffic.
SSL Termination
Load balancers can handle TLS decryption, offloading the CPU-intensive work from backend servers:
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/ssl/certs/example.com.pem;
ssl_certificate_key /etc/ssl/private/example.com.key;
location / {
proxy_pass http://app_servers; # Backend receives plain HTTP
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}Backend servers communicate over HTTP internally, while the load balancer handles HTTPS externally. This reduces CPU load on app servers.
Cloud Load Balancers
| AWS | GCP | Azure | Description |
|---|---|---|---|
| ALB | HTTP(S) LB | App Gateway | Layer 7 — HTTP/HTTPS routing |
| NLB | TCP/UDP LB | Load Balancer | Layer 4 — TCP/UDP, high throughput |
| CLB | — | — | Legacy (Classic) load balancer |
| GWLB | — | — | Layer 3/4 for third-party appliances |
AWS ALB Example
# Create a target group
aws elbv2 create-target-group \
--name my-targets \
--protocol HTTP \
--port 80 \
--vpc-id vpc-12345678
# Register targets
aws elbv2 register-targets \
--target-group-arn arn:aws:elasticloadbalancing:... \
--targets Id=i-0abcdef1234567890 Id=i-0abcdef1234567891
# Create ALB
aws elbv2 create-load-balancer \
--name my-alb \
--subnets subnet-12345678 subnet-87654321 \
--security-groups sg-12345678Common Use Cases
1. Web application scaling
Distribute HTTP traffic across multiple web servers. Add or remove servers based on traffic without downtime.
2. Database read replicas
Route read queries to multiple read replicas while sending writes to the primary database.
3. Microservices routing
Layer 7 load balancers route API requests to the appropriate microservice based on URL path or headers.
4. Global load balancing
Route users to the nearest data center using geographic DNS routing plus local load balancers.
5. Blue-green deployment
Route traffic to the “blue” environment while the “green” environment is tested. Switch instantly when ready.
FAQ
Related Terms
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro