5 Ways to Prevent AWS Lambda Cold Starts
AWS Lambda provides many benefits for developers, including scalability, flexibility, faster release times, and reduced cost. However, also comes with limitations such as cold starts. Cold starts can increase the latency of serverless applications. Read on to discover some tips to improve the performance of AWS Lambda.
How Does AWS Lambda Work?
Lambda functions run on their own container. When you create a new function, Lambda packages it into a new container. This container is then executed on a multi-tenant cluster of managed machines.
Before the functions start running, each container is allocated its necessary CPU and RAM capacity. When a function finishes running, the allocated RAM is multiplied by the amount of time the function spent running. AWS charges customers based on the allocated memory and the amount of function run time.
AWS Lambda can simultaneously execute many instances of the same function, or of different functions from the same AWS account. This makes Lambda suitable for deploying highly scalable cloud computing solutions.
What Is an AWS Lambda Cold Start?
When running a Lambda function, it stays active as long as you’re running it. It means that your container stays alive, and ready for execution. AWS can drop the container after a period of inactivity, and your function becomes inactive or cold. A cold start happens when you execute an inactive Lambda function. The execution of an inactive Lamda function happens when there are no available containers, and the function needs to start up a new one. A worm start happens when there are available containers.
Spinning up new containers in a cold start creates a delay. That’s why cold starts make serverless applications respond slower.
5 Ways to Reduce the Impact of Lambda Cold Starts
You cannot entirely avoid cold starts, but you can reduce their duration and frequency by using the following tips. to of cold starts:
- Prefer dynamically typed languages—use languages like Node.js or Python instead of statically typed programming languages like C# and Java. Dynamically typed languages check what you type during run-time as opposed to compile-time in statically typed languages.
- Avoid using Lambdas in Virtual Private Cloud (VPC)—a VPC is an isolated, secure, private cloud hosted on a public cloud. VPC isolates your computing resources from each other, which can increase the delay time and cause cold starts.
- Avoid HTTPS calls inside your lambda—SSL handshake and other security-related calls can create cold starts since they are limited by CPU power.
- Avoid dependencies—Java dependencies that scan classpath like Spring can cause cold starts. In addition, loading Java classes can take some time and may lead to a cold start.
- Increase the memory on AWS Lambda to get more CPU capacity—this can make the execution time of Lambda faster, and also reduce costs compared to lower memory settings.
How to Optimize AWS Lambda Performance
There is more to Lambda performance than cold starts, here are some tips you can use to improve performance.
Optimize memory size
Lambda enables you to increase or decrease the amount of RAM memory allocated to your function. RAM memory size impacts the amount of network bandwidth and CPU time your function gets.
Choosing the smallest possible amount of RAM can add latency to your application. It can also make your application more expensive if the added latency exceeds the amount of RAM cost saving. Therefore, you should find the ideal trade-off between the price and the performance of your application by testing your Lambda function at each available resource.
Optimize language runtime
Compiled languages such as Java and C#, have a slow initial startup time. Interpreted languages like Python and Node.js have much faster initial start-up times. The language you choose can affect the performance of your application.
AWS Lambda supports both compiled and interpreted languages. If your application is sensitive to spikes and infrequent traffic or requires low latency, you should use an interpreted language. If your application does not experience spikes in traffic and you know what your traffic looks like, you can use any language you want.
Monitoring of AWS Lambda gives you the information you need to identify issues that can negatively impact performance. You can use AWS X-Ray to monitor your application at the component level. X-Ray enables you to trace the application lifecycle throughout the entire execution. As a result, you can look deeper at the components of the Lambda function, and view metrics like latency.
There are many ways to reduce the impact of cold starts. You can use dynamically typed languages, avoid dependencies, or avoid HTTPS calls inside your lambda. However, you cannot eliminate them completely. You can improve the overall Lambda performance by optimizing memory size, monitor performance, and optimizing language runtime.