Understanding Ribbon Load Balancing and Eager-Load Mode in Feign Microservices
This article explains how Feign uses Ribbon for remote calls, details Ribbon's load‑balancing mechanisms, enumerates built‑in strategies, and demonstrates how to enable eager‑load mode to pre‑warm clients and avoid first‑request latency in Java microservice environments.
Introduction
First, understand how Feign performs remote calls, involving a registry, load balancer, and FeignClient relationships. Microservices register with Eureka or Nacos, Feign uses Ribbon for load balancing, which obtains the service list from the registry and caches it locally before the client invokes a service.
How Ribbon Performs Load Balancing
Ribbon obtains the service list from Nacos/Eureka, which is crucial for load balancing.
RibbonClientConfiguration
In the RibbonClientConfiguration class, Ribbon relies on LoadBalancer and the ILoadBalancer interface methods such as adding new services, selecting a service, marking a server down, retrieving the service list, and getting all servers (healthy and unhealthy).
ZoneAwareLoadBalancer
The default load balancer is ZoneAwareLoadBalancer , which extends DynamicServerListLoadBalancer . Its restOfInit method calls two important methods: enableAndInitLearnNewServersFeature and updateListOfServers .
LOGGER.info("Using serverListUpdater {}", serverListUpdater.getClass().getSimpleName());
serverListUpdater.start(updateAction);The ServerListUpdater.start method creates a custom thread to fetch the service list.
Ribbon Load Balancing Strategies
There are seven built‑in strategies:
RoundRobinRule – round‑robin selection.
WeightedResponseTimeRule – prefers services with shorter response times.
RandomRule – random selection.
BestAvailableRule – selects the instance with the fewest active connections.
RetryRule – retries failed services within a timeout.
AvailabilityFilteringRule – filters out unhealthy instances.
ZoneAvoidanceRule – zone‑aware selection.
Custom strategies can be implemented as described in the referenced article.
Ribbon Eager‑Load Mode
By default, Ribbon creates a client only when the first HTTP request occurs, which adds client‑creation latency to the first call. Eager‑load pre‑loads the client at application startup to avoid this delay.
@GetMapping("/requestSystem2Api")
public String requestSystem2Api() {
long startTime = System.currentTimeMillis();
R
stringR = iTestServiceClient.testRequestMethod();
if (null != stringR) {
log.info("接口返回:" + stringR.getMsg());
}
long needTime = System.currentTimeMillis() - startTime;
log.info("接口调用需要的时间:" + needTime);
return "";
}Log analysis shows that the first call incurs extra time because DynamicServerListLoadBalancer loads the Feign client, while subsequent calls are fast.
Enabling Ribbon Eager‑Load
ribbon:
nacos:
enabled: true # enable nacos polling
eager-load:
enabled: true # turn on eager‑load to prevent first‑request timeout
clients: Lxlxxx-system2 # specify services to eager‑load
ReadTimeout: 10000
ConnectTimeout: 10000
MaxAutoRetries: 0
MaxAutoRetriesNextServer: 1
OkToRetryOnAllOperations: falseWhen the application starts, logs confirm that the Lxlxxx-system2 service is loaded, eliminating the first‑request timeout.
Conclusion
Eager‑load works like a client‑side pre‑warm, loading services at startup to prevent timeouts caused by complex business logic or heavy data processing during inter‑service calls.
Reference
[1] Ribbon custom load‑balancing strategy: https://juejin.cn/post/7155754618486521869
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.