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?