aws-sdk-java-v2 icon indicating copy to clipboard operation
aws-sdk-java-v2 copied to clipboard

UrlConnectionHttpClient to support configurable idle connection timeout

Open sjfaz opened this issue 2 years ago • 2 comments

Describe the feature

When using the DynamoDBClient in the Java v2 SDK with the UrlConnectionHttpClient idle connections timeout after only 5 seconds. This means that if there is no activity on the connection for 5 seconds, the connection will be closed by the client. The next request to DynamoDB will need to establish a new connection and will likely need to connect to a different DynamoDB front-end instance. There is no built-in setting to be able to configure this idle connection timeout currently.

In comparison, when using the Apache HTTP client with the DynamoDBClient, the default idle connection timeout lasts for 60 seconds and can also be configured.

The new feature should allow the idle connection time for UrlConnectionHttpClient to be configurable, similar to how the Apache Client works, with a default timeout of 60 seconds.

Use Case

Having connection re-use with DynamoDB is advantageous because it means requests can return to the same DynamoDB front-end instance more often and this has a latency benefit as it can re-use cached metadata.

Connection re-use can occur even across lambda invocations (if the client is declared outside the handler). This HTTP client has been widely recommended as a good lean option (small jar file size) for Lambda.

If the idle connection timeout can be increased from 5 seconds to 60+ seconds then it will mean higher connection re-use and improved latency in many cases.

Proposed Solution

I propose a new feature to make the UrlConnectionHttpClient configurable with the CONNECTION_MAX_IDLE_TIMEOUT setting, similar to how the Apache client works, with a default timeout of 60 seconds.

Other Information

I am aware of a workaround by setting the below, but it's not ideal, to support natively would be much better.

System.setProperty("http.keepAlive.time.server", "120");

Acknowledgements

  • [ ] I may be able to implement this feature request
  • [ ] This feature might incur a breaking change

AWS Java SDK version used

2.20.46

JDK version used

Java 11

Operating System and version

MacOS Monterey 12.6.5

sjfaz avatar Apr 24 '23 09:04 sjfaz

Request acknowledged.

Community note: please vote by adding a 👍 reaction to the original issue to help us prioritize this request.

debora-ito avatar May 03 '23 01:05 debora-ito

Hi @debora-ito

Based on a detailed analysis of UrlConnectionHttpClient and ApacheHttpClient, I'd like to provide some insights into the feasibility of adding custom idle connection timeout support to UrlConnectionHttpClient.

Key Findings

  • HttpURLConnection (used by UrlConnectionHttpClient) manages connection pooling at the JVM level, not at the application level.
    The default idle timeout is 5 seconds, but it cannot be controlled per connection—only via system-wide properties (jdk.httpclient.keepalive.timeout).

  • UrlConnectionHttpClient does not have an active connection manager. Unlike ApacheHttpClient, it relies on the JVM’s default keep-alive behavior, with no built-in way to close idle connections.

  • ApacheHttpClient already provides full idle timeout customization. It offers connection pooling, eviction policies, and configurable idle timeouts via connectionMaxIdleTimeout(Duration.ofSeconds(x)).

Image

While adding configurable idle timeout support to UrlConnectionHttpClient seems useful, it is not technically feasible due to the JVM-level pooling limitations in HttpURLConnection.

🔹 Given that ApacheHttpClient already provides robust and configurable connection pooling, the engineering effort required to "work around" HttpURLConnection's limitations might not be justified. 🔹 Instead of modifying UrlConnectionHttpClient, users needing idle timeout support should be encouraged to use ApacheHttpClient, which already solves this problem efficiently.

roamariei avatar Feb 28 '25 16:02 roamariei