Why load balancing distributes traffic in Nginx - Performance Analysis
We want to understand how the work done by nginx changes when it distributes traffic across servers.
Specifically, how does the number of requests affect the processing time in load balancing?
Analyze the time complexity of the following nginx load balancing configuration.
upstream backend {
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
This code sets up nginx to distribute incoming requests evenly across three backend servers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Routing each incoming request to one of the backend servers.
- How many times: Once per request, repeated for every request nginx receives.
As the number of requests increases, nginx processes each request by selecting a backend server.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 routing decisions |
| 100 | 100 routing decisions |
| 1000 | 1000 routing decisions |
Pattern observation: The number of routing operations grows directly with the number of requests.
Time Complexity: O(n)
This means the work nginx does grows in a straight line with the number of requests it handles.
[X] Wrong: "Load balancing makes nginx handle requests faster regardless of how many requests come in."
[OK] Correct: Load balancing spreads requests but nginx still processes each request once, so total work grows with requests.
Understanding how nginx handles many requests helps you explain real-world server behavior clearly and confidently.
"What if we added more backend servers? How would the time complexity of request handling change?"
Practice
Solution
Step 1: Understand the purpose of load balancing
Load balancing spreads incoming requests across multiple servers to avoid overloading one server.Step 2: Recognize the benefit of spreading traffic
By distributing traffic, servers handle requests faster and the service stays stable.Final Answer:
To prevent any single server from becoming overloaded and improve response time -> Option BQuick Check:
Load balancing improves speed and stability [OK]
- Thinking load balancing increases server storage
- Believing it reduces the number of servers needed
- Confusing load balancing with website design
Solution
Step 1: Identify Nginx load balancing syntax
Nginx uses theupstreamblock to define a group of servers for load balancing.Step 2: Check the correct block and server entries
The correct syntax isupstream backend { server IP; server IP; }to list servers.Final Answer:
upstream backend { server 192.168.1.10; server 192.168.1.11; } -> Option CQuick Check:
Useupstreamfor server groups [OK]
- Using 'loadbalance' instead of 'upstream'
- Writing 'server_group' which is invalid
- Misusing 'proxy_pass' inside server group
upstream backend {
server 192.168.1.10;
server 192.168.1.11;
}
server {
location / {
proxy_pass http://backend;
}
}Solution
Step 1: Understand the upstream block
Theupstream backenddefines two servers to share traffic.Step 2: Analyze proxy_pass usage
Theproxy_pass http://backend;sends requests to the upstream group, distributing load.Final Answer:
The request is distributed between 192.168.1.10 and 192.168.1.11 -> Option AQuick Check:
Requests split across servers [OK]
- Thinking requests always go to first server
- Assuming proxy_pass blocks requests without extra config
- Believing requests go to only one fixed server
Solution
Step 1: Check upstream and proxy_pass names
Ifproxy_passpoints to a different name than the upstream block, load balancing won't work.Step 2: Understand traffic routing
Requests will not be distributed if proxy_pass does not reference the correct upstream group.Final Answer:
The proxy_pass URL does not match the upstream name -> Option DQuick Check:
proxy_pass must match upstream name [OK]
- Ignoring mismatch between proxy_pass and upstream name
- Assuming offline servers cause all traffic to one server
- Confusing load balancing methods with traffic distribution issues
Solution
Step 1: Understand load balancing methods
round_robindistributes evenly but does not keep user sessions on one server.Step 2: Identify method that keeps user on same server
ip_hashuses client IP to send requests to the same server, maintaining session consistency.Final Answer:
ip_hash -> Option AQuick Check:
ip_hash keeps user on same server [OK]
- Choosing round_robin for session persistence
- Confusing least_conn with user stickiness
- Thinking random method keeps sessions consistent
Ready to prove it?
Pass the test and this pattern moves unknown
