0
0
Nginxdevops~10 mins

Weighted round-robin in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Weighted round-robin
Start
List servers with weights
Select server based on weight
Send request to selected server
Next request -> Repeat selection
Cycle continues
Requests are distributed to servers in proportion to their assigned weights, cycling through servers repeatedly.
Execution Sample
Nginx
upstream backend {
    server backend1.example.com weight=3;
    server backend2.example.com weight=1;
}

server {
    location / {
        proxy_pass http://backend;
    }
}
This config sends 3 requests to backend1 for every 1 request to backend2 in a repeating cycle.
Process Table
Request NumberSelected ServerReason
1backend1.example.comWeight 3 means backend1 chosen first 3 times
2backend1.example.comSecond request to backend1 due to weight
3backend1.example.comThird request to backend1 due to weight
4backend2.example.comWeight 1 means backend2 chosen once after backend1's 3
5backend1.example.comCycle repeats, backend1 chosen again
6backend1.example.comSecond request in new cycle to backend1
7backend1.example.comThird request in new cycle to backend1
8backend2.example.combackend2 chosen once again
9backend1.example.comCycle repeats again
💡 The cycle repeats indefinitely, distributing requests according to weights.
Status Tracker
VariableStartAfter Request 1After Request 2After Request 3After Request 4After Request 5After Request 6After Request 7After Request 8After Request 9
Current ServerNonebackend1.example.combackend1.example.combackend1.example.combackend2.example.combackend1.example.combackend1.example.combackend1.example.combackend2.example.combackend1.example.com
Request Count0123456789
Key Moments - 3 Insights
Why does backend1 get 3 requests before backend2 gets 1?
Because backend1 has weight=3 and backend2 has weight=1, nginx sends 3 requests to backend1 for every 1 to backend2, as shown in execution_table rows 1-4.
Does the order of servers in the config affect the weighted distribution?
No, nginx uses weights to decide distribution regardless of order. The cycle repeats based on weights, not position, as seen in the repeating pattern in execution_table.
What happens after all weighted requests are sent?
The cycle restarts from the first server again, continuing the weighted round-robin indefinitely, as shown by requests 5-9 in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which server handles the 4th request?
Abackend1.example.com
Bbackend3.example.com
Cbackend2.example.com
DNo server assigned
💡 Hint
Check the 'Selected Server' column for Request Number 4 in execution_table.
At which request number does the weighted cycle repeat?
ARequest 4
BRequest 5
CRequest 3
DRequest 6
💡 Hint
Look at execution_table rows after Request 4 to see when backend1 is selected again.
If backend2 weight changes to 2, how does the selection change for first 5 requests?
Abackend1 chosen 3 times, backend2 twice
Bbackend1 chosen twice, backend2 twice
Cbackend1 chosen 3 times, backend2 once
Dbackend1 chosen once, backend2 4 times
💡 Hint
Weights determine how many times each server is selected per cycle; increasing backend2 weight to 2 means 2 requests go to backend2 per cycle.
Concept Snapshot
Weighted round-robin in nginx:
- Assign weights to servers in upstream block.
- Requests distributed proportionally to weights.
- Cycle repeats indefinitely.
- Syntax: server name weight=number;
- Higher weight = more requests.
- Useful for load balancing uneven server capacities.
Full Transcript
Weighted round-robin in nginx means sending requests to servers based on their assigned weights. For example, if one server has weight 3 and another weight 1, the first server gets 3 requests for every 1 request the second gets. This cycle repeats forever. The configuration uses the upstream block with server lines specifying weights. The execution table shows which server handles each request in order, cycling through according to weights. Variables track the current server and request count. Key points include understanding why servers get requests in proportion to weights, that order in config does not affect distribution, and that the cycle repeats indefinitely. Quizzes test understanding of which server handles which request and how changing weights affects distribution.