AWS Lambda Cold Start: What is it? And why should you care?
And some tips and trick to reduce it.
Zero-to-Hero with Serverless in 4 weeks —Workshop by Yan Cui
Join 20+ AWS Heroes & Community Builders and 1000+ happy students in leveling up your serverless game & becoming the serverless expert in your company. The workshop is taught by Yan Cui, AWS Serverless Hero and Consultant.
In this episode of The CloudHandbook, let’s deep dive into the AWS Lambda Cold starts. One of the issues we get when working with Lambda is that affects the latency of the system.
AWS Lambda is one of the most popular serverless computing services from AWS. It is widely used in production today and handles some of the most popular production workloads in the world.
For example, Netflix utilizes AWS lambda to handle its millions of concurrent views from its 260 million active users from all around the world.
There is no doubt that it is used in mission-critical workloads.
What is Lambda Cold start?
When your lambda function is triggered by any event, some steps happen behind the scenes:
first, it downloads your code
start a new execution environment
initialize code
run your lambda handler
Three things happen: prepare envs, init code, and shutdown envs.
During the first two steps, only the environment gets prepared, which is often called a cold start. In the diagram above, the red part is a cold start.
To run Lambda function, AWS doesn’t run any dedicated servers 24 x 7 to execute your lambda function.
In other words, a cold start occurs when AWS Lambda is not available to use because it is busy setting up the required environments.
It needs to boot up a new instance of lambda containers before it can execute your code which may affect the performance and latency.
Pricing model for AWS Lambda is pay only for what you used. In this case, the time required to bootup Lambda or Cold start time is not charged. You pay only for actual invocation duration.
Why do cold starts occur?
When the Lambda function is invoked for the first time, it prepares all the necessary environments as discussed above. When it gets invoked and successful, lambda retains the environment for a certain time (which is unknown) instead of destroying it.
During this time, if the new request do not arrive, the execution environment is shut down. For the new requests, lambda will have to instantiate new lambda instances. This is when a cold start occurs.
Let’s talk about some invocation patterns here where a cold start may occur.
When lambda gets four simultaneous requests as shown below. For each request, Lambda has to set up environments independently, so each Lambda experiences a cold start.
But if the requests are subsequent, only the couple of initial invocations may experience the cold start, the other invocation will utilize the existing warm environments.
How often do you experience cold start?
According to AWS, Based on an analysis of production Lambda workloads, cold starts typically occur in under 1% of invocations.
There are some best practices to reduce the cold.
Let’s discuss some of them here:
Best Practices to reduce Cold start time
1. Lambda SnapStart
At re:Invent 2022, AWS introduced Lambda SnapStart to address cold starts for Java runtime. This blog describes how you can reduce cold starts on Java: Reducing Java cold starts on AWS Lambda functions with SnapStart
2. Keep Functions Warm
Schedule regular executions of your Lambda functions using a cron job or by invoking them periodically with a CloudWatch Events rule. This keeps the function container warm and reduces the likelihood of cold starts.
3. Use Provisioned Concurrency
AWS Lambda Provisioned Concurrency allows you to pre-warm function instances to avoid cold starts. By provisioning a certain number of instances, you ensure that there's always a warm instance available to handle incoming requests.
4. Optimize Package Size
Smaller deployment packages tend to load faster, reducing cold start times. Remove unnecessary dependencies and code to keep the package size minimal. Use tools like AWS Lambda layers to separate reusable components and reduce the size of the deployment package.
5. Optimize Initialization
Minimize the initialization time of your Lambda function. Move any expensive initialization code outside the handler function, utilize global variables effectively, and consider using lazy initialization for resources that are not needed immediately.
6. Use Language Runtimes with Faster Startup
Different programming languages have different cold start behaviors. For example, languages like Node.js and Python typically have faster cold starts compared to Java or .NET. Consider the trade-offs between language features and cold start performance when choosing a runtime.
7. Tune Memory Allocation
Cold starts can be affected by the memory allocated to your Lambda function. Experiment with different memory settings to find the optimal balance between performance and cost. Higher memory settings can lead to faster initialization times.
8. Optimize Database Connections
If your Lambda function connects to a database, consider connection pooling to reduce the overhead of establishing new connections during cold starts. Reuse existing connections whenever possible to avoid the latency associated with creating new ones.
9. Parallelize Initialization
If your Lambda function performs multiple initialization tasks that can run concurrently, parallelize them to reduce overall initialization time. This can be particularly effective for functions with complex setup requirements.
10. Cache Data
Cache frequently accessed data or resources in memory to reduce the need for repeated initialization during cold starts. Just be mindful of the data's volatility and ensure that the cache doesn't grow indefinitely.
11. Monitor and Optimize
Continuously monitor your Lambda functions' performance, including cold start times, and optimize accordingly. Use AWS CloudWatch metrics and logs to identify trends and areas for improvement.
By implementing these best practices, you can minimize the impact of cold starts on your AWS Lambda functions and improve overall application performance.
Reference links:
How do language, memory and package size affect cold starts of AWS Lambda?
Reducing Java cold starts on AWS Lambda functions with SnapStart
Now over to you, how are you minimizing your cold starts? Share in the comment below.