Load Balancing
Basic Concepts and Principles
Load balancing is a technology that distributes workload or network traffic across multiple computing resources. Its core goal is to optimize resource usage, maximize throughput, minimize response time through reasonable request distribution, while avoiding overloading a single resource.
Load Balancing Strategy Types
1. Random Strategy
- Default strategy
- Implementation principle: Randomly select one from available service providers
- Characteristics: Simple and efficient, but cannot consider actual load situation of each node
- Applicable scenarios: When providers have similar performance and call pressure is not high
2. Round Robin Strategy
- Implementation principle: Call each service provider in a fixed order
- Characteristics: Fair distribution of requests, but cannot consider response time differences
- Applicable scenarios: When providers have similar performance and processing capabilities
3. Least Active Strategy
- Implementation principle: Prefer to select the provider with the fewest currently processing requests
- Characteristics: Dynamically sense provider load situation, more intelligent
- Applicable scenarios: When there are large differences in processing capabilities among providers
4. Consistent Hash Strategy
- Implementation principle: Requests with the same parameters always go to the same provider
- Characteristics: Can utilize local cache, avoid frequent provider switching
- Applicable scenarios: Scenarios requiring session state preservation
Configuration Example
<!-- Service provider configuration -->
<dubbo:service interface="..." loadbalance="roundrobin"/>
<!-- Service consumer configuration -->
<dubbo:reference interface="..." loadbalance="leastactive"/>
Custom Balancer
- Implement custom load balancer
public class WzkLoadBalance implements LoadBalance {
@Override
public <T> Invoker<T> select(List<Invoker<T>> invokers, URL url,
RpcException invocation) throws RpcException {
// Implement custom load balancing logic
int index = (int) (System.currentTimeMillis() % invokers.size());
return invokers.get(index);
}
}
- SPI Configuration
File path: META-INF/dubbo/org.apache.dubbo.rpc.cluster.LoadBalance
wzk=com.example.loadbalance.WzkLoadBalance
- Use custom load balancer
@Reference(loadbalance = "wzk")
private DemoService demoService;
Advanced Configuration and Optimization
- Weight Configuration: Can set weights for different service providers
<dubbo:provider weight="100"/>
<dubbo:provider weight="200"/>
- Dynamic Adjustment: Some advanced load balancers support dynamic strategy adjustment
- Health Checks: Combine with health check mechanism to automatically remove unhealthy nodes
- Zone Awareness: Prefer to select providers in the same machine room or same region