How do I reduce initialization and invocation duration latency for my Java Lambda function?
4 minute read
My Java AWS Lambda function isn’t performing optimally. Why is this happening, and how do I reduce my Java function’s initialization and duration latency?
There are two main factors that can contribute to high latency in a Java Lambda function:
Java virtual machine (JVM) lazy class loading
To reduce memory usage, the JVM delays initializing a Java class library until the library is first called in an application. This delay can cause a high number of input-output (I/O) operations, which results in higher duration latencies for first invocations in a Lambda execution environment.
The Java Reflection API
The Java Reflection API allows Java code to discover information about other classes, interfaces, fields, and methods, and then operate on their underlying values. Because reflection involves types that are dynamically resolved, certain JVM optimizations can’t be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts.
To optimize your Java Lambda function’s performance, you can implement one or more of the best practices outlined in this article.
Note: Configuring provisioned concurrency incurs charges to your AWS account. You can configure provisioned concurrency on a version of a function or on a Lambda function alias.
Initialize your function's static logic outside of the function handler
When you initialize a Lambda function, Lambda allocates a burst of host CPU capacity for up to 10 seconds. Because of this CPU burst, it’s a best practice to do the following outside of the function handler:
Import libraries and dependencies
Set up configuration
Initialize connections to other services
This static initialization allows these resources to be initialized once per sandbox and then reused for all future invocations in the execution environment.
Make API calls to any lazily loaded libraries outside of the function handler
To avoid lazy loading of libraries during initialization, you can make dummy API calls to any lazily loaded libraries outside of the function handler. These dummy calls initialize the libraries and prewarm the SDK that you're using.
Note: Dummy calls to lazily loaded libraries can fail if Lambda doesn't have the information required for a successful API call. If the call fails, make sure that you catch the error.
Reduce reflective operations in your Lambda code
Reflective operations have slower performance than their non-reflective counterparts. Avoid reflective operations in sections of code that are called frequently.
Reduce the number of classes in your application
Reducing the size of your deployment package to its runtime necessities reduces the amount of time that it takes to invoke your function.
Set your Lambda function’s configuration parameters in advance
Setting your function’s configuration parameters in advance reduces your function’s invocation and duration latency. If you don’t specify configuration variables in your code, it takes Lambda more time to discover the default variables for your function.