AWS Developer Tools Blog

Announcing availability of the AWS CRT HTTP Client in the AWS SDK for Java 2.x

We are excited to announce the general availability (GA) of the AWS Common Runtime (CRT) HTTP Client in the AWS SDK for Java 2.x. With release version 2.20.0 of the SDK, the AWS CRT HTTP Client can now be used in production environments.

The AWS CRT HTTP Client is an asynchronous, non-blocking HTTP client that can be used by AWS service clients to invoke AWS APIs. You can use it as an alternative to the default Netty implementation of the SdkAsyncHttpClient interface. It offers faster SDK startup time and smaller memory footprint, which is especially important with AWS Lambda, as well as lower P90 latency and enhanced connection management.

Faster startup time and smaller memory footprint

The AWS CRT HTTP Client is built on top of the Java bindings of the AWS CRT, which is written in C. It has faster startup time than other HTTP clients supported in the SDK.

We have observed an improvement of up-to 76% in startup latency and a reduction of up-to 14% in memory usage in Lambda when switching from the Netty async HTTP client to the AWS CRT HTTP client. Note that the result may vary based on the application configuration such as Lambda function memory setting. The following charts show the Lambda cold start duration and max memory usage between the Netty async HTTP client (NettyNioAsyncHttpClient) and the AWS CRT HTTP client (AwsCrtAsyncHttpClient) from our tests. In our test code, we used a DynamoBD async SDK client to send a ListTables request.

AWS Lambda Cold Start Duration Comparison

AWS Lambda Max Memory Usage Comparison

Lower P90 request latency

In addition to cold-start improvement, we observed an improvement of up-to 9% in P90 latency when comparing the AWS CRT HTTP client with Netty async HTTP client.

The following chart compares the P90 latency between AWS CRT HTTP client and the Netty async HTTP client. The test application used an S3AsyncClient to send a getObject request to download a small S3 object.

P90 Latency Comparison

Enhanced connection management

The AWS CRT HTTP client has a connection health option that ensures “unhealthy” connections are not being reused. Based on your use-case, you can simply set a throughput threshold that determines whether a connection is healthy. If the connection falls below the set threshold for a configured amount of time, the AWS CRT HTTP client will treat the connection as unhealthy and close it, after the in-progress request finishes. If your application is hitting a network path that results in a dramatically slower throughput, this option will help the application recover by closing the slow connection and establishing a fresh connection for new requests.

In addition, the AWS CRT HTTP client offers enhanced DNS load balancing support. It has an asynchronous DNS resolver that polls each requested DNS address at a regular interval and balances the traffic across multiple endpoints, which allows for automatic fail-over in the unlikely event of a slow endpoint or server outage.

Getting Started

To use the AWS CRT HTTP client, first add the aws-crt-client dependency to your pom.xml. Search the Maven central repository for the most recent versions of the aws-crt-client artifact.

<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>aws-crt-client</artifactId>
    <version>${aws.sdk.version}</version>
</dependency>

Then, configure your application to use the AWS CRT HTTP client in one of the following ways.

Option 1 (preferred): Specify the CRT HTTP client through the client builder

This is the preferred option. It allows you to customize options such as max concurrency based on your use-case.

// Creating an asynchronous S3 client with a CRT HTTP client that is managed by the SDK
S3AsyncClient.builder()
             .httpClientBuilder(AwsCrtAsyncHttpClient.builder()
                                                     .maxConcurrency(100))
             .build();

The SDK by default includes netty-nio-client dependency. If you opt-in to the AWS CRT HTTP Client in your application, it’s recommended to remove netty-nio-client from your application to reduce the package size. Following is the sample POM file for an application that only has the CRT HTTP client in the classpath.

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>software.amazon.awssdk</groupId>
                <artifactId>bom</artifactId>
                <version>${aws.java.sdk.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
         <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>s3</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>software.amazon.awssdk</groupId>
                    <artifactId>netty-nio-client</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>aws-crt-client</artifactId>
        </dependency>
    </dependencies>

Option 2: Configure the HTTP client using a system property at JVM startup

The following option will create an AWS CRT HTTP client with the default configuration.

# Specify the default asynchronous HTTP client as AwsCrtAsyncHttpClient 
java -Dsoftware.amazon.awssdk.http.async.service.impl=\ 
software.amazon.awssdk.http.crt.AwsCrtSdkHttpService \ 
MyService.jar

Option 3: Configure the HTTP client using a system property in Java code.

Similar to option 2, the following option will create an AWS CRT HTTP client with the default configuration.

System.setProperty("software.amazon.awssdk.http.async.service.impl",
                   "software.amazon.awssdk.http.crt.AwsCrtSdkHttpService");

Limitation

In this release, HTTP/2 protocol is not yet supported in the AWS CRT HTTP Client. However, we are planning to implement it in the future. In the meantime, if you are using the service SDK clients that require HTTP/2 support such as KinesisAsyncClient and TranscribeStreamingAsyncClient, consider using NettyAsyncHttpClient instead.

Conclusion

In this blog post, we showed you how to get started with AWS CRT HTTP Client. To learn more about how to configure the client, visit our Developer Guide and API Reference. We would love your feedback on this HTTP client and as well as any other features you would like to see implemented in the future. Please let us know by opening a GitHub issue.

Zoe Wang

Zoe Wang

Zoe is a Senior Software Development Engineer working on the AWS SDK for Java. She is passionate about building tools to improve the developer experience. You can find her on GitHub @zoewangg.