AWS Lambda SnapStart
AWS Lambda SnapStart is a new feature that was introduced by AWS on Nov 29, 2022 to reduce the cold start times of Lambda.
Join the DZone community and get the full member experience.
Join For FreeAWS Lambda SnapStart is a new feature that was introduced by AWS on Nov 29, 2022, to reduce the cold start times of Lambda.
Lambda cold start times has been one of the limitations for many years that increases the latency, and this recent change with SnapStart will decrease the latency that minimizes cold starts.
Currently, this feature in Lambda is available on Java 11 and Java 17 runtimes.
Before going deep dive into this, we will see how Lambda execution takes place and the phases of execution
Lambda execution contains two phases: cold start and warm start.
Overall, the Lambda function is a service running on a virtual machine managed by AWS. For an end user, this is considered serverless.
Cold start can be considered like bootstrapping the system, where this will incur latency in making the system available.
In the cold start, code is downloaded from the specified location like an S3 bucket that the Lambda function executes, creates an execution environment, and executes initialization code (like the init method in the Lambda function that has functionalities related to creating database connections or any other objects).
Once Lambda functions 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 processing of the request.
Scenarios where Lambda cold starts occur:
- When the Lambda function is deployed for the first time
- 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 environment 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.
In case there is a delay between the requests for each Lambda execution, the existing environments are reused if the previous invocation has been completed. In this scenario, there is no cold start, as well.
- If the Lambda function is idle for more than 15 minutes (max time out period) (i.e.) no request is getting processed, the existing container 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
- Use Lambda Provisioned Concurrency: This feature keeps Lambda functions initialized and warm. Unlike on-demand Lambda functions, this means that all setup activities happen ahead of invocation (including container start, bootstrap runtime, and running the initialization code). Provisioned concurrency adds cost to Lambda service.
- Reduce the init code execution time by utilizing some design patterns (for example, connection pool-like libraries).
- 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
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 the general configuration.
- On the general configuration pane, choose edit.
- On the edit basic settings page, for SnapStart, choose published versions.
- 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/17 and Corretto runtime.
In the configuration section, edit SnapStart for published versions:
In the versions section, publish the version.
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.
Go to alias, create alias name, and for the alias, create function URL.
In this POC, load testing is done with the Apache bench tool for below two use cases:
Case 1: Execute the unpublished version of Lambda (SnapStart not enabled) with 10 concurrent requests and total of 100 requests. This will incur cold starts:
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.
For 100% requests, Lambda with SnapStart has taken 1902 ms.
For 100% of 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.
Published at DZone with permission of Kiran Kumar. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments