Using a serverless architecture has several underlying benefits as we already have discussed in our post about AWS Lambda but, one unavoidable problem that arises with this service is that of cold start, which means that it takes a few seconds for the function to start running. Before we get into increasing the speed of AWS Lambda startup, first let’s look into why this problem even exists.

Why Does AWS Lamda Go Through Cold Starts? 

If your function is not currently running, AWS needs to deploy your code and spin up a new container before the request can begin. So, AWS has to find an EC2 instance and allocate a worker to initialize that further initializes your function. This process is a common occurrence but it lowers your applications’ productivity. While it may sound bad, cold starts are essential to the scalability of any serverless architecture.

How to Reduce AWS Lambda Cold Starts? 

1. Create Smaller Functions

Creating smaller functions can reduce the setup time required by the vendor and quicken the allocation of resources. Analyse your architecture and separate the concerns to avoid one big function slowing down the process. Follow the high cohesion and loose coupling principle to monitor and identify the functions or services that should be kept together or can be separated without any hindrance to your application functionality.

2. Preload Dependencies

Use handlers between invoking the service and function to load the dependencies before invocation. This would definitely help you reduce the total time taken by cold starts and should work for all applications.

3. Reduce Setup Variables

Static variables play a huge role in the setting up of a container. Increasing the number of static variables will increase the time taken in the preparation of a serverless container. Removing unnecessary static variables might be helpful in some cases and you will have to try it out yourself to see whether it works for your application.

4. Enable Provisional Concurrency

In 2019, AWS released provisioned concurrency for the lambda function to provide more control over the performance of a serverless application running on AWS. This feature keeps functions initialized and ready to respond in double-digit milliseconds. But since it’s another one of AWS services, it further costs more to use provisional concurrency. If you’re looking to deliver latency-sensitive microservices and the cost of failure to deliver exceeds the service cost, it can be one of the best ways to reduce cold starts.

5. Avoid HTTP/HTTPS Calls Inside Lambda

If not critical to your application, remove the HTTPS call. Whenever these HTTPS calls happen, security-related calls like SSL handshake also take place. Limited by CPU power, several calls paired resource allocation for lambda function can initiate a cold start.

6. Choose the Right Programming Languages

It is no secret that some languages are better for speed and resource management. While AWS has been improving its speed, the underlying language of your application can affect the time it takes to execute the lambda functions on EC2 instances. According to Nathan Malishev, a serverless expert, Node.js and Python are two of the best-suited languages that work well with Lambda functions whereas .NET, Go and Java still have some scope for improvement.

Many serverless experts have been working on solutions to avoid cold starts and based on their experiments, it looks promising. However, there is no one-size-fits-all kind of solution for a cold start. You need to analyze your requirements and then, carefully pick the one that suits you the best. If you require further guidance, our experts are always here to help you. Stanra Tech Solutions is a Select AWS Consulting Partner that can help with your business problems arising in cloud development. Connect with us or visit us on our website.