How Nginx’s Smooth Weighted Round‑Robin Load Balancing Works
This article explains Nginx's five load‑balancing methods, focusing on the weighted round‑robin algorithm, its basic and smooth variants, provides code examples, step‑by‑step execution, and a mathematical proof of why the smooth version distributes traffic fairly according to server weights.
Weighted Round‑Robin in Nginx
Nginx supports five load‑balancing algorithms; this article concentrates on the weight‑based round‑robin method, where each server is assigned a weight value and the probability of being selected is proportional to that weight.
upstream backend {
server 10.xxx.xxx.1 weight=10;
server 10.xxx.xxx.2 weight=20;
}After a full cycle, the server with weight 10 will be chosen roughly half as often as the server with weight 20 (a 1:2 ratio). The basic round‑robin simply picks the next node in order, e.g., {a, b, c}, which can lead to bursts when weights differ.
When weights differ, the naive weighted round‑robin may produce a sequence such as {a, a, a, a, b, b, c}, concentrating load on the high‑weight server.
Smooth Weighted Round‑Robin
Algorithm is as follows: on each peer selection we increase current_weight of each eligible peer by its weight, select the peer with the greatest current_weight and reduce its current_weight by the total number of weight points distributed among peers.
The algorithm executes in two steps for each selection:
Increase each node’s current_weight by its own weight.
Select the node with the highest current_weight and subtract the sum of all weights from that node’s current_weight.
For example, with nodes a, b, c having weights 5, 1, 1, the selection sequence becomes {a, a, b, a, c, a, a}, which is smoother than the naive sequence {c, b, a, a, a, a, a}.
The proof shows that after each full round the sum of all current_weight values returns to zero, guaranteeing that each node is chosen a number of times proportional to its weight and that no node is selected repeatedly in a burst.
Conclusion
The smooth weighted round‑robin algorithm can be implemented with just two simple steps and provides fair, weight‑proportional distribution of requests across backend servers.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
