Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting Azure Functions Rate of Service Bus Message Intake

Suppose we have the following scenario:

  • Service Bus Queue with 10,000 messages
  • Azure Functions (on Consumption plan) with function set up as a trigger for the SB Queue
  • An external (out of our control) system which fails past a certain request rate

If I queue those 10k messages as fast as I can, the external system is not able to handle the concurrent load. I don't know how many function instances or app service instances ("scale out") Azure runs at the same time to process these messages.

Here is a code sample of what my function definition looks like:

public class MyQueueProcessor
{
    private IMyDependency MyDependency { get; }

    public MyQueueProcessor(IMyDependency myDependency)
    {
        MyDependency = myDependency;
    }

    [FunctionName(nameof(MyQueueProcessor))]
    public async Task Run([ServiceBusTrigger("my-queue-name", Connection = "MyQueueConnection")] string item, ILogger log)
    {
        var req = JsonConvert.DeserializeObject<MyQueueRequest>(item);

        log.LogInformation($"Beginning processing of " + req);

        // Valudate req
        // Call external service REST API
        await MyDependency.CallService(req, new { otherParameters = true });

        log.LogInformation($"Finished processing of " + req);
    }
}

I am not able to process all 10k messages with a single function because the external service call takes a number of seconds to complete (5-10s), and processing all of these messages with one function call would exceed the 10min maximum for function calls.

How can I limit the number of concurrent functions running that are consuming the messages from my queue?

like image 376
qJake Avatar asked Apr 15 '20 14:04

qJake


People also ask

What is Service Bus throttling?

How does throttling work in Service Bus Premium? With exclusive resource allocation for Service Bus Premium, throttling is purely driven by the limitations of the resources allocated to the namespace. If the number of requests are more than the current resources can service, then the requests will be throttled.

How do I throttle Azure function?

In order to limit inbound HTTP traffic or a number of incoming messages from EventHub, there is a need to edit the host. json configuration file of Azure Function. Bear in mind that online edit in SCM or Portal will restart an application.

What can be the maximum lock duration for a message in Azure Service Bus queue?

By default, the message lock expires after 60 seconds. This value can be extended to 5 minutes.


1 Answers

You can now limit the number of instances with "Enforce Scale Out Limit" in the Azure Portal under the "Scale out" menu on your Azure Function App. When it is turned off the limit will default to 200 instances on a consumption plan.

Note that when you are using the Service Bus binding each instance can also receive and process messages concurrently. You can limit this by setting maxConcurrentCalls in host.json. The default value is 16.

{
  "version": "2.0",
  "extensions": {
    "serviceBus": {
      "messageHandlerOptions": {
        "autoComplete": true,
        "maxAutoRenewDuration": "00:05:00",
        "maxConcurrentCalls": 16
      }
    }
  }
}

The maximum number of messages being processed concurrently will then be the number of instances multiplied by maxConcurrentCalls.

like image 109
sveinungf Avatar answered Sep 30 '22 14:09

sveinungf