Load Balancing Strategies and Configuration Details

Load balancing strategies and their usage methods supported by Dubbo

Dubbo has a built-in client-based load balancing mechanism. Below are the currently supported load balancing algorithms. With the automatic service discovery mechanism mentioned earlier, the consumer will automatically use the Weighted Random LoadBalance strategy for service calls.

If you want to adjust the load balancing algorithm, the following are built-in load balancing strategies in the Dubbo framework:

AlgorithmFeaturesRemarksConfiguration Value
Weighted Random LoadBalanceWeighted RandomDefault algorithm, same default weightrandom (default)
RoundRobin LoadBalanceWeighted Round RobinBased on Nginx’s smooth weighted round-robin algorithm, same default weightroundrobin
LeastActive LoadBalanceLeast Active + Weighted RandomBased on the “to each according to their ability” conceptleastactive
Shortest-Response LoadBalanceShortest Response + Weighted RandomMore focused on response speedshortestresponse
ConsistentHash LoadBalanceConsistent HashDeterministic input, deterministic provider, suitable for stateful requestsconsistenthash
P2C LoadBalancePower of Two ChoiceRandomly selects two nodes and then chooses the one with fewer connections.p2c
Adaptive LoadBalanceAdaptive Load BalancingBased on P2C algorithm, chooses the node with the least load among the twoadaptive

Global Configuration

The default strategy of the Dubbo framework is random weighted random load balancing. To adjust the strategy, simply set the corresponding loadbalance value. For each load balancing strategy value, refer to the table at the top of this document.

Specify global configuration for all service calls:

dubbo:
  consumer:
    loadbalance: roundrobin

Interface Level Configuration

Different load balancing strategies can be specified for each service.

Set on the provider side as the default value for the consumer:

@DubboService(loadbalance = "roundrobin")
public class DemoServiceImpl implements DemoService {}

Set on the consumer side, which has higher priority:

@DubboReference(loadbalance = "roundrobin")
private DemoService demoService;

Method Level Configuration

Load balancing strategies can also be specified at the method level.

In Spring Boot development mode, there are the following ways to configure method-level parameters:

JavaConfig

@Configuration
public class DubboConfiguration {
    @Bean
    public ServiceBean demoService() {
        MethodConfig method = new MethodConfig();
        method.setName("sayHello");
        method.setLoadbalance("roundrobin");

        ServiceBean service = new ServiceBean();
        service.setInterface(DemoService.class);
        service.setRef(new DemoServiceImpl());
        service.addMethod(method);
        return service;
    }
}
@Autowired
private DemoService demoService;

@Configuration
public class DubboConfiguration {
    @Bean
    public ReferenceBean demoService() {
        MethodConfig method = new MethodConfig();
        method.setName("sayHello");
        method.setLoadbalance("roundrobin");

        ReferenceBean<DemoService> reference = new ReferenceBean<>();
        reference.setInterface(DemoService.class);
        reference.addMethod(method);
        return reference;
    }
}

dubbo.properties

dubbo.reference.org.apache.dubbo.samples.api.DemoService.sayHello.loadbalance=roundrobin

Consistent Hash Configuration

By default, the first parameter is used as the hash key. If you need to switch parameters, you can specify the hash.arguments property.

ReferenceConfig<DemoService> referenceConfig = new ReferenceConfig<DemoService>();
// ... init
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("hash.arguments", "1");
parameters.put("sayHello.hash.arguments", "0,1");
referenceConfig.setParameters(parameters);
referenceConfig.setLoadBalance("consistenthash");
referenceConfig.get();

Adaptive Load Balancing Configuration

Simply set loadbalance to p2c or adaptive on the consumer or provider side. You can refer to Working Principle.

Last modified September 30, 2024: Translate (22d3d83a3b)