How does Nginx work as a load balancer and what algorithms does it support?
How Nginx works as a load balancer using upstream blocks, plus round robin, least_conn, ip_hash and weighted algorithms with config examples and interview tips.
Expected Interview Answer
Nginx works as a load balancer by defining an upstream group of backend servers and forwarding incoming requests to them with proxy_pass, distributing traffic according to a chosen algorithm to spread load and improve availability.
Nginx supports several balancing methods: round robin (the default, cycling through servers in order), weighted round robin (more traffic to higher-weight servers), least_conn (send to the server with the fewest active connections), ip_hash (map a client IP to a consistent server for session stickiness), and hash (based on a custom key). It also performs passive health checks, marking failed servers as unavailable, and supports keepalive connections and backup servers for resilience.
- Distributes traffic to prevent any one server overloading
- Improves availability with automatic failover
- Supports session stickiness via ip_hash
- Weights let heterogeneous servers share load fairly
- Scales horizontally by adding upstream servers
AI Mentor Explanation
A captain rotating bowlers across an innings is like Nginx load balancing: round robin is bowling each fit bowler in turn, weighting is giving your strike bowler more overs, and least-connections is handing the next over to whoever is freshest rather than the one who just finished a long spell. ip_hash is like always giving a particular batter's dismissal to the same specialist bowler, keeping that matchup consistent throughout the match.
Step-by-Step Explanation
Step 1
Define an upstream group
List backend servers inside an upstream {} block with optional weights.
Step 2
Choose an algorithm
Default is round robin; add least_conn, ip_hash, or hash directives as needed.
Step 3
Point traffic to the group
Use proxy_pass http://upstream_name; inside a location block.
Step 4
Configure health handling
Set max_fails and fail_timeout so failing servers are temporarily removed.
Step 5
Add resilience
Mark backup servers, tune keepalive connections, and weight heterogeneous hardware.
What Interviewer Expects
- Knowledge of the upstream block and proxy_pass
- Naming round robin, least_conn, ip_hash and weighting
- Understanding when session stickiness is needed
- Awareness of passive health checks (max_fails/fail_timeout)
- Mention of weights for uneven server capacity
Common Mistakes
- Forgetting round robin is the default and needs no directive
- Using ip_hash when true shared session storage is the right fix
- Confusing least_conn with least response time
- Not configuring health checks so dead servers keep getting traffic
- Assuming Nginx open source does active health checks by default
Best Answer (HR Friendly)
“Nginx spreads incoming traffic across several backend servers so no single one gets overwhelmed. It can rotate requests evenly, send more to stronger servers, pick the least-busy server, or keep a user on the same server, and it automatically stops sending traffic to servers that stop responding.”
Code Example
upstream app_servers {
least_conn;
server 10.0.0.11:8080 weight=3 max_fails=3 fail_timeout=30s;
server 10.0.0.12:8080 weight=1;
server 10.0.0.13:8080 backup;
}
server {
listen 80;
location / {
proxy_pass http://app_servers;
proxy_set_header Host $host;
}
}Follow-up Questions
- When would you use ip_hash instead of round robin?
- How does Nginx detect and handle a failed backend?
- What is the difference between passive and active health checks?
- How do weights change round robin distribution?
- Why can least_conn outperform round robin for long-lived requests?
MCQ Practice
1. What is the default Nginx load balancing method?
Round robin is the default, cycling through upstream servers in order without any extra directive.
2. Which method provides basic session stickiness?
ip_hash maps each client IP to a consistent upstream server, keeping a user's session on the same backend.
3. Which directive sends requests to the server with the fewest active connections?
least_conn routes each new request to whichever upstream server currently has the fewest active connections.
Flash Cards
Default balancing method — Round robin — cycles through upstream servers in order.
least_conn — Routes each request to the server with the fewest active connections.
ip_hash — Maps a client IP to a consistent backend for session stickiness.
Passive health check — max_fails and fail_timeout temporarily remove failing servers from rotation.