Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda async concurrency limits

I'm working on an AWS Lambda function that currently makes hundreds of API calls but when going into production it will make hundreds of thousands. The problem is that I can't test at that scale.

I'm using the async module to execute my api calls with async.eachLimit so that I can limit the concurrency (I currently set it a 300).

The thing that I don't understand is the limits on AWS Lambda. Here's what the docs say:

AWS Lambda Resource Limits per Invocation

  • Number of file descriptors: 1,024
  • Number of processes and threads (combined total): 1,024

As I understand it, Node.js is single threaded so I don't think I would exceed that limit. I'm not using child processes and the async library doesn't either so OK on that front too.

Now about those file descriptors, my function strictly calls the rest of AWS's API and I'm never writing to disk so I don't think I'm using them.

The other important AWS Lambda limits are execution time and memory consumed. Those are very clearly reported on each execution and I am perfectly aware when I'm close to reaching them or not, so let's ignore these for now.

A little bit of context:

The exact nature of my function is that every time a sports match starts I need to subscribe all mobile devices to the appropriate SNS topics, so basically I'm calling our own MySQL database and then the AWS SNS endpoint repeatedly.

So the question is...

How far can I push async's concurrency in AWS Lambda in this context? Are there any practical limits or something else that might come into play that I'm not considering?

like image 434
Julian Avatar asked Aug 04 '17 18:08

Julian


People also ask

What is AWS Lambda concurrency limit?

The default concurrency limit per AWS Region is 1,000 invocations at any given time. The default burst concurrency quota per Region is between 500 and 3,000, which varies per Region. There is no maximum concurrency limit for Lambda functions.

What happens when Lambda reaches concurrency limit?

When the burst concurrency limit is reached, the function starts to scale linearly. If this isn't enough concurrency to serve all requests, additional requests are throttled and should be retried. The function continues to scale until the account's concurrency limit for the function's Region is reached.

Can Lambda function be async?

Lambda functions can be invoked either synchronously or asynchronously, depending upon the trigger. In synchronous invocations, the caller waits for the function to complete execution and the function can return a value.

How many requests can Lambda handle per second?

Lambda doesn't limit the number of “requests per second/minute“, for example, as is common in API services. Developers can run as many requests per period of time as needed, providing that it doesn't violates concurrency limits.


1 Answers

As I understand it, Node.js is single threaded so I don't think I would exceed that limit. I'm not using child processes and the async library doesn't either so OK on that front too.

Node.js is event driven, not single threaded.
The Javascript engine runs on a single thread (the event loop) and delegates I/O operation to an internal library (libuv) which handles its thread pool and asynchronous operations.
async doesn't open a child process on its own, but behind the scenes, whether you're making an HTTP request or interacting with the file system, you're delegating these operations to libuv.

In other words, you've answered your own question well with the resources limits:

How far can I push async's concurrency in AWS Lambda in this context? Are there any practical limits or something else that might come into play that I'm not considering?

AWS Lambda Resource Limits per Invocation

  • Number of file descriptors: 1,024
  • Number of processes and threads (combined total): 1,024

It's hard to say whether libuv would open a new thread for each I/O operation, so you might get away with a little more than the numbers listed above. But you will probably run out or memory way before reaching those limits anyway.
The bottom line is no, you won't be able to make hundreds of thousands of calls in a single lambda execution.

Regarding the context of your function, depending on how often your job needs to run, you might want to refactor your lambda to multiple executions (it would also run faster), or have it on an EC2 with auto scaling triggered by lambda.

like image 64
LifeQuery Avatar answered Sep 28 '22 11:09

LifeQuery