26

There are a few pieces of my app that cannot afford the additional 1-2 second delay caused by the "freeze-thaw" cycle that Lambda functions go through when they're new or unused for some period of time.

How can I keep these Lambda functions warm so AWS doesn't have to re-provision them all the time? This goes for both 1) infrequently-used functions and 2) recently-deployed functions.

Ideally, there is a setting that I missed called "keep warm" that increases the cost of the Lambda functions, but always keeps a function warm and ready to respond, but I'm pretty sure this does not exist.

I suppose an option is to use a CloudWatch timer to ping the functions every so often... but this feels wrong to me. Also, I don't know the interval that AWS uses to put Lambda functions on ice.

10
  • 5
    Using something like a CloudWatch timer to ping the function every so often is the only way to accomplish what you want at this time.
    – Mark B
    Mar 18, 2017 at 18:32
  • Thanks @MarkB Do you have a sense of the ideal interval? I can't seem to find any concrete numbers in the Lambda documentation. Are we talking 1 minute pings? 1 hour pings?
    – samcorcos
    Mar 18, 2017 at 18:43
  • 4
    Anecdotally, I think you want something in the 5 to 15 minute range. Declare a global variable outside the handler, then inside the handler, set that variable to the value from context.awsRequestId only if it's not already set... then log the value of the variable. This gives you essentially a unique container ID that you can use to track container reuse and determine the effectiveness of your strategy. Store the time in a similar variable, and an incrementing counter, and you get a good perspective. Mar 18, 2017 at 18:55
  • @Michael-sqlbot I ran some tests (see link) and it looks like the interval doesn't matter all that much... Thoughts? medium.com/@SamCorcos/…
    – samcorcos
    Mar 21, 2017 at 0:23
  • 2
    Nice post! Note, though, that I believe you're misapplying the "freeze/thaw" concept. The freeze/thaw cycle is what's preserving the variables. It's when you're using a newly launched container -- not a thawed one that was previously frozen -- that you see the delay: 'Lambda will actually “freeze” the process and thaw it out the next time you call the function (but only if the container is reused, which isn’t a guarantee).' The long spin-up times are when there's not a frozen conainer available to be thawed and reused. Mar 21, 2017 at 1:37

2 Answers 2

10

UPDATE DEC 2019

AWS now also offers 'Provisioned Concurrency'. https://aws.amazon.com/blogs/aws/new-provisioned-concurrency-for-lambda-functions/

Basically you pay around 10$/month (for a 1GB Lambda) per instance that you want to keep 'warm'.

7

BBC has published a nice article on iPlayer engineering where they describe a similar issue.

They have chosen to call the function every few minutes using CloudWatch Scheduled Events.

So in theory, it should just stay there, except it might not. So we have set up a scheduled event to keep the container ‘warm’. We invoke the function every couple of minutes not to do any processing, but to make sure we’ve got the model ready. This accounts for a very small percentage of invocations but helps us mitigate race conditions in the model download.(We also limit artificially how many lambdas we invoke in parallel as an additional measure).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.