Nginx · The edge of your stack
How does load balancing work in Nginx?
Short answer
Nginx distributes requests across servers in an upstream group using round robin by default, or least_conn, ip_hash, hash or random. Weights, backup servers and passive failure tracking shape the result.
An upstream block defines a pool of backend servers. Nginx selects one per request according to the configured method. Round robin is the default and works well when requests are similar in cost.
upstream app {
least_conn;
server 10.0.0.11:8080 weight=3 max_fails=3 fail_timeout=10s;
server 10.0.0.12:8080 weight=1;
server 10.0.0.13:8080 backup;
keepalive 64;
}
server {
location / {
proxy_pass http://app;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}Methods and controls
- Round robin spreads requests evenly by default
- least_conn sends to the server with the fewest active connections
- ip_hash keeps a client on one server, useful for simple session affinity
- hash key gives consistent routing for a chosen value
- weight adjusts share; backup is used only when primary servers are unavailable
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1When would you choose least_conn over round robin?
- 2What are the limits of ip_hash for session affinity?
- 3How does backup differ from a normal server?
- 4How does keepalive change load balancing behavior?
Say it in the interview
I define an upstream pool and pick a method. Round robin is fine for uniform requests; least_conn is better when request durations vary. I use weights for uneven capacity and backup servers for failover, plus upstream keepalive to avoid a new TCP connection per request. I am careful with ip_hash because it reduces balance and is not a real session store; shared session state is safer.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Nginx mock interview and get scored feedback on depth, clarity and confidence.