AWS Developer Tools Blog

Introducing SDK Client Warm-Up feature in the AWS SDK for Java 2.x

The first service call from an application that uses the AWS SDK for Java 2.x takes longer than subsequent calls. This problem is known as cold start. The new SDK client warm-up feature reduces cold-start latency. One call to SdkWarmUp.warmUp() during application startup exercises the SDK request path before your first service call. With AWS Lambda SnapStart, the warm-up becomes part of the snapshot, so the first request after every restore benefits as well.

How it works

On the first service call, the JVM loads and initializes the SDK classes for the request path, then runs that code in the interpreter until the just-in-time (JIT) compiler compiles it to native code. Establishing the connection adds a DNS lookup, a TLS handshake, and certificate chain validation. The calls that follow reuse the loaded code and a pooled connection, so they complete faster.

SDK client warm-up loads that code during application startup. It warms both the service client and the HTTP client:

  • The service client executes a local-only operation with a pre-written response rather than a AWS endpoint. This loads the request marshalling and response unmarshalling code.
  • The HTTP client makes one network call to an AWS endpoint, which loads the connection setup code. Because the call is unsigned and invokes no service operation, it requires no AWS credentials or permissions and incurs no AWS charges.

Requirements

SDK client warm-up ships in Java SDK version 2.54.0 and later as part of the sdk-core module, so it is available to every service client without an additional dependency. For more information about project setup, see Set up an Apache Maven project.

The following pom.xml adds the Amazon Simple Storage Service (Amazon S3) module :

<dependencies>
  <dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>s3</artifactId>
    <version>2.54.0</version>
  </dependency>
</dependencies>

Warm every client on the classpath

SdkWarmUp.warmUp() warms every service client on your classpath, along with the HTTP clients that they use. Use this method when your application calls each of those clients.

Invoke warmUp() with no arguments to warm all the clients on your classpath:

import software.amazon.awssdk.core.warmup.SdkWarmUp;

SdkWarmUp.warmUp();

Warm specific clients

The SdkWarmUp.warmUp(Class<? extends SdkClient>... clients) overload warms only the clients you name, passed as SdkClient class objects. A synchronous client class warms the synchronous path (the service client and the synchronous HTTP clients), and an asynchronous client class warms the asynchronous path (the service client and the asynchronous HTTP clients).

If a dependency brings in service modules you don’t need, warmUp() warms those unused clients too, which adds to your startup time. To warm the clients you need and skip the rest, use the warmUp(Class...)overload.

The following example warms the synchronous service clients for Amazon S3 and Amazon DynamoDB:

import software.amazon.awssdk.core.warmup.SdkWarmUp;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;

SdkWarmUp.warmUp(S3Client.class, DynamoDbClient.class);

Warm-up applies to any application that starts a new JVM, not only to functions that use SnapStart. In a service that runs on Amazon EC2 or in a container, call SdkWarmUp.warmUp() during startup, before the instance registers with a load balancer or reports itself healthy. The SDK request path is then already initialized when the first request arrives.

Use warm-up with AWS Lambda SnapStart

AWS Lambda SnapStart reduces cold starts by taking a snapshot of your initialized function and restoring from it on later invocations. When the warm-up is part of that snapshot, every restore starts with warm clients.

Call SdkWarmUp.warmUp() in the constructor of your function handler class:

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import software.amazon.awssdk.core.warmup.SdkWarmUp;

public class MyHandler implements RequestHandler<String, String> {

    public MyHandler() {
        // Warm-up runs during initialization, before SnapStart takes the snapshot.
        SdkWarmUp.warmUp();
        // Your other initialization here.
    }

    @Override
    public String handleRequest(String input, Context context) {
        // Your handler logic here.
    }
}

For more information, see Lambda SnapStart and Java runtime hooks for SnapStart.

For more examples of SDK warmup, see the AWS SDK for Java 2.x Developer Guide.

Conclusion

In this post, I introduced SDK client warm-up, a new feature in the AWS SDK for Java 2.x that moves SDK initialization from your first request to startup. I showed you how to warm every client on your classpath with a single call, and how to use warm-up in both long-running services and functions that use AWS Lambda SnapStart.

To learn more, see the SDK client warm-up topic in the AWS SDK for Java 2.x Developer Guide. Try it out today and share your feedback by creating an issue in the aws-sdk-java-v2 GitHub repository.

John Viegas

John Viegas

John Viegas serves as a Software Development Engineer on the Java SDK team at Amazon Web Services (AWS). With a passion for enhancing the developer experience, he actively engages in projects and tools designed to elevate software development. For further insights into his contributions, feel free to explore his GitHub profile under the handle @joviegas.