Azure function: limit the number of calls per second
Asked Answered
D

4

10

I have an Azure function triggered by queue messages. This function makes a request to third-party API. Unfortunately this API has limit - 10 transactions per second, but I might have more than 10 messages per second in service bus queue. How can I limit the number of calls of Azure function to satisfy third-party API limitations?

Disbelief answered 17/3, 2018 at 18:7 Comment(1)
Already answered here. There's no good story for throttling ATM.Linsang
M
6

Unfortunately there is no built-in option for this.

The only reliable way to limit concurrent executions would be to run on a fixed App Service Plan (not Consumption Plan) with just 1 instance running all the time. You will have to pay for this instance.

Then set the option in host.json file:

"serviceBus": {
    // The maximum number of concurrent calls to the callback the message
    // pump should initiate. The default is 16.
    "maxConcurrentCalls": 10
}

Finally, make sure your function takes a second to execute (or other minimal duration, and adjust concurrent calls accordingly).

As @SeanFeldman suggested, see some other ideas in this answer. It's about Storage Queues, but applies to Service Bus too.

Mackie answered 17/3, 2018 at 19:4 Comment(0)
B
2

Do you want to get rid of N-10 messages you receive in a second interval or do you want to treat every message in respect to the API throttling limit? For the latter, you can add the messages processed by your function to another queue from which you can read a batch of 10 messages via another function (timer trigger) every second

Baelbeer answered 18/3, 2018 at 17:49 Comment(2)
This doesn't necessarily guarantee the limit. If the calls were slow in second #1 and processed just 5 requests, the second function might get 5+10 requests at second #2.Mackie
The second function (triggered by the timer) decides on how many messages it reads (batch size or app logic)Baelbeer
O
1

You can try writing some custom logic i.e. implement your own in-memory queue in Azure function to queue up requests and limit the calls to third party API. Anyway until the call to third party API succeeds, you dont need to acknowledge the messages in the queue. In this way reliability is also maintained.

Octahedrite answered 17/3, 2018 at 18:50 Comment(1)
That goes against the model of Functions. It's then easier to just write a custom Service Bus consumer app.Mackie
B
0

The best way to maintain integrity of the system is to throttle the consumption of the Service Bus messages. You can control how your QueueClient processes the messages, see: https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-get-started-with-queues#4-receive-messages-from-the-queue

Check out the "Max Concurrent calls"

 static void RegisterOnMessageHandlerAndReceiveMessages()
{
    // Configure the message handler options in terms of exception handling, number of concurrent messages to deliver, etc.
    var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
    {
        // Maximum number of concurrent calls to the callback ProcessMessagesAsync(), set to 1 for simplicity.
        // Set it according to how many messages the application wants to process in parallel.
        MaxConcurrentCalls = 1,

        // Indicates whether the message pump should automatically complete the messages after returning from user callback.
        // False below indicates the complete operation is handled by the user callback as in ProcessMessagesAsync().
        AutoComplete = false
    };

    // Register the function that processes messages.
    queueClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
}
Bordiuk answered 17/3, 2018 at 19:7 Comment(1)
One doesn't have direct access to QueueClient in Azure FunctionsMackie

© 2022 - 2024 — McMap. All rights reserved.