Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda - If I Start a Thread, what Happens when the Lambda is Frozen/Thawed?

I've got a Java-based AWS Lambda function that starts a background thread. The thread runs a scheduled task that reports metrics to an external service.

According to the Lambda docs, a Lambda function will be "frozen" for some period of time after it finishes executing, and can be "thawed" and re-used if the function is triggered again in short order.

My question is about what happens to background threads when this happens. Say, for example, that I started my background thread in the constructor of the class that contains my handler function. According to the docs, all variables outside of the scope of the handler function will remain initialized, so my thread should still exist, but will it still be running? Will the fact that my thread is running in the background prevent the Lambda from finishing execution in the first place? Or will it just exceed the five minute limit because it hasn't entirely finished, even though the handler function has completed?

like image 717
MusikPolice Avatar asked Sep 23 '16 18:09

MusikPolice


People also ask

How does Lambda handle failure during event processing?

Asynchronous invocation – Lambda retries function errors twice. If the function doesn't have enough capacity to handle all incoming requests, events might wait in the queue for hours or days to be sent to the function.

How do you stop a running Lambda that is stuck in a recursive loop?

In this event, you can press the “Throttle” button in the Lambda console to scale the function concurrency down to zero and break the recursion cycle.

What is a cold start Lambda?

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. initialize your function module.


1 Answers

This is addressed in the official AWS blog here:

But let’s say you have a background process running when the function finishes – what happens to it if the container is reused? In this case, 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). So in the reuse case, your background processes will still be there, but they won’t have been executing while you were away.

like image 85
Mark B Avatar answered Sep 17 '22 16:09

Mark B