Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection pooling in AWS across lambdas

We know lambdas are charged by the execution time. So now I want to connect to SQL Server DB from lambda. If I create a connection in each lambda, it would be heavy for lambda.

Is there any best way to maintain my SQL connections alive in one place and will be able to use across my all lambdas. Or at least alive for one lambda for multiple executions.

I know, lambdas should be treated like stateless, but still, I am looking for a better solution for this issue. I have searched over many sites but no luck. I am finding less help and references for AWS in google.

like image 909
Learner Avatar asked Jun 28 '18 04:06

Learner


People also ask

How do you share data between lambdas?

There is no in-built technique for sharing data between Lambda functions. Each function runs independently and there is no shared datastore. You will need to use an external datastore -- that is, something outside of Lambda that can persist the data.

How does Lambda concurrency work?

Lambda concurrency consists of the number of requests a certain function serves during any given time. Once a function is invoked, Lambda uses an instance of the function in order to process an event. After the function code stops running, it can start handling another request.

Can Lambda talk to database?

Yes. AWS Lambda can connect to an AWS hosted databases such as RDS or DynamoDB. AWS Lambda can also connect to external databases which are public or grant network access.


1 Answers

Yes, there is a better solution.

Basically, you are right, Lambdas should be treated as stateless.

However, in AWS Lambda there is concept of container reuse. Which means that if you invoke Lambdas on a frequent basis then it is highly possible that the same container which served your previous request will be used for serving your current request. And if that happens, then you get the previous Execution Context which includes all declarations and database connections (except the handler code) as is from the previous execution.

Following is what is documented for AWS Lambda container reuse

After a Lambda function is executed, AWS Lambda maintains the Execution Context for some time in anticipation of another Lambda function invocation. In effect, the service freezes the Execution Context after a Lambda function completes, and thaws the context for reuse, if AWS Lambda chooses to reuse the context when the Lambda function is invoked again. This Execution Context reuse approach has the following implications:

  • Any declarations in your Lambda function code (outside the handler code, see Programming Model) remains initialized, providing additional optimization when the function is invoked again. For example, if your Lambda function establishes a database connection, instead of reestablishing the connection, the original connection is used in subsequent invocations. We suggest adding logic in your code to check if a connection exists before creating one.

For more details check here

like image 154
Arafat Nalkhande Avatar answered Sep 21 '22 07:09

Arafat Nalkhande