7

I've scoured for any answer but everything I've read are about concurrent lambda executions and async keyword syntax in Node however I can't find information about lambda instance execution.

The genesis of this was that I was at a meetup and someone mentioned that lambda instances (i.e. a ephemeral container hosted by AWS containing my code) can only execute one request at a time. This means that if I had 5 requests come in (for the sake of simplicity lets say to an already warm instance) they would all run in a separate instance, i.e. in 5 separate containers.

The bananas thing to me is that this undermines years of development in async programming. Starting back in 2009 node.js popularized programming with i/o in mind given that for a boring run of the mill CRUD app most of your request time is spent waiting on external DB calls or something. Writing async code allowed a single thread of execution to seemingly execute many simultaneous requests. While node didn't invent it I think it's fair to say it popularized it and has been a massive driver of backend technology development over the last decade. Many languages have added features to make async programming easier (callbacks/tasks/promises/futures or whatever you want to call them) and web servers have shifted to event loop based (node, vertx, kestrel etc) away from the single thread per request models of yester year.

Anyways enough with the history lesson, my point is that if what I heard is true then developing with lambdas throws most of that out the window. If the lambda run time will never send multiple requests through my running instance then programming in an async style will just waste resources. Say for example I'm talking C# and my lambda is for retrieving widgets. Then this code var response = await db.GetWidgets() is actually inefficient because it pushes the current threadcontext onto the stack so it can allow for other code to execute while it waits for that call to comeback. Since no other request will be invoked until the original one completes it makes more sense to program in a synchronous style save for places where parallel calls can be made.

Is this correct?

If so I'm honestly shocked it's not discussed more. Async programming has paradigm shift I've seen in the last few years and this totally changes that.

TL;DR: does lambda really only allow one request execution at a time per instance? If so this up ends major shift in server development towards asynchronous code.

Piers MacDonald
  • 563
  • 1
  • 5
  • 18
  • As answers indicate, all the lambda runtimes will handle only one execution at a time. With a custom runtime, though, you may be able to handle multiple request simultaneously by calling the next-invocation API again before the first execution is done. I'd be interested in finding out if that works. https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html – Matt Timmermans May 23 '21 at 14:40

3 Answers3

4

Yes, you are correct - Lambda will spin up multiple containers to handle concurrent requests even if your Lambda does some work asynchronously (I have confirmed this through my own experience, and many other people have observed the same behavior - see this answer to a similar question). This is true for every supported runtime (C#, Node.js, etc).

This means that async code in your Lambda functions won't allow one Lambda container to handle multiple requests at once, as you stated. That being said, you still get all the other benefits of async code and you could still potentially improve your Lambda's performance by, for example, making many web service or database calls at once asynchronously - so this property of Lambda does not make async programming useless on the platform.

2

Further to the above answer, please see here. From what I understand, it's a synchronous loop. So, the only way to make things async from a request-handling perspective is to delegate the work to a message queue, e.g. SQS, as written here. I think this is similar to how Celery is used to make Django asynchronous. Lastly, if you truly want async handling of requests in line with async/await in node.js/python/c#/c++, if you may need to use AWS Fargate / EC2 instead of Lambda. Otherwise in Lambda, as you have mentioned yourself, it's bananas indeed. On the other hand, for heavy traffic, for which async/await shows its benefits, Lambda is not a good fit. There is a break-even analysis here about the three services: ec2, Lambda and Fargate.

Arnab De
  • 402
  • 4
  • 12
1

Your question is :

Since no other request will be invoked until the original one completes it makes more sense to program in a synchronous style save for places where parallel calls can be made.

No because you no longer have to wait the answer as you should do if you were using a sync process. Your trigger itself must die after the call so it will free memory. Either the lamba sends a notification or triggers a new service once it is completed, either a watcher looks at the result value (it is possible to wait the answer with a sync lambda, but it is not accurate due to the underlying async process beneath lambda system itself). As an Android developper, you can compare that to intent and broadcast, and it is completely async.

It is a complete different way to design solution because the async mechanism must be managed on the workflow layer itself and no longer in the core of the app, the solution becomes an aggregation of notifiers/watchers that triggers micro-services, it is no longer a single binary of thousand lines of code.

Each lambda function must be an individual micro-services.

Coming back to handle heavy traffic, you can run millions of Lambda in parallel as long as your micro-service is ending quickly, it won't cost much. To ensure that your workflow is not dropping anything, you can add SQS (queue messaging) in the solution.

C.Vergnaud
  • 857
  • 6
  • 15
  • I'm not sure I understand what you're saying. I'm talking about code executing within the lambda, not system architecture. When I say "async" I'm not referring to an asynchronous system with message queues etc. I'm talking about async programming features in languages, think `async/await`. The wait that feature works is by dumping whatever the execution context is (stack etc) into memory and allowing another thread to run. However since lamdba won't give you any other threads to process that context switching is a performance hit. – Piers MacDonald Oct 31 '18 at 19:48
  • I thought your were speaking of monolithic applications. From the lambda side itself, using async in the lambda function will be also a bad idea regarding the cost model. You have to exit as fast as possible. – C.Vergnaud Nov 01 '18 at 08:40
  • 1
    That just seems weird to me. We've been spending years telling developers to think of I/O and write async code and this model tosses that away. For seemingly no reason, it would be possible to write a load balancer to allow lambda instances to handle multiple requests. – Piers MacDonald Nov 03 '18 at 00:09
  • It is crucial to keep thinking about the I/O and async constraints but you design the solution with theses constraints, one step above the lambda function itself. – C.Vergnaud Nov 05 '18 at 11:32