Bird
Raised Fist0
Nginxdevops~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the weight parameter control in nginx's weighted round-robin load balancing?
easy
A. The maximum number of connections a server can handle
B. The timeout duration for server responses
C. The order in which servers are listed in the config
D. The number of requests sent to a server relative to others

Solution

  1. Step 1: Understand the role of weight in load balancing

    The weight defines how many requests a server receives compared to others.
  2. Step 2: Relate weight to request distribution

    Higher weight means more requests go to that server, balancing load by capacity.
  3. Final Answer:

    The number of requests sent to a server relative to others -> Option D
  4. Quick Check:

    Weight controls request share [OK]
Hint: Weight sets request share per server [OK]
Common Mistakes:
  • Confusing weight with timeout
  • Thinking weight limits connections
  • Assuming weight sets server order
2. Which of the following is the correct syntax to assign weights to servers in nginx for weighted round-robin?
easy
A. server backend1.example.com weight:3;
B. server backend1.example.com weight 3;
C. server backend1.example.com weight=3;
D. server backend1.example.com weight(3);

Solution

  1. Step 1: Recall nginx upstream server syntax

    Weights are assigned using weight=number with an equal sign and semicolon.
  2. Step 2: Check each option's syntax

    Only server backend1.example.com weight=3; uses the correct weight=3; format.
  3. Final Answer:

    server backend1.example.com weight=3; -> Option C
  4. Quick Check:

    Correct syntax uses equal sign and semicolon [OK]
Hint: Use weight=number; syntax for servers [OK]
Common Mistakes:
  • Omitting equal sign
  • Using colon instead of equal sign
  • Using parentheses incorrectly
3. Given this nginx upstream block:
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?
medium
A. 2
B. 6
C. 4
D. 1

Solution

  1. Step 1: Calculate total weight

    backend1 weight=3, backend2 weight=1, total = 4.
  2. Step 2: Calculate requests for backend2

    Requests for backend2 = (1/4) * 8 = 2.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Requests proportional to weight [OK]
Hint: Requests split by weight ratio [OK]
Common Mistakes:
  • Ignoring total weight sum
  • Assigning equal requests despite weights
  • Miscounting total requests
4. You configured nginx with weighted round-robin but all servers receive equal requests. What is the most likely cause?
medium
A. nginx does not support weighted round-robin
B. Weights were not specified correctly in the config
C. The servers have identical IP addresses
D. The upstream block is missing the weight directive

Solution

  1. Step 1: Understand weighted round-robin requires correct weight syntax

    If weights are missing or incorrect, nginx defaults to equal distribution.
  2. Step 2: Analyze the options

    Weights were not specified correctly in the config states weights not specified correctly, which explains equal requests.
  3. Final Answer:

    Weights were not specified correctly in the config -> Option B
  4. Quick Check:

    Incorrect weights cause equal load [OK]
Hint: Check weight syntax if load is equal [OK]
Common Mistakes:
  • Assuming nginx lacks weighted support
  • Confusing IP issues with load balancing
  • Missing weight directive entirely
5. You have three servers with weights 5, 3, and 2. You want to add a fourth server but keep the total weight sum at 10. What weight should you assign to the new server to maintain the same total weight and keep the relative proportions of the first three servers?
hard
A. 0 (do not add the server)
B. 5
C. 2
D. 1

Solution

  1. Step 1: Calculate current total weight

    Sum of existing weights = 5 + 3 + 2 = 10.
  2. Step 2: Determine weight for new server

    To keep total sum at 10 when adding a fourth server, assign weight=0 to it.
  3. 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).
  4. Final Answer:

    0 (do not add the server) -> Option A
  5. Quick Check:

    Weight=0 keeps total and proportions unchanged [OK]
Hint: Assign weight=0 to keep total sum and proportions [OK]
Common Mistakes:
  • Assigning positive weight that increases total sum
  • Forgetting to maintain exact total of 10
  • Ignoring impact on relative proportions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard