Backend Development 7 min read

Investigation of HttpClient Connection Reuse Issue and Tomcat maxKeepAliveRequests Setting

The article documents a systematic investigation of why an Apache HttpClient‑based utility class fails to reuse connections, reproduces the problem with a test server and client, analyzes Tomcat's default maxKeepAliveRequests limit of 100, and provides recommendations for configuration.

Zhuanzhuan Tech
Zhuanzhuan Tech
Zhuanzhuan Tech
Investigation of HttpClient Connection Reuse Issue and Tomcat maxKeepAliveRequests Setting

We built a reusable HTTP client wrapper HttpClientUtil based on Apache HttpClient with a connection pool, and promoted connection reuse as a key feature. A colleague reported that connections were not being reused, prompting a detailed investigation.

Verification – Server side : A simple web service was deployed on 192.168.149.18 exposing a GET endpoint http://192.168.149.18:8087/hello . 请求URL:http://192.168.149.18:8087/hello 请求方式:GET 返回结果:Hello,adu!

Verification – Client side : A test method repeatedly invoked the endpoint 300 times using the wrapper:

@Test
public void httpGetTest() throws Exception {
    for (int i = 1; i <= 300; i++) {
        String url = "http://192.168.149.18:8087/hello?i=" + i;
        String res = HttpClientUtil.httpGet(url);
        logger.info("i={},res={}", i, res);
        Thread.sleep(10);
    }
}

Network traffic was captured with tcpdump -n host 192.168.149.18 -w tcp.pcap and examined in Wireshark. The capture showed that after every 100 requests the server sent a FIN packet, forcing the client to close the connection and establish a new one via a three‑way handshake.

Analysis : The pattern (reconnections at request numbers 100, 200, 300) matched Tomcat's default maxKeepAliveRequests setting, which limits the number of requests per persistent connection to 100. This parameter is defined in org.apache.tomcat.util.net.AbstractEndpoint and can be adjusted; setting it to 0 disables the limit.

The Apache documentation states that MaxKeepAliveRequests controls how many requests a Keep‑Alive connection may handle, recommending a high value for performance while acknowledging that unlimited connections increase server resource consumption.

In HTTP/1.1, reusing connections reduces handshake overhead for both client and server, but server resources are finite, so limits such as keepalivetimeout (default 5 seconds) and maxKeepAliveRequests (default 100) are applied.

Conclusion : For optimal performance, increase maxKeepAliveRequests if the workload requires more than 100 requests per connection, but balance this against server load and load‑balancing considerations. Monitoring connection reuse and adjusting keep‑alive settings are essential for high‑throughput backend services.

BackendJavaPerformanceTomcatkeepaliveHttpClientConnectionPooling
Zhuanzhuan Tech
Written by

Zhuanzhuan Tech

A platform for Zhuanzhuan R&D and industry peers to learn and exchange technology, regularly sharing frontline experience and cutting‑edge topics. We welcome practical discussions and sharing; contact waterystone with any questions.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.