Effectively running Java on ServerlessChoice of HTTP Clients2 min

Prerequisites

  • Before moving on to HTTP clients, the first and foremost requirement to improve performance in Lambda functions is to upgrade from AWS SDK Java v1 to AWS SDK Java v2 as soon as possible. Version 2 of the AWS SDK was released in Nov 2018 with a lot of performance upgrades.

AWS SDK for Java offers various HTTP clients that are used by the SDK to connect to AWS Service APIs. This can be broadly categorized into two categories:

Sync Clients

For synchronous interactions with AWS services for lower latency, using synchronous service clients like S3Client or DynamoDBClient you can use either the ApacheHttpClient (default) or UrlConnectionHttpClient. It is generally recommended to use UrlConnectionHttpClient in Lambda because it is lightweight and minimizes jar size.

Async Clients

For async interactions with AWS services, you can use NettyNioAsyncHttpClient or AwsCrtAsyncHttpClient.

AwsCrtAsyncHttpClient is recommended for Lambda because it has quicker loading times and thus leading to faster startups. It also uses less memory as compared to NettyNioAsyncHttpClient. AwsCrtAsyncHttpClient improves latency by up-to 76%, provides up-to 14% reduction in memory usage and up-to 9% improvement in P90 latency. Thumbs up this GitHub issue to add support for sync client APIs to CRT Client.

CRT1

CRT2

One limitation of AwsCrtAsyncHttpClient is that it currently does not support HTTP/2 protocol. So if you are using the service SDK clients that require HTTP/2 support such as KinesisAsyncClient and TranscribeStreamingAsyncClient, consider using NettyAsyncHttpClient instead.

General guidance for choosing HTTP clients

HTTPClientDecisionTree

To learn more, visit this AWS Blog.


Created by:

Dhiraj Mahapatro
Dhiraj MahapatroPrincipal Specialist SA, Serverless, AWS.
Vipan Kumar
Vipan KumarSr. Solutions Architect