When it comes to cluster load balancing, Dubbo provides multiple balancing strategies, with the default being weighted random
, a weighted random load balancing strategy.
In terms of implementation, Dubbo provides client-side load balancing, meaning the Consumer determines which Provider instance to submit the request to using a load balancing algorithm.
Currently, Dubbo has the following built-in load balancing algorithms, which can be enabled by adjusting configuration items.
Algorithm | Features | Remarks |
---|---|---|
Weighted Random LoadBalance | Weighted Random | Default algorithm, default weights are equal |
RoundRobin LoadBalance | Weighted Round Robin | Inspired by Nginx’s smooth weighted round-robin algorithm, default weights are equal, |
LeastActive LoadBalance | Least Active First + Weighted Random | Based on the idea of rewarding the more capable |
Shortest-Response LoadBalance | Shortest Response First + Weighted Random | Focuses more on response speed |
ConsistentHash LoadBalance | Consistent Hash | Deterministic input, deterministic provider, suitable for stateful requests |
P2C LoadBalance | Power of Two Choice | Randomly selects two nodes, then chooses the one with the smaller “number of connections”. |
Adaptive LoadBalance | Adaptive Load Balancing | Based on the P2C algorithm, selects the node with the smallest load among the two |
During weighted round-robin, if a node’s weight is too large, there can be an issue of calls being overly concentrated in a certain period.
For example, if the weights of nodes A, B, and C are as follows: {A: 3, B: 2, C: 1}
Then according to the most primitive round-robin algorithm, the call process will become: A A A B B C
To address this, Dubbo has optimized this by borrowing from Nginx’s smooth weighted round-robin algorithm. The call process can be abstracted into the following table:
Pre-round Sum Weights | Winner of This Round | Total Weight | Post-round Weights (Winner’s weight minus total weight) |
---|---|---|---|
Initial Round | \ | \ | A(0), B(0), C(0) |
A(3), B(2), C(1) | A | 6 | A(-3), B(2), C(1) |
A(0), B(4), C(2) | B | 6 | A(0), B(-2), C(2) |
A(3), B(0), C(3) | A | 6 | A(-3), B(0), C(3) |
A(0), B(2), C(4) | C | 6 | A(0), B(2), C(-2) |
A(3), B(4), C(-1) | B | 6 | A(3), B(-2), C(-1) |
A(6), B(0), C(0) | A | 6 | A(0), B(0), C(0) |
We found that after a total of weighted rounds (3+2+1), the cycle returns to the starting point. Throughout the process, the node traffic is smooth, and even within a very short time period, the probability is distributed as expected.
If users have a need for weighted round-robin, they can use this algorithm with confidence.
The response time here = the average response time of a provider within the window time, with the default window time being 30s.
<dubbo:parameter key="hash.arguments" value="0,1" />
<dubbo:parameter key="hash.nodes" value="320" />
The Power of Two Choice algorithm is simple yet classic, with the main idea as follows:
Here is the Dubbo P2C Algorithm Implementation Proposal
Adaptive load balancing is an algorithm that automatically adjusts traffic distribution based on the load of backend instances. It always tries to forward requests to the node with the least load.
Here is the Dubbo Adaptive Algorithm Implementation Proposal
Dubbo supports configuring the default load balancing strategy on the provider side, so all consumers will use the provider-specified load balancing strategy by default. Consumers can configure the load balancing strategy they want to use. If neither is configured, the default random load balancing strategy is used.
Within the same application, different services can be configured to use different load balancing strategies, and different methods of the same service can be configured to use different load balancing strategies.
For specific configuration methods, refer to the following multi-language implementations:
Load balancing strategies support custom extension implementations. For details, see Dubbo Extensibility