Types: Nginx Load Balancing


Nginx supports multiple load-balancing algorithms to distribute traffic among backend servers. By default, it uses Round Robin, but you can change it based on your needs.
Default: Round Robin (No special config needed)
How it works?
Requests are sent to each server in order, one by one.
Example:
Request 1 →
server 1 (3000)
Request 2 →
server 2 (3001)
Request 3 →
server 3 (3002)
Request 4 →
server 1 (3000)
… (Repeats)
Example Nginx Config (Default Round Robin)
upstream my_api {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
server 127.0.0.1:3002;
}
server {
listen 80;
location / {
proxy_pass http://my_api;
}
}
Simple and fair distribution.
No extra configuration needed.
Doesn’t consider server load or response time.
Least Connections (Good for uneven load)
How it works?
Requests go to the server with the least active connections. This is useful if some requests take longer than others.
Example:
- If
server 1
is handling 5 requests butserver 2
has 2, Nginx will send the next request toserver 2
.
Nginx Config
upstream my_api {
least_conn;
server 127.0.0.1:3000;
server 127.0.0.1:3001;
server 127.0.0.1:3002;
}
Helps when some requests take longer.
Good for APIs with variable response times.
May cause imbalances if one server is much faster.
IP Hash (Stick same user to same server)
How it works?
Requests from the same IP always go to the same backend server. This is useful for session-based apps (e.g., authentication, shopping carts).
Example:
User A (IP: 192.168.1.1) →
server 1 (3000)
always.User B (IP: 192.168.1.2) →
server 2 (3001)
always.
Nginx Config
upstream my_api {
ip_hash;
server 127.0.0.1:3000;
server 127.0.0.1:3001;
server 127.0.0.1:3002;
}
Keeps user sessions consistent.
Useful for apps with login sessions.
If a server goes down, users lose access (no automatic failover).
Weighted Load Balancing (Prioritize faster servers)
How it works?
Assigns weights to servers. A server with a higher weight gets more requests.
Example:
server 1 (3000)
→ weight 3 (gets 3x more traffic).server 2 (3001)
→ weight 2.server 3 (3002)
→ weight 1 (gets the least traffic).
Nginx Config
upstream my_api {
server 127.0.0.1:3000 weight=3;
server 127.0.0.1:3001 weight=2;
server 127.0.0.1:3002 weight=1;
}
Distributes load based on server capacity.
Great if some servers are more powerful.
Needs manual tuning.
Which One Should You Use?
Load Balancing Strategy | Best For | Notes |
Round Robin (Default) | General use | Simple & fair distribution |
Least Connections | APIs with slow responses | Reduces overload on busy servers |
IP Hash | Session-based apps | Ensures users always hit the same server |
Weighted Load Balancing | Mixed-power servers | Distributes based on server strength |
Subscribe to my newsletter
Read articles from Prince Bansal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
