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

查看:372
本文介绍了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.

我知道,应该将lambda视为无状态,但我仍然在寻找一个更好的解决方案。我搜索了许多站点,但没有运气。我在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.

基本上,没错,Lambdas应该被视为无状态。

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
函数调用。实际上,如果AWS Lambda在再次调用Lambda
函数时选择重用上下文,则服务会在Lambda函数完成后冻结Execution
上下文,并解冻
重用上下文。这种执行上下文重用方法具有
的以下含义:

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.

有关更多详细信息,请此处

For more details check here

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

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