Search code examples
spring-bootspring-webfluxreactor-nettyspring-resttemplate

Decrease initial lag in webclient call


I am currently using Webclient of spring-webflux package to make synchronous REST calls.

But the time taken to mae the first request is longer than the time taken by RestTemplate.

I have observed that the successive calls take much lesser time and more or less same to that of RestTemplate.

Is there a solution to decrease the initial lag for Webclient?


Solution

  • By default, the initialization of the HttpClient resources happens on demand. This means that the first request absorbs the extra time needed to initialize and load:

    • the event loop group
    • the host name resolver
    • the native transport libraries (when native transport is used)
    • the native libraries for the security (in case of OpenSsl)

    You can preload these resources - check this documentation

    Things that cannot be preloaded are:

    • host name resolution happens with the first request
    • in case a connection pool is used (the default) - with the first request, a connection to the URL is established, the subsequent requests to the same URL reuse the connections from the pool so they are faster.