Unleashing Serverless Magic with Lambda SnapStart

Jigyasa
4 min readJun 24, 2024

--

What is Lambda SnapStart?

With new innovations, AWS SnapStart makes your Java-based function cold starts up to 10x faster for latency-sensitive Java applications at no extra cost, and with minimal or no code changes.

Understanding performance:

When AWS Lambda runs your application, it reports multiple values such as duration and init duration to Amazon CloudWatch Logs . The init duration includes JVM startup time & class initialization phases such as handler class constructors and static variables, whereas the duration is the time of the actual handler method being executed:

Consider an example call of the function where init duration of 5000 ms and a duration of 900 ms.

Therefore, the total function duration for the first call is the sum of both values (5000 ms+ 900 ms = 5900 ms).

First request will take 5900 ms to execute.

Case 1: Execution environment is available . Second request will take 900 millisec.

Case 2: Execution environment is not available . Second request will take 5900 millisec.

Tested results

Execution environments can only handle a single request at a time. Therefore, when your application receives multiple concurrent requests (at the same time), AWS Lambda has to create additional execution environments. This again will trigger the initialization phase and creates new environment.

To address this AWS has provided SnapStart feature:

Instead of running the function initialization phase on every cold-start, the function initialization will now already be run at deployment time by creating a new function version . Lambda will then take a snapshot of the initialized execution environment. This snapshot will be encrypted and persisted in a tiered cache for low latency access.

Later, when the function is invoked and scaled, Lambda resumes the execution environment from the persisted snapshot instead of initializing from scratch. This results in a lower startup latency.

How SnapStart Works

  1. Initialization: When you deploy your Lambda function, SnapStart initializes the execution environment and captures a snapshot of it.
  2. Snapshot Creation: This snapshot includes the runtime, the initialized state of the function code, and the dependencies.
  3. Fast Startup: When the function is invoked, Lambda uses the snapshot to quickly initialize the function, significantly reducing the cold start time.

By reducing the time spent initializing the function, SnapStart can potentially lower the overall cost of running your serverless applications.

Updating a snapshot:

Lambda creates a snapshot for each published function version. To update a snapshot, publish a new function version. Lambda automatically updates your snapshots with the latest runtime and security patches.

Best Practices

  • Benchmark Before and After: Measure your function’s cold start time before and after enabling SnapStart to quantify the improvement.
  • Enable for Latency-Sensitive Functions: Prioritize enabling SnapStart for functions that are latency-sensitive and require quick responses.
  • Monitor and Adjust: Continuously monitor your functions using CloudWatch to ensure they are performing optimally with SnapStart enabled.

Managing Temporary Data in AWS Lambda

Lambda functions often need to initialize ephemeral data during the initialization phase. This data can include temporary credentials, cached timestamps, or other transient information. It’s essential to refresh this ephemeral data in the function handler before using it, even when SnapStart is not being used.

What if we need to perform some steps before starting snapshot ?

You can use runtime hooks to implement code before Lambda creates a snapshot or after Lambda resumes a function from a snapshot. Runtime hooks are available as part of the open-source Coordinated Restore at Checkpoint (CRaC) project (part of OpenJDK).

How about security ?

Lambda SnapStart supports encryption at rest. Lambda encrypts snapshots with an AWS KMS key. By default, Lambda uses an AWS managed key.

When you delete a SnapStart function or function version, all Invoke requests to that function or function version fail. Lambda automatically deletes snapshots that are not invoked for 14 days. Lambda removes all resources associated with deleted snapshots in compliance with the General Data Protection Regulation (GDPR).

AWS Lambda SnapStart is a valuable feature for reducing cold start latency and enhancing the performance of serverless applications.

By pre-warming the execution environment, SnapStart ensures that your functions start quickly and respond faster to incoming requests. Additionally, properly managing temporary data in your Lambda functions is crucial for maintaining reliability and performance. By following the best practices you can optimize your serverless applications for both speed and efficiency.

--

--