Weighted round-robin in Nginx - Time & Space Complexity
We want to understand how the time needed to pick a server grows as the number of servers increases in weighted round-robin load balancing.
How does the selection process scale when more servers with different weights are added?
Analyze the time complexity of the following nginx weighted round-robin snippet.
upstream backend {
server backend1.example.com weight=3;
server backend2.example.com weight=1;
server backend3.example.com weight=2;
}
server {
location / {
proxy_pass http://backend;
}
}
This configuration distributes requests to servers based on their weights, cycling through them proportionally.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Iterating through the list of servers to select the next one based on weights.
- How many times: Once per request, cycling through all servers in weighted order.
As the number of servers increases, the selection process checks more servers to find the next one according to weights.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 | 3 checks per cycle |
| 10 | 10 checks per cycle |
| 100 | 100 checks per cycle |
Pattern observation: The number of operations grows linearly with the number of servers.
Time Complexity: O(n)
This means the time to select a server grows directly in proportion to the number of servers configured.
[X] Wrong: "Selecting a server in weighted round-robin is always constant time regardless of server count."
[OK] Correct: The selection process must check servers in order to respect weights, so more servers mean more checks.
Understanding how load balancing scales helps you design systems that handle more traffic smoothly. This skill shows you can think about efficiency in real setups.
"What if nginx used a different algorithm that stored precomputed weighted lists? How would the time complexity change?"
Practice
weight parameter control in nginx's weighted round-robin load balancing?Solution
Step 1: Understand the role of weight in load balancing
The weight defines how many requests a server receives compared to others.Step 2: Relate weight to request distribution
Higher weight means more requests go to that server, balancing load by capacity.Final Answer:
The number of requests sent to a server relative to others -> Option DQuick Check:
Weight controls request share [OK]
- Confusing weight with timeout
- Thinking weight limits connections
- Assuming weight sets server order
Solution
Step 1: Recall nginx upstream server syntax
Weights are assigned usingweight=numberwith an equal sign and semicolon.Step 2: Check each option's syntax
Only server backend1.example.com weight=3; uses the correctweight=3;format.Final Answer:
server backend1.example.com weight=3; -> Option CQuick Check:
Correct syntax uses equal sign and semicolon [OK]
- Omitting equal sign
- Using colon instead of equal sign
- Using parentheses incorrectly
upstream backend {
server backend1.example.com weight=3;
server backend2.example.com weight=1;
}
If 8 requests arrive, how many requests will backend2 receive approximately?Solution
Step 1: Calculate total weight
backend1 weight=3, backend2 weight=1, total = 4.Step 2: Calculate requests for backend2
Requests for backend2 = (1/4) * 8 = 2.Final Answer:
2 -> Option AQuick Check:
Requests proportional to weight [OK]
- Ignoring total weight sum
- Assigning equal requests despite weights
- Miscounting total requests
Solution
Step 1: Understand weighted round-robin requires correct weight syntax
If weights are missing or incorrect, nginx defaults to equal distribution.Step 2: Analyze the options
Weights were not specified correctly in the config states weights not specified correctly, which explains equal requests.Final Answer:
Weights were not specified correctly in the config -> Option BQuick Check:
Incorrect weights cause equal load [OK]
- Assuming nginx lacks weighted support
- Confusing IP issues with load balancing
- Missing weight directive entirely
Solution
Step 1: Calculate current total weight
Sum of existing weights = 5 + 3 + 2 = 10.Step 2: Determine weight for new server
To keep total sum at 10 when adding a fourth server, assign weight=0 to it.Step 3: Verify proportions
Weight=0 means no requests to the new server, so first three retain exact proportions (5:3:2 out of total 10).Final Answer:
0 (do not add the server) -> Option AQuick Check:
Weight=0 keeps total and proportions unchanged [OK]
- Assigning positive weight that increases total sum
- Forgetting to maintain exact total of 10
- Ignoring impact on relative proportions
Ready to prove it?
Pass the test and this pattern moves unknown
