#88: Optimizing Lambda cold starts with AJ Stuyvenberg
Nov 8, 2023
auto_awesome
AJ Stuyvenberg, an AWS Hero and staff engineer at Datadog, shares insights on optimizing Lambda cold starts. They discuss the gradual adoption of serverless technology, comparison of AWS services for serverless container deployment, running containers on AWS, Java's rise in serverless development, and strategies for optimizing Lambda cold starts.
Optimizing Lambda cold starts: mistakes to avoid, such as importing the entire AWS SDK
Using BUN and container image caching can significantly reduce cold start times for BUN-based Lambda functions
Monitoring and optimizing memory allocation can improve Lambda cold start times by minimizing swapping
Deep dives
Terraform and CDK are popular choices for infrastructure deployment
Terraform is widely used, especially in enterprises, due to its ability to handle both infrastructure and application development. CDK is gaining traction but is still relatively new compared to Serverless Framework. CloudFormation remains a popular choice as well.
Optimizing Lambda cold starts: common mistakes
Common mistakes in optimizing Lambda cold starts include: 1) Importing the entire AWS SDK instead of specific clients, resulting in slower initialization; 2) Overcomplicating deployment or packaging, such as relying too heavily on Lambda layers; 3) Swapping at initialization due to exceeding memory limits, leading to slower cold starts; 4) Including unused code in the Lambda function, contributing to longer initialization times; 5) Failing to split monolithic Lambda functions into smaller dedicated functions for specific routes, resulting in unnecessary code and slower initialization.
Cold start performance improvements with BUN and container images
AJ discusses the improvements in cold start performance achieved through the use of BUN, a new JavaScript runtime, and container images. By using BUN as a binary or compiling it with the runtime code in a container image, and taking advantage of container image caching strategies, cold start times for BUN-based Lambda functions can be significantly reduced. The caching mechanism in container images allows for a higher cache hit rate, resulting in faster cold starts.
Memory allocation and CPU optimization during initialization
During Lambda initialization, there is an uncapped VCPU, providing more CPU power than during regular function execution. However, excessive swapping of memory during initialization can negatively impact performance. Monitoring and optimizing memory allocation can improve cold start times, ensuring memory usage stays within limits and minimizing swapping.
The future of serverless tooling
While Terraform and CDK dominate serverless tooling, there is room for further innovation. There is a growing interest in new tools such as Wing Lang and renewed interest in Chalice. AJ also mentions the need for a tool that can compile CloudFormation but interpret it in its own native language, enabling greater flexibility and ease of use.
In this episode, I spoke with AJ Stuyvenberg, who is an AWS Hero and staff engineer at Datadog. We discussed the findings from the latest state of serverless report and spoke about Lambda cold starts at length. AJ has done some incredible research into Lambda cold starts and shared the 4 biggest mistakes people make regarding Lambda cold starts.
If you care about getting the best performance for your Lambda functions and minimizing cold starts, then this is the episode for you!
For more stories about real-world use of serverless technologies, please subscribe to the channel and follow me on X as @theburningmonk.
And if you're hungry for more insights, best practices, and invaluable tips on building serverless apps, make sure to subscribe to our free newsletter and elevate your serverless game! https://theburningmonk.com/subscribe