Companies switch to cloud services like AWS for reliability and scalability. Eventually, every organization switch to serverless architecture and experience the notorious cold start problems. Although there is no way around a cold start for AWS Lambda functions, certain tips and tricks could definitely reduce it significantly. In this blog, we will discuss why cold start actually happens and look out for potential ways to prevent this scenario.
Whenever you switch to serverless architecture, you need to deal with AWS Lambda and startup latency due to cold starts. A cold start is a common phenomenon that occurs when a function is called for the first time after deployment. This happens solely because the container takes time to form. The delay caused due to a cold start can hamper the application’s productivity. Apart from this usual scenario, programming languages with a higher number of dependencies associated with lambda functions also contribute to the latency of the cold start. For example - whenever, you call a lambda function, all the dependencies are imported into the container for lambda functions which further adds to latency. However, there are ways by which we can reduce the latency of AWS Lambda cold start. Let’s look at a few of them.
When an HTTP call happens in serverless applications, SSL handshake and other security-related calls occur simultaneously. These calls initiate a cold start in AWS Lambda.
To curb this menace, AWS SDK always ships with HTTP client libraries from which one can make SDK call. All the SDK libraries are capable of connection pooling. On contrary, Lambda functions can only serve one request at a time. This renders HTTP connections useless. Using the HTTP client available in AWS SDK can reduce the cold start latency.
If you import dependencies post invoking functions, it will likely contribute towards latency leading to cold starts. Preloading dependencies is one of the simple yet effective solutions to dodge the cold start situation by a fair margin. The use of handlers is beneficial for invoking service and functions to load dependencies.
In serverless architecture, large functions automatically translate to more setup time. Although the serverless architecture breaks the monolithic structure into granular services, one needs to analyze the architecture to find out how to separate the concerns.
Every serverless architecture is unique and has its own requirements. You need to continuously monitor them to identify whether the functions/services could be kept together or must be separated. Software architects approach this with the "high cohesion loose coupling" principle.
Preparing of serverless container consists setting up of static variables. However, the setting of static variables and their associated component takes a while. The more the number of static variables, the more the cold start latency will be. Although it might not eliminate completely, removing unnecessary static variables significantly lowers down the cold start latency.
An experimental technique is administered by Nathan Mashilev, a software engineer. He ran two tests, one with and another without VPC to determine the time taken to a cold start. The results were as follows
The one with VPC had a higher latency than the one without VPC
There is a massive difference of 8.83 seconds by running lambda functions outside of VPC.
Goncalo Neves, Product Engineer at Fidel faced a series of cold starts when building a payment solution. To solve this challenge, he created a serverless plugin warmup that warmed the lambda functions at specific intervals keeping the container active. The plugin can be easily installed via NPM (node package manager)
If you are into the serverless ecosystem, you need to deal with lambda functions and the cold starts associated with it. However, many serverless experts have found some unique solutions to the problems. You just need to analyze the solutions and pick the ones that best suit your organizational needs. For a more detailed understanding of lambda cold starts, visit our updated article on How to Avoid AWS Lambda Cold Starts? If you have any suggestions, feel free to mention them in the comments section below.