Connection pooling in AWS across lambdas

一个人想着一个人 提交于 2020-01-24 03:44:13

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/51074990/connection-pooling-in-aws-across-lambdas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!