Suppose we have the following scenario:
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?
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.
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.
By default, the message lock expires after 60 seconds. This value can be extended to 5 minutes.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With