spring-boottomcatspring-webfluxnettyproject-reactor

Spring WebClient Request Delay due to Inactive Connection


I'm using Spring WebClient in my Spring Boot application to call external APIs. Application is running on JBoss EAP 7.3.x, Java 8, Springboot 2.7.1 is used. I've configured connection pooling to improve performance, but I'm experiencing a consistent issue:

  1. The first request after application startup takes 13-15 seconds. This, I was able to resolve with a warmup request, but I'm open to proper ways of warmup.
  2. Subsequent requests are much faster.
  3. If there is a period of inactivity (e.g., 1-2 minutes), the next request again takes 13-15 seconds.

I have detailed logs that indicate the connection is being closed after inactivity:

DEBUG reactor.netty.http.client.HttpClient - [....] READ COMPLETE
DEBUG reactor.netty.http.client.HttpClient - [....] USER_EVENT: SslCloseCompletionEvent
DEBUG reactor.netty.http.client.HttpClient - [....] INACTIVE
DEBUG r.n.r.DefaultPooledConnectionProvider - [1a115ad9, L:/ip_address ! test.mysite.net/ip_of_target:443] onStateChange(PooledConnection{channel=[id: 1a115ad9, L:/ip_address ! R
.mysite.net/target_ip:443}}, [disconnecting])
DEBUG reactor.netty.http.client.HttpClient - [....] UNREGISTERED
@Configuration
public class WebClientConfig {

    @Bean
    public ConnectionProvider connectionProvider() {
        return ConnectionProvider.builder("MyApplicationConnection")
            .maxConnections(100) 
            .pendingAcquireTimeout(Duration.ofSeconds(60)) 
            .maxIdleTime(Duration.ofMinutes(30)) 
            .maxLifeTime(Duration.ofMinutes(60)) 
            .evictInBackground(Duration.ofMinutes(5)) 
            .build();
    }

    @Bean
    public HttpClient httpClient(ConnectionProvider connectionProvider) {
        return HttpClient.create(connectionProvider)
            .doOnConnected(connection ->
                connection.addHandlerLast(new ReadTimeoutHandler(10)) 
                          .addHandlerLast(new WriteTimeoutHandler(10))) 
            .wiretap("reactor.netty.http.client.HttpClient", LogLevel.DEBUG, AdvancedByteBufFormat.TEXTUAL); 
    }

    @Bean
    public WebClient webClient(HttpClient httpClient) {
        return WebClient.builder()
            .clientConnector(new ReactorClientHttpConnector(httpClient))
            .baseUrl("https://test.mysite.net")
            .defaultHeader(HttpHeaders.CONNECTION, "keep-alive") 
            .build();
    }
}

I have tried increasing maxIdleTime and maxLifeTime but this had no effect. I want my subsequent requests to be faster, even if there's a period of inactivity. Basically, my application is an API based application that would invoke other APIs using webclient. If there are no requests coming to my API for some time, that shouldn't make the next request slow.


Solution

  • The issue was resolved after enabling DNS caching. Yes, inactive connections get released but the issue was associated with extra DNS lookups on each new connections.

    return HttpClient.create(connectionProvider)
            .resolver(DefaultAddressResolverGroup.INSTANCE)