Introduction

Lambda is a Serverless platform that runs on top of AWS Cloud infrastructure, a Function as a Service (FaaS) that allows developers to keep their focus on what they have to build, the entry point is a single function called handler which runs their source codes depending on certain events.

At the moment, the execution of the code encounters a problem referred to as cold start that adds a latency on top of certain executions which this article intends to improve by using one of the latest AWS updates, the SnapStart option.

Serverless Computing - AWS Lambda - Amazon Web Services
AWS Lambda is a serverless compute service for running code without having to provision or manage servers. You pay only for the compute time you consume.

SnapStart

SnapStart is a performance optimization option that helps to reduce the initialization time of Lambda functions at no additional costs.

It allows this by creating snapshots of functions at versions publishing time and then simply starting these up without having to go through the whole usual initialization process, those cached snapshots are deleted after 14 days of inactivity.

For now, the SnapStart option is launched and available for the Java (11) runtime (Coretto) in the following regions: US East (Ohio, N. Virginia), US West (Oregon), Asia Pacific (Singapore, Sydney, Tokyo), and Europe (Frankfurt, Ireland, Stockholm).

Performance Gain

OK! let's highlight the power of SnapStart by exemplifying using Java programming language with a Spring Boot powered demo available on GitHub.

No matter the framework used by your function you should expect SnapStart to work the same, Quarkus, Micronaut, or any other could do as well.

For that, we will invoke a version of our function without SnapStart and another with SnapStart and see the stats. Of course, the function in question will be powered by Java (11) runtime.

What is the AWS Serverless Application Model (AWS SAM)? - AWS Serverless Application Model
Describes how you can use the AWS Serverless Application Model (AWS SAM) to build serverless applications.

SAM is used to build and deploy the function with simple commands like:

  • sam build
  • sam deploy --guided: once deployed, it prints the API endpoint in the console that you can use for testing either on your browser or with curl.

More technical information are provided in the README file of the project.

First, let's highlight the execution of our function with SnapStart disabled, we have pieces of information related to the durations of our function:

Execution summary without SnapStart

To get the cold start duration, we will refer to the Init Duration. Meaning in this first execution, we have approximately 9s of initialization time.

Next, with SnapStart enabled the restoration occurs. For this scenario, in the template.yml file we added that option then we rebuilt and redeployed it in order to publish a fresh version of the function since SnapStart creates snapshots on published versions as per the screenshot below:

Enable SnapStart in SAM config file

Therefore, the execution summary looks like the following:

Execution summary with SnapStart

We have instead the Restore Duration in place of Init Duration, SnapStart has reduced the cold start duration by 95%, from 9s to 395 ms ✅.

———————

We have just started our journey to build a network of professionals to grow even more our free knowledge-sharing community that’ll give you a chance to learn interesting things about topics like cloud computing, software development, and software architectures while keeping the door open to more opportunities.

Does this speak to you? If YES, feel free to Join our Discord Server to stay in touch with the community and be part of independently organized events.

———————

Conclusion

In summary, SnapStart brings together a good benefit to our function during the first executions, even if it happens a few times it's still worth it.

The project source that we used is hosted on Github, Insofar as you want to make your own tests.

Thanks for reading this article, recommend and share if you enjoyed it. Follow us on Facebook, Twitter, LinkedIn for more content.