JD Interview: Why is the First HTTP Call So Slow?

Click “Architecture Wang” to follow and pin the official accountDaily technical content delivered promptly!JD Interview: Why is the First HTTP Call So Slow?01IntroductionJD Interview: Why is the First HTTP Call So Slow?First, we need to understand how Feign performs remote calls, which includes the relationship between the registration center, load balancing, and FeignClient. Microservices register with the server through either Eureka or Nacos. Feign relies on Ribbon for load balancing, which requires obtaining the service list from the registration center and caching the services locally. Then, the FeignClient makes the call. This is the general process.JD Interview: Why is the First HTTP Call So Slow?JD Interview: Why is the First HTTP Call So Slow?02How Ribbon Performs Load BalancingJD Interview: Why is the First HTTP Call So Slow?First, we need to clarify how Ribbon performs load balancing, specifically how it obtains the service list from Nacos or Eureka, which is crucial.RibbonClientConfigurationIn the RibbonClientConfiguration class, through LoadBalancer, we know that Ribbon relies on LoadBalancer for load balancing. The methods of the ILoadBalancer interface include adding new services, selecting a service from the load balancer, marking a server as down, obtaining the service list, getting alive servers, and retrieving all servers (both healthy and unhealthy).JD Interview: Why is the First HTTP Call So Slow?ILoadBalancer InterfaceZoneAwareLoadBalancerThe default load balancer is ZoneAwareLoadBalancer, which inherits from the parent class DynamicServerListLoadBalancer. The two important methods in the restOfInit method are enableAndInitLearnNewServersFeature and updateListOfServers.JD Interview: Why is the First HTTP Call So Slow?restOfInit MethodInside the enableAndInitLearnNewServersFeature method.

LOGGER.info(Using serverListUpdater {}, serverListUpdater.getClass().getSimpleName());
serverListUpdater.start(updateAction);

Let’s look at the implementation of the ServerListUpdater.start method, which uses a custom thread to fetch the service list.JD Interview: Why is the First HTTP Call So Slow?ServerListUpdater.startJD Interview: Why is the First HTTP Call So Slow?03Ribbon Load Balancing StrategiesJD Interview: Why is the First HTTP Call So Slow?Having discussed how to obtain the service list, it is also necessary to talk about the load balancing strategies, which mainly include seven types:

  • RoundRobinRule (Round-robin strategy, calls services in order)
  • WeightedResponseTimeRule (Weighted response time strategy, prioritizes services with shorter response times)
  • RandomRule (Random strategy, randomly selects a service from the provider list)
  • BestAvailableRule (Least connections strategy, selects the service instance with the least connections)
  • RetryRule (Retry strategy, retries obtaining a failed service, returns NULL if not obtained within a specified time)
  • AvailabilityFilteringRule (Availability-sensitive strategy, filters unhealthy service instances)
  • ZoneAvoidanceRule (Zone-sensitive strategy)

JD Interview: Why is the First HTTP Call So Slow?04Ribbon Eager Load ModeJD Interview: Why is the First HTTP Call So Slow?Ribbon creates the load client only after the service starts and when a call is made. Therefore, during the first HTTP request, not only the HTTP request time is considered, but also the client creation time, which is why the first call is slow. Here’s a method to call it.System service calls System2 service

@GetMapping(/requestSystem2Api)
public String requestSystem2Api(){
    long startTime = System.currentTimeMillis();
    R<String> stringR = iTestServiceClient.testRequestMethod();
    if (null !=stringR){
        log.info(接口返回:+stringR.getMsg());
    }
    long needTime = System.currentTimeMillis() - startTime;
    log.info(接口调用需要的时间:+needTime);
    return 
;
}

From the call log, we can see that the first call to the System2 service causes Ribbon’s DynamicServerListLoadBalancer to load the Feign client and make the call, resulting in a longer time for the first call. The second call can be seen to be much faster.JD Interview: Why is the First HTTP Call So Slow?First call is slow, second call is fast JD Interview: Why is the First HTTP Call So Slow?05Enabling Ribbon Eager LoadJD Interview: Why is the First HTTP Call So Slow?

ribbon:
  nacos:
    enabled: true # Enable Nacos polling
  eager-load:
    enabled: true # Enable Ribbon's eager load mode (to prevent first request timeout)
  clients: Lxlxxx-system2 # Specify the service to enable (needs to enable Ribbon's eager load mode)
  ReadTimeout: 10000
  ConnectTimeout: 10000
  MaxAutoRetries: 0
  MaxAutoRetriesNextServer: 1
  OkToRetryOnAllOperations: false

When the project starts, you can see from the logs that the Lxlxxx-system2 service has been loaded, thus avoiding the first request timeout issue.JD Interview: Why is the First HTTP Call So Slow?Enabling Ribbon Eager LoadJD Interview: Why is the First HTTP Call So Slow?06ConclusionJD Interview: Why is the First HTTP Call So Slow?This eager load mode is similar to a “client load preheating” operation, which loads during project startup to prevent service calls from timing out due to data volume or complexity of business logic. If your service calls are complex and slow, you might want to try this solution.Source: juejin.cn/post/7249624466150408250

Leave a Comment