AWS Lambda Snapstart- The Quick & Easy Way To Get Started With Serverless Computing

Introduction

AWS Lambda SnapStart is a new feature introduced by AWS recently on Nov 29, 2022, to reduce the cold start times of Lambda. 

Lambda cold start times have been one of the limitations for many years that increase the latency, and this recent change with SnapStart will decrease the latency that minimizes cold starts.

This feature in Lambda is available only on Java 11 Corretto runtime.

Before diving deep into this, we will see how Lambda execution takes place and the phases of execution.

How Lambda execution takes place and the phases of execution

Lambda execution contains 2 phases, mainly Cold Start and Warm Start.

Overall, the Lambda function is a service running on a Virtual Machine managed by AWS; for the end user, this is considered Serverless.

Cold Start can be considered like bootstrapping, which will incur latency in making the system available. 

In the cold start, code is downloaded from the specified location, like the S3 bucket that the Lambda function executes, creates the execution environment and executes initialization code (like the init method in the Lambda function that has functionalities related to making database connections, or any other objects)

Once Lambda function cold start phase is completed, Lambda will be available to handle and process the requests. This phase is called the Warm phase. The actual execution of the Lambda function is mainly in the Warm phase, and the performance of the Lambda function is determined by the time of execution in the Warm Phase.

Because of Cold starts, Lambda function execution incurs latency in the actual request processing.

Scenarios where Lambda Cold starts occur

  1. When the Lambda function is deployed for the first time
  2. Concurrent request processing during Lambda function Scaling

Lambda execution environments handle one request at a time. After the invocation of the Lambda function has ended, the execution environment is retained for a period. If another request arrives within the period, the atmosphere is reused to handle the subsequent request. In this scenario, there is no Cold Start.

If requests arrive simultaneously or concurrently, the Lambda service scales up the Lambda function to provide multiple execution environments. Each environment setup experiences a full cold start.

Concurrent request processing during Lambda function Scaling

If there is a delay between the requests for each Lambda execution, the existing environments are reused if the previous invocation has been completed. Therefore, in this scenario also, there is no Cold Start.

  1. If the Lambda function is idle for more than 15 minutes (max time out period) (i.e.) no request is getting processed, the existing content of Lambda is destroyed, and after that, any new request invokes the Lambda function, which will incur a cold start.

Ways To Reduce Cold Starts

  1. Use Lambda Provisioned Concurrency: This feature keeps Lambda functions initialized and warm. Unlike on-demand Lambda functions, all setup activities happen ahead of invocation (including container start, bootstrap runtime and running the initialization code). Provisioned Concurrency adds cost to Lambda service.
  2. Reduce the init code execution time by utilizing some design patterns (for example, connection pool-like libraries)
  3. AWS Lambda SnapStart – New feature introduced recently for Java 11 Corretto runtime. This also will result in Lambda Provisioned Concurrency but with no cost.

How AWS Lambda SnapStart Works?

How AWS Lambda SnapStart works

When the Lambda function with SnapStart enabled is published, this will prepare the execution environment and initialization activity. A snapshot of the execution environment is created, encrypted and cached for a certain duration.

Now when the Lambda function is executed the first time or scaled for concurrent execution, there will not be a cold start, and the snapshot of the execution environment is restored from the cache.

In case of 14 days of inactivity on the Lambda function, the snapshots are removed from the cache.

Activation Of AWS Lambda SnapStart

  • Open the Functions page of the Lambda console.
  • Choose the name of a function.
  • Choose Configuration, and then choose General configuration.
  • On the General configuration pane, select Edit.
  • On the Edit basic settings page, for SnapStart, choose Published versions.

(This feature is currently available only in a few AWS regions, Java 11, and Corretto runtime)

(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))

  • Choose Save.
  • Publish a function version. Lambda initializes your code, creates a snapshot of the initialized execution environment, and then caches the snapshot for low-latency access.
  • Invoke the function version.

Note: AWS Lambda SnapStart will not work for unpublished versions.

Proof Of Concept

In this proof of concept, consider a Lambda function with Spring Boot code deployed in the Singapore region with Java 11 and Corretto runtime.

In the configuration section, edit SnapStart for published versions.

Editing SnapStart for published versions.

In the Versions section, publish the version.

Publish the version to create an execution environment in SnapStart

When the version is getting published, SnapStart creates an execution environment, and the snapshot is cached. 

Before the first invocation only, the Lambda execution environment is created.

Creating Lambda execution environment

Go to alias, create alias name, and create function URL for the alias

In this POC, load testing is done with the Apache bench tool for the below 2 use cases.

Case 1: Execute the unpublished version of Lambda (SnapStart not enabled) with 10 concurrent requests and a total of 100 requests. This will incur Cold Starts.

Execute the unpublished version of Lambda

Execute the unpublished version of Lambda

Case 2: Execute the published version of Lambda (SnapStart is enabled) with 10 concurrent requests and a total of 100 requests. This will not incur Cold Starts.

Execute the published version of Lambda

Execute the published version of Lambda

For 100% requests, Lambda with SnapStart has taken 1902 ms

For 100% requests, Lambda without SnapStart has taken 6249 ms

With SnapStart enabled, the latency is reduced to 70%. This proves that SnapStart is reusing the cached snapshot of the execution environment during Lambda invocations which are not leading to cold starts.

Conclusion

AWS Lambda is a powerful, flexible service for building and running serverless applications. With Lambda, you can easily scale your application to handle any workload without worrying about infrastructure or capacity planning.

You can use the AWS Lambda Snapstart to quickly and easily set up a serverless application with everything you need to get up and running in just a few clicks. The Snapstart includes a range of templates and resources that make it easy to build and deploy your application, and you can use the AWS Management Console or the AWS CLI to manage your functions and events.

Keep Reading

Keep Reading

Launch Faster with Low Cost: Master GTM with Pre-built Solutions in Our Webinar!

Register Today!
  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?