Understanding AWS Lambda cold start

How to Avoid Lambda Cold Starts

A “cold start” is the 1st request that a new Lambda worker handles. This request takes longer to process because the Lambda service needs to: find a space in its EC2 fleet to allocate the worker. initialize the worker.

According to an analysis of production Lambda workloads, cold starts typically occur in under 1% of invocations. The duration of a cold start varies from under 100 ms to over 1 second.26-Apr-2021

The main factors driving cold start latency are: Memory size: the more memory you allocate to your function, the faster it will start up; Runtime: usually scripting languages (Python, Ruby, Javascript) perform a lot better in startup time in comparison to compiled runtimes.

Using Lambda Pings and Provision Concurrency to prevent Cold Start.

AWS Lambda is one of the most used AWS services around the world. Its automatic scaling behavior and cost-effective on-demand invoking mechanism have made it even more popular.

However, Lambda functions take significant time to get started initially, and this directly impacts application performance.

This behavior is known as cold starts, and in this article, I will discuss 2 methods to overcome this problem, and you can choose one based on your requirements and cost.

1. Using Lambda Ping

The first method to prevent cold-starts is using a CloudWatch event rule and scheduled event to ping the Lambda function regularly.

Usually, the Lambda execution environment has about 10 to 15 minutes of idle time, and if there were no Lambdas invoked during that time, the execution environment is removed.

With this approach, we can ping the Lambda function every 5 minutes or so and keep the environment hot and running.

But, this solution is only viable for a limited time since AWS Lambda removes the execution environments every 1 hour or 2 hour.

Using Lambda Pings with Serverless

Now, let's see how we can implement Lambda Pings using a serverless application.

Once you created the serverless application, you just need to do a small configuration in serverless.yml file.

service: lambdaColdStartsprovider:  
  name: aws   
  runtime: nodejs12.xcustom:  
  warmup:  
    enabled: true  
    events:  
      -schedule: ‘corn(0/5 8–17 ? \* MON-FRI \*)’functions:  
  hello:  
    handler: handler.hello  
    events:  
      -http:  
         path: data  
         method: getplugins:  
  -serverless-plugin-warmup

In this example, I have used an NPM library called serverless-plugin-warmup to schedule the Lambda pings and configured it under the custom section.

Under the custom events section, I have defined a cron job to be executed every 5 minutes from Monday to Friday between 8:00 am and 5:55 pm to ping the Lambda regularly.

Cost Calculation

Similar to any other AWS service, Lambda cost is also calculated based on its use.

Lambda with a 4GB memory will cost 0.0000667 USD per second.

Now, I will show you how much it will cost to use the Lambda ping solution for a month.

Lambda Memory = 4GB  
Lambda Duration Cost for 4GB Memory = 0.0000667 USD per second  
Ping Interval = Every 5 MinutesNumber of times Lambda needs to be invoked =(24\*60)/5 = 288 times per day.Cost per day = 288\*0.0000667 USD = 0.0192096 USD  
Cost per month =30\*0.0192096 USD = 0.57 USDTotal Cost for a 1 Lambda = 0.57 USD  
Total Cost for 100 Lambdas = 57 USD

If we planing to use the Lambda ping solution to run 100 Lambdas with 4GB memory for a month, it will cost 57 USD.

2. Using Lambda Provision Concurrency

The second solution is to use a Lambda provision concurrency to create pre-warmed execution environments.

After defining a pre wormed environment, the environment will download the Lambda code with dependencies and initialize code. You can define any number of pre wormed environments based on your requirement.

Whenever there is a request from the API gateway, the Lambda service will avoid provisioning execution environments and use one of the pre-warmed environments.

Implementing this solution is easier than implementing Lambda pings. You just need to set the provisionedConcurrency attribute under your function.

Using Provision Concurrency with Serverless

In the below example, I have provisioned 5 pre wormed execution environments for hello Lambda.

service: lambdaColdStartsprovider:  
  name: aws   
  runtime: nodejs12.xfunctions:  
  hello:  
    handler: handler.hello  
    events:  
      -http:  
         path: data  
         method: get  
    provisionedConcurrency: 10

However, ensure you only apply provision concurrency for most needed Lambda functions since provision concurrency costs are high.

Cost Calculation

Duration cost in Lambda when provision concurrency is enabled is low than the normal Lambda duration cost.

It only costs around 0.0000097222 USD for a Lambda with 1GB memory per second. But this doesn't reduce the overall cost of this approach method since there is an additional cost for Provisioned Concurrency.

Let’s calculate how much it cost to run a Lambda with Provision Concurrency for a month.

Lambda Memory = 4GB  
Lambda Duration Cost for 4GB Memory = 0.0000388888 USD per secondInvoked on Every 5 Minutes  
Number of times Lambda needs to be invoked =(24\*60)/5 = 288 times per day.Cost per day = 288\*0.0000388888 USD = 0.0112 USD  
Cost per month = 30\*0.0112 USD = 0.336 USDTotal Cost for a 1 Lambda = 0.336 USD  
Total Cost for 100 Lambdas = 33.6 USD

As you can see, the duration cost is much less than the Lambda ping approach. But, that’s not all. We need to consider the provisioned capacity cost.

Provision capacity cost is charged as soon as you active provision concurrency and it will keep charging until you stop it.

For this example, Lambdas will run for a duration of a month, and let's see what the final cost is.

Lambda Duration Cost for 100 Lambdas per Month = 33.6 USDProvision Concurrency Cost for 4GB Memory = 0.0000166668 USD per second  
Provision Capacity Duration for a Month = 30\*24\*60\*60 = 2592000 secondsCost per Lambda Provision Capacity = 2592000\*0.0000166668 = 43.2003456 USDProvison Capacity Cost for 100 Lambdas per Month = 43.2003456\*100 = 4320.03 USDTotal Cost = 4353.63 USD

Ultimately it will cost more than 4000 USD per month to run 100 Lambdas with provision concurrency.

Conclusion

As a developer, you should always be mindful when using on-demand services to minimize the cost. So, I advise you to use these approaches when necessary and always follow the best practices.

For example, you should only apply provision concurrencies to most needed Lambdas since its cost is high. Also, you can use optimize cron jobs to minimize the cost with proper scheduling.

In addition to that, you can always use tools like lambda-power-tuning to find the best memory allocation to your Lambda and use AWS Pricing Calculator to find the perfect budget.

Thank you for Reading!!!

Comments