简体   繁体   English

Aws lambda 节点和并发

[英]Aws lambda node and concurrency

I develop for first time on aws lambda with serverless我第一次使用无服务器在 aws lambda 上开发

I know that my NodeJS code is not blocking so a NodeJS server can handle several requests simultaneously我知道我的 NodeJS 代码没有阻塞,因此 NodeJS 服务器可以同时处理多个请求

My question : does Lambda create an instance for each call ?我的问题:Lambda 是否为每个调用创建一个实例? example if there are 10 simultaneous connections, will Lambda create 10 instances of NodeJS例如如果有 10 个同时连接,Lambda 将创建 10 个 NodeJS 实例

Currently, in my tests, I have the impression that lambda creates an instance for each call because at each call, my code creates a new connection to my database while locally my code keeps in memory the connection to my database目前,在我的测试中,我的印象是 lambda 会为每次调用创建一个实例,因为在每次调用时,我的代码都会创建一个到我的数据库的新连接,而在本地我的代码会在内存中保存到我的数据库的连接

Yes, this is a fundamental feature of AWS Lambda (and "serverless" functions in general).是的,这是 AWS Lambda 的一个基本特性(以及一般的“无服务器”函数)。 A new instance is created for each request.为每个请求创建一个新实例。

If you have multiple parallel executions, all will be separate instances (and this, each would use its own connection to the DB).如果您有多个并行执行,则所有实例都是单独的实例(并且每个实例都将使用自己的数据库连接)。

Now, if you are invoking multiple Lambda functions one after another, that's a bit different.现在,如果您一个接一个地调用多个 Lambda 函数,那就有点不同了。 It is possible that subsequent invocations of the Lambda function reuse the context. Lambda 函数的后续调用可能会重用上下文。 That means there is a possibility of reusing some things, like DB connection in subsequent calls to the Lambda function.这意味着有可能重用某些东西,例如在后续调用 Lambda 函数时的数据库连接。

There is no exact information about how long a Lambda function keeps the previous context alive.没有关于 Lambda 函数将先前的上下文保持多久的确切信息。 Also, in order to reuse things like DB connection, you must define and obtain a connection outside of your Handler function.此外,为了重用 DB 连接之类的东西,您必须在 Handler 函数之外定义和获取连接。 If you put it in the handler function, it will certainly not be reused.如果放在handler函数中,肯定不会被重用。

When the context is reused, you have something called a "warm" start.当上下文被重用时,你就有了一种叫做“热”启动的东西。 Lambda function is started quicker. Lambda 函数启动更快。 If some time has passed and the context cannot be reused anymore, you have a "cold" start, meaning the Lambda function will take more time to start its execution (it needs to pull all the dependencies when doing the cold start)如果一段时间过去了,上下文不能再被重用,你有一个“冷”启动,这意味着 Lambda 函数将需要更多时间来开始执行(它需要在冷启动时拉取所有依赖项)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM