Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How AWS Lambda container reuse works?

I have already gone through this link , (It was published in Dec-2014),Also referred this and this

How the AWS Lambda container reuse works as on May-2016, May you please share any specific link which tells in details ? Below I have few questions all around this AWS Lambda container reuse.

Consider a use case :

A Lambda function name "processMessageLambda" receives request when it has to process a message, and that message it receives from the POST REST API( from AWS API Gateway, as this lambda function connected with).

Now this 'processMessageLambda' process the message and store it to database.

So logically it does the following : connect to database, store the message and shutdown the connection. (It works fine in normal case.).

If requests arrive say - 10 per second , and each lambda function takes 30 seconds to execute then it actually opens many database connections.

Q1: May we use 'connection pooling' on this case(e.g. boneCP) ? as numbers of calls to "processMessageLambda" would be like hundred per second or Ten per second ?

(refer :simple example of container reuse - It works as it says, but what will happen many request would arrive say - 10 request per seconds )

Q-2: If its possible to use the connection pooling, then how this aws lambda container would be reUsed ?

If consider a case :

Lets consider that requests received by this Lambda function are Ten per second, on this case - 10 different container of this lambda function would be created or single container of lambda function would be created and that would be used with all these 10 requests ?

Q-3: If 10 different container of lambda function would be created then that means 10 database connections would be used , so those 10 container would be reUsed on further requests ?

Q-4: May you please explain from a developer point of view, that how it actually aws lambda container reuse works or how a developer think about it while reusing the aws lambda container ?

Q-5: If container reuse already in place,How a developer need to maintain the state of variables so a developer know which variable would be reused ?

like image 931
Sumit Arora Avatar asked May 30 '16 10:05

Sumit Arora


1 Answers

I build and maintain multiple serverless applications. One thing to remember which helps me is: two Lambda functions live in different universes. This immediately answer a few questions:

1: No, you can only do connection pooling per Lambda. If you fire 100 Lambda functions, you have 100 connections.

2: -

3: The exact algorithm behind container re-use is not specified. The next requests may use an existing container or a new container. There is no way to tell. Read this blog post for more info.

4: The best way imo is to think about it that containers are not re-used at all, with the added rule to always use unique (random) filenames in scratch space if you need it (the /tmp folder). When I tried to keep the connection open for re-use, the connection timed out and THEN got re-used. This resulted in database connection issues. Now I just open and close during each invocation.

5: Keep your code stateless (except modules, NEVER use global variables) and always use a unique (random) name if you need to store files in scratch space. These two general rules of thumb save me from lots of issues.

like image 136
Luc Hendriks Avatar answered Oct 15 '22 11:10

Luc Hendriks