Limit Azure Function to run 1 request at a time - ideally with a delay

Willo 0 Reputation points
2023-02-02T23:35:30.8433333+00:00

I have a javascript Azure function on a consumption plan that is triggered by an HTTP request. This Azure function makes an HTTP request itself to a 3rd party API.

The problem I am facing is that the Azure function is sometimes getting called dozens/hundreds of times within a few seconds, and so the outgoing API calls to the 3rd party start returning 429 (too many requests).

I want to try and limit the azure function to only handle one request at a time. To do so I have added:

maxConcurrentRequests: 1

WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUT = 1

to host.json and function configuration respectively

I have also added the following function that I await call before each external API call to try and slow down the requests

function RunAfterOneSec() {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve('resolved');
      }, 1500);
    });
}

However, despite these efforts, I am still getting 429 responses from the API rate limited (limited to 60 requests/min)

Any help would be greatly appreciated.

Cheers.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,116 questions
{count} votes

2 answers

Sort by: Most helpful
  1. TP 71,761 Reputation points
    2023-02-07T06:30:44.9833333+00:00

    Hi,

    I recommend you use one of the open source rate throttling packages available. For example, node rate limiter:

    https://github.com/jhurliman/node-rate-limiter

    There are many others if you don't like the above. I've not tested it specifically with Azure Functions.

    If the above is helpful please click Accept Answer.

    Thanks.

    -TP


  2. MughundhanRaveendran-MSFT 12,401 Reputation points
    2023-02-18T06:17:36.29+00:00

    @Willo , First I would suggest you look at the third party API to make sure that there are no other requests that are being served apart from the Azure Function's outbound requests.

    The second thing to note here is that azure functions are event based. So a client is sending requests to Function and it is getting executed. You have already made sure that the function would execute only once for a request and no concurrency behavior by enabling the app settings. So you will have to make sure that the client doesn't send more than 60 requests/min. You can check the Function execution and errors detector present in the "Diagnose and solve problems" section to look at the function executions per hour/min