Does lambda function implement the single instance concurrency model?
Hi, we're investigating the way to migrate our traditional model backend apps to AWS lambda service. After searching around as well as reading the docs from lambda official website, I can tell that lambda uses a thread-safe, single instance for single request way for the concurrency model, am i right?
The question is: For a programming model like nodejs, which leverages event pool to max the ability to process the IO-intense works, can we specify the single instance with multiple requests concurrency model to mitigate the global concurrency limit and cold start problem? and i think this way could also save cost for saving the IO waiting time.
I see there are ways to use reserved/provisioned instances though,I think it could be better to support more fine-grained concurrency methods.
At first, YES, you're right, you should think about your lambda function as "single request = single thread-safe stateless lambda". Such paradigm of thinking force us, engineers, to detach compute from data (state), scale each of it independently, avoid shared state, side effects, and at the end – reach high level of parallelism avoiding hard-to-debug pitfals of parallel programming.
Regarding the second half of your question: the real beauty of the AWS Lambda is that it allows to move away from thinking about low-level concepts as CPUs utilization and IO-waiting time, and focus only only on the "business logic" and what exactly you want to achieve with your code. (and of course ,internally, AWS Lambda doing quite extensive under-the-hood optimization to avoid wasting of resources)
So technically you can, run own event loop inside single lambda call and handle multiple request within each lambda call. However I would call it an anti-pattern and a maybe sign, that you might not need Lambda here at all.
I would recommend simply to give it a try, and don't think too much about underneath resources optimization. Also, if I am not mistaken, the default lambda reserved concurrency is 1000, so should be enough to experiment with high traffic.
Sure, thanks for your quick reply! I'll definitely give it a try and update here when I have more thoughts.
Lambda functions run in run-time environments, each instance in its own environment. Each instance can handle a single request at a time. There is no way to route multiple requests to a single instance at the same time.
Saying that, there are event sources that support batching, e.g., SQS, Kinesis Data Streams, etc. For those event sources you could configure the function to be invoked for multiple events and then you can handle them within a single invocation and better utilize the CPU and reduce the cost for IO intensive workloads. If your workload can be asynchronous, consider using this method (e.g., if the request is received from API gateway don't invoke the function directly but rather send it to SQS and then invoke the function using batching.
Not able to set lambda reserved concurrency in new accountAccepted Answerasked 5 days ago
Max number of SQS as triggers for a single lambda?Accepted Answerasked 2 years ago
Does lambda function implement the single instance concurrency model?Accepted Answerasked 4 months ago
Possible to override default GraphQL @model resolvers with Lambda function resolvers?Accepted Answerasked a month ago
is it possible that SQS Triggers to a Lambda Function Alias?Accepted Answerasked 4 months ago
Lambda Provisioned Concurrency on Version vs. Aliasasked a month ago
Does the Lambda , AWS Active MQ support Message Groups ?asked 5 months ago
Recommended Lambda function sizeAccepted Answerasked 3 years ago
can we attach the custom domain to lambda function urls ?Accepted Answerasked a month ago
If a Lambda @ Edge function calls DynamoDB, does the traffic stay within the AWS network?Accepted Answerasked a year ago