Ribbon Load Balancing

📢 This article was translated by gemini-3-flash-preview

When a consumer initiates a request, it’s intercepted by Ribbon. Ribbon pulls the provider list from Eureka, Eureka returns the list, and Ribbon selects a server based on IRule to send the request.

Detailed interception flow: Request -> DynamicServerListLoadBalancer (extracts service ID from URL, e.g., userService) -> DynamicServerListLoadBalancer -> Eureka -> DynamicServerListLoadBalancer -> IRule -> DynamicServerListLoadBalancer -> Execute Request.

Load Balancing Strategies

Built-in Load Balancing Rule ClassDescription
ZoneAvoidanceRule (Eureka Default)Selects servers based on zone availability. Uses Zones to categorize servers (e.g., a data center or a rack) and then performs round-robin within the Zone.
RoundRobinRuleSimple round-robin through the service list. This is the default Ribbon rule.
AvailabilityFilteringRuleIgnores the following two types of servers:
(1) By default, if a connection fails 3 times, the server is set to a “short-circuit” state for 30 seconds. If it fails again, the duration increases exponentially.
(2) Servers with high concurrency. Clients configured with this rule will ignore servers where the active connection count is too high. The upper limit can be configured via <clientName>.<clientConfigNameSpace>.ActiveConnectionsLimit.
WeightedResponseTimeRuleAssigns a weight to each server. The longer the response time, the lower the weight. Servers are chosen randomly, but the weight influences the selection probability.
BestAvailableRuleIgnores “short-circuited” servers and picks the one with the lowest concurrent connection count.
RandomRuleRandomly picks an available server.
RetryRuleSelection logic with a retry mechanism.

Using the Random Strategy

You can modify the load balancing rule by defining an IRule implementation. There are two ways to do this:

  1. Code Method: Define a new IRule in a configuration class (Global setting).
1
2
3
4
@Bean
public IRule randomRule(){
    return new RandomRule();
}
  1. Configuration File Method: Add configuration to the application.yml of the consumer (e.g., orderServer) to modify the rule for a specific service.
1
2
3
userService: # Configure load balancing for a specific microservice
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule # Load balancing rule 

Eager Loading

Ribbon uses lazy loading by default, meaning it creates the LoadBalanceClient only during the first access. This can result in a long initial request time. Eager loading creates it when the project starts, reducing the latency of the first request. Enable eager loading in the config:

1
2
3
4
ribbon:
  eager-load:
    enabled: true
    clients: userService # Specify eager loading for the userService