nahi/httpclient — Issue #68: Much higher connection number than needed
Author: xb
Created: 2011-10-12 16:42:18+00:00
State: open

It looks lik e httpclient employs a LRU strategy in choosing which TCP connection to a HTTP server to re-use when there is a new HTTP request instead of using an MRU strategy.

# scenario
Consider following scenario: The user of httpclient uses 5 requests per second, each request takes 2 seconds to complete. The server has a timeout of 60 seconds until the server closes the TCP connection. Consider furthermore that there was a recent usage spike and thus there are 500 TCP connections between httpclient and the HTTP server.

# expected result
It would be expected that, over time, the number of open TCP connections drops from 500 to about 10 (because, on average, every 0.2 seconds, there will be 1 TCP connection becoming free to be reused, and every 0.2 seconds, there will be a new HTTP request to be processed, keeping about 10 TCP connections busy).

# actual result
The number of open TCP connections drops from 500 to about 300.

# analysis
The reason is that httpclient does not let any of the 300 TCP connections to time out, because it does not re-use the most recently used TCP connection first (resulting in many warm TCP connections being cooled down and finally timed out), but it reuses the least recently used TCP connection first (resulting in many warm TCP connections being kept warm and thus never giving them a chance to time out).

# recommended solution
Use an MRU approach.