Use SnapStart to Improve Cold Start of Java Lambda Function

Use SnapStart to Improve Cold Start of Java Lambda Function

Use SnapStart to Improve Cold Start of Java Lambda Function

AWS lambdas written in java are infamous for their cold start time. I myself experienced it a few years back. Although you are not charged for the cold start or the time lambda takes to prepare your function, it results in an overall increase in application latency making it a genuine problem to solve.

I still remember the phase when my java lambdas(with spring boot framework) were giving latency of 15-20 seconds on cold start. We tried some of the optimization techniques and latency improved to a certain level. However, it was far from ideal in our case. And, we ended up rewriting all our Java lambdas to improve the cold start and overall execution time.

Today, when I saw the announcement of Lambda SnapStart, It brought back all the nostalgia of my struggle. I felt as if it was just for me 🙂

You might be wondering what’s Lambda SnapStart. And how can you use SnapStart to improve cold start of Java lambda function. Let’s get into it.

Don’t want to miss any posts from us? join us on our Facebook group, and follow us on Facebook, Twitter, LinkedIn, and Instagram. You can also subscribe to our newsletter below to not miss any updates from us.

Lambda SnapStart Overview

On 28th Nov 2022, AWS introduced Lambda SnapStart in AWS re:Invent. It’s a new performance optimization technique that improves the start-up time of your lambda function up to 10 times at no extra cost to you.

Sounds good?

Hell yeah 🙂

AWS lambdas using java were the worst hit by cold start. And I guess that’s the reason SnapStart is released first for Java 11(Corretto) only. It is a little disheartening to see that it only supports one runtime now. But I am sure, there is a lot more in line for this in future.

Also, I would have been happier if it supported Java 17 which is the baseline for spring boot 3.0 . But who knows, we might get it soon 🙂

How does Lambda SnapStart Work?

Once you enable SnapStart for your java lambda, publishing a new version of the function triggers an optimization process. It launches your function, lets it complete the init phase and then takes a snapshot of memory and disk state and caches it.

This means, with SnapStart on, when your lambda is called for the first time, it resumes new execution environments from the cached snapshot instead of initializing them from scratch. As a result, It improves the start-up time for your Lambda.

Let’s see how you can enable Lambda SnapStart for your Java Lambda. Mind it, It is only supported for Java 11 runtime so If you try to enable it for any other runtime, the dropdown is disabled as seen below.

Use SnapStart to Improve Cold Start of Java Lambda Function 6

Enable SnapStart for the Lambda

From the lambda console, click on your lambda function, and click on the Configuration tab.

This is what it looks like-

Use SnapStart to Improve Cold Start of Java Lambda Function 6

Click on Edit.

Use SnapStart to Improve Cold Start of Java Lambda Function 2

In the SnapStart section as seen below, choose the PublishedVerions option in the dropdown and click Save.

Once saved, when you publish a new lambda version, the optimization process gets started. As you can notice in the below screenshot, it says SnapStart adds a few minutes to the overall version creation time because it takes some time to optimize, prepare a snapshot and save it for later use.

Use SnapStart to Improve Cold Start of Java Lambda Function 4

Enable Lambda SnapStart using SAM or Serverless Application Model

Using SAM, you can do it like below-

TestFunc
  Type: AWS::Serverless::Function
  Properties:
    ...
    SnapStart:
      ApplyOn: PublishedVersions

Any noticed Improvement?

I tried creating a new lambda function using Java 11 and enabling SnapStart. For a barebone HelloWorld application, I didn’t find much of a difference in init time as you can notice in the below screenshots.

Before SnapStart

Lambda Cold Start

After SnapStart

After SnapStart Cold Start

As you can notice, I didn’t find much difference between the init duration(without SnapStart) and restore duration (with SnapStart). And the reason might be that I haven’t added any dependency to it or the code is extremely minimal with just HelloWorld.

Having said that, I have seen Jeff Barr’s blog and on deploying a fully-fledged app, he has seen that init without SnapStart was 6 seconds and with SnapStart it came to 142ms which is a huge huge improvement.

Do try it with your workload running in Java and let me know in the comment section on how much improvement you can see with SnapStart.

AWS Lambda SnapStart Supported Regions

At the time of writing this post, Lambda SnapStart is generally available in the following AWS Regions: US East (Ohio), US East (N. Virginia), US West (Oregon), Asia Pacific (Singapore), Asia Pacific (Tokyo), Asia Pacific (Sydney), Europe (Frankfurt), Europe (Ireland), and Europe (Stockholm). 

Conclusion:

In this post, we learnt how you can use the AWS lambda SnapStart feature to optimize the start-up time of java lambda at no extra cost.

  • It improves the performance of your lambda up to 10 times.
  • As of now, supports Java 11 only
  • It can be a great tool to improve the overall execution time of your lambda function using Java.

I hope you found this post helpful. Feel free to drop your questions in the comment section.

Enjoyed the content?

Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.

Don’t forget to motivate me by-

  • Adding a comment below on what you liked and what can be improved.
  • Follow us on
  • Share this post with your friends

Suggested Read:

Leave a Reply

Your email address will not be published. Required fields are marked *