Home > Blockchain >  Long timeout for AWS lambda function
Long timeout for AWS lambda function

Time:12-02

We have a lambda that interacts with a cloud-based application. We initially set the lambda timeout to 15 seconds, and it typically completes in < 10 seconds, so everything seemed fine.

Over time we’ve come to find there are circumstances in which API responses can be delayed, occasionally up to 20 seconds or more. We increased the timeout to 40 seconds, and that takes care of most cases. Yet there are still times when the response is delayed longer than that.

Question: is it ok to set a much longer timeout, like 90 seconds? Does that introduce any problems or risks?

CodePudding user response:

As already commented a main use of the timeout is to stop the lambda function, if it takes more than expected. This could happen due to an infinite loop or a software bug and in that case you want to terminate the function to stop wasting resources.

In practice this seldom happens. What happens regularly, is what you have experienced: The assumptions that you made, when you first designed your function aren't true or have changed.

You made an assumption that the API should reply back quickly, so that everything will finish in 10 seconds. This wasn't true and you've changed the timeout once. You find out that you need to change the timeout again. This should make you re-think your whole architecture. If a long delay is a rare occasion, you probably just need to increase the timeout once more and forget about it. If it isn't or you expect not to be in the future, then you have more problems than simply setting the timeout:

  • You waste computational resources, while doing nothing, but waiting for a reply. You are still getting charged for the time your function runs.
  • If it takes long for some cases, it means that it could happen that it will completely timeouts and an invocation fails. You therefore need a retry mechanism and a solid error reporting solution.

CodePudding user response:

On AWS lambda side, it is ok if you don't reach the max execution time of 900 seconds (15 minutes). See: http://docs.aws.amazon.com/lambda/latest/dg/limits.html

If your lambda is triggered by SQS, you have to take care of visibility timeout. The recommandation is to set the queue visibility timeout to 6X the timeout of your lambda function

Moreoever, you have to take care if other processes in your solution wait for the lambda results and if it is ok to delay them increasing the lambda timeout.

  • Related