- Newest
- Most votes
- Most comments
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.
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.
Relevant content
- asked a year ago
- Accepted Answerasked a year ago
- Accepted Answerasked 2 years ago
- asked 8 months ago
- AWS OFFICIALUpdated 10 months ago
- AWS OFFICIALUpdated 10 months ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 15 days ago
Sure, thanks for your quick reply! I'll definitely give it a try and update here when I have more thoughts.