AWS 中跨 lambda 的连接池 [英] Connection pooling in AWS across lambdas

查看:36
本文介绍了AWS 中跨 lambda 的连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道 lambda 是按执行时间收费的.所以现在我想从 lambda 连接到 SQL Server DB.如果我在每个 lambda 中创建一个连接,对于 lambda 来说会很重.

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.

有什么最好的方法可以在一个地方保持我的 SQL 连接有效,并且能够在我的所有 lambda 表达式中使用.或者至少对于多次执行的一个 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.

我知道,lambdas 应该被视为无状态,但是,我仍然在为这个问题寻找更好的解决方案.我已经搜索了许多网站,但没有运气.我在 google 中找到的 AWS 帮助和参考较少.

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.

推荐答案

是的,有更好的解决方案.

Yes, there is a better solution.

基本上,您是对的,Lambda 应该被视为无状态.

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

然而,在 AWS Lambda 中有容器重用的概念.这意味着,如果您频繁调用 Lambda,则很可能会使用为您之前的请求提供服务的同一个容器来为您当前的请求提供服务.如果发生这种情况,那么您将获得先前的执行上下文,其中包括来自先前执行的所有声明和数据库连接(处理程序代码除外).

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.

以下是有关 AWS Lambda 容器重用的记录

Following is what is documented for AWS Lambda container reuse

执行 Lambda 函数后,AWS Lambda 会维护预期另一个 Lambda 的执行上下文一段时间函数调用.实际上,服务冻结了执行Lambda 函数完成后的上下文,并解冻上下文重用,如果 AWS Lambda 选择重用上下文,当 Lambda函数再次被调用.这种执行上下文重用方法具有以下含义:

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:

  • Lambda 函数代码中的任何声明(在处理程序代码之外,请参阅编程模型)保持初始化状态,提供额外的再次调用该函数时进行优化.例如,如果您的Lambda 函数建立数据库连接,而不是重新建立连接,原来的连接用于后续调用.我们建议在您的代码中添加逻辑以进行检查如果在创建连接之前存在连接.
  • 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.

有关详细信息,请查看此处

这篇关于AWS 中跨 lambda 的连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆