Home > Net >  Aws lambda node and concurrency
Aws lambda node and concurrency

Time:07-20

I develop for first time on aws lambda with serverless

I know that my NodeJS code is not blocking so a NodeJS server can handle several requests simultaneously

My question : does Lambda create an instance for each call ? example if there are 10 simultaneous connections, will Lambda create 10 instances of 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

CodePudding user response:

Yes, this is a fundamental feature of AWS Lambda (and "serverless" functions in general). A new instance is created for each request.

CodePudding user response:

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. It is possible that subsequent invocations of the Lambda function reuse the context. That means there is a possibility of reusing some things, like DB connection in subsequent calls to the Lambda function.

There is no exact information about how long a Lambda function keeps the previous context alive. Also, in order to reuse things like DB connection, you must define and obtain a connection outside of your Handler function. If you put it in the handler function, it will certainly not be reused.

When the context is reused, you have something called a "warm" start. Lambda function is started quicker. 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)

  • Related