18
votes

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.

2
Thank you so much for the quick response @mjwills. Is it possible to maintain the connection alive for the same lambda for multiple executions?Learner

2 Answers

16
votes

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

3
votes

I don't know about how it would work with C# in lambda but I suspect that it should work the same way as node or somewhat similiar.

Each lambda function keeps the connection available for some time after it has been called, so if you can that lambda function again it reuses that connection but I had the same problem where every lambda function would be able to read and write to the database and that just cause the number of connections to jump up.

The way I handled connection pooling is by having a single lambda function that's responsible for connecting to the database and all the other lambda functions call this one to get what they want from the database and because that lambda reuses the connection it worked like a charm; I've attached a part of the design I used that might clarify what I wrote

enter image description here