[SOLVED] Apache HttpClient: first call very slow, following calls very fast

Issue

I am using Apache HttpClient 4.5.13 since some years and I have no problems until today.

In a Springboot app when it call a specific server, the first call (or the first call after some inactivity) required about 8 seconds (!!!) then all the following calls (to the same server) are very fast (less than a second).
(The same call with curl always takes less than a second)
If i wait some time (for example, an hour) and retry, the first call takes again 8 seconds and the following are very fast.

What could be the reason of the slow first call?
I used this client for years and I never had this problem.
A bit of code:

RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(Integer.parseInt(env.getProperty("httpclient.connection.timeout")))
                .setConnectionRequestTimeout(Integer.parseInt(env.getProperty("httpclient.connection.request.timeout")))
                .setSocketTimeout(Integer.parseInt(env.getProperty("httpclient.socket.timeout")))
                .setCookieSpec("easy")
                .build();
        
                    
        result = HttpClients.custom().setSSLContext(sslContext)
                    .setSSLHostnameVerifier(getHostnameVerifier())
                    .setConnectionManager(poolingConnManager)
                    .setDefaultRequestConfig(requestConfig)
                    .setDefaultSocketConfig(socketConfig)
                    .setDefaultCookieSpecRegistry(r)
                    .addInterceptorFirst(customHttpRequestInterceptor)
                    .addInterceptorLast(customHttpResponseInterceptor)
                    .build();

I also enabled Http debug but I don’t see anything strange, the server response arrive after 8 seconds:

[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "POST /service/url1 HTTP/1.1[\r][\n]"
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "Authorization: Bearer eyJjdHkiOiJKV1QiLCJyZWFsb[...][\r][\n]"
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "X-Tax-Code: AAAAAAAAAAAAAAA01[\r][\n]"
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "Content-type: application/json[\r][\n]"
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "accept: application/json[\r][\n]"
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "Content-Length: 25[\r][\n]"
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "Host: serverhost[\r][\n]"
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "Connection: Keep-Alive[\r][\n]"
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "User-Agent: Apache-HttpClient/4.5.13 (Java/1.8.0_252)[\r][\n]"
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "Accept-Encoding: gzip,deflate[\r][\n]"
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "[\r][\n]"
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "{"product_name":"XXXXXXX"}"
**Server response arrives after 8 seconds here**
[2022-01-22 17:11:48][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 << "HTTP/1.1 200 [\r][\n]"

UPDATE: I rewrite the client using OkHttpClient and I got the same slow call, so I think that it doesn’t depend on client.

Solution

After some days I finally got the same slow responses with curl.

So it is a server side problem.

Answered By – pacionet

Answer Checked By – Mildred Charles (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *