Peter
Peter

Reputation: 329

Azure Function: Limiting number of concurrant Service Bus messages

I am working on an Azure Function that triggers from a session-based Service Bus topic and sends data to a CRM system via API requests. The CRM's API has usage and load-protection limits, and we may recieve a huge amount of messages in a short timeframe, so I am looking for the best way to throttle the function.

I'm thinking the maxConcurrentSessions setting in the host.json might help me, I could limit it to around 20 messages at a time. I could also look at the "Enforce Scale Out Limit" in the Function App, but I guess that wouldn't necessarily limit the number of messages processed at one time. Would maxConcurrentSessions be the best setting to go for, or are there other considerations?

Another approach (probably in addition to the above) would be to "pause" or stop the function from taking any more messages for a period of time after I reach an API limit. Does anyone know if it's possible to programatically pause the execution of an Azure Function in real-time, or to prevent it from taking any more messages?

Upvotes: 3

Views: 3686

Answers (2)

suziki
suziki

Reputation: 14080

I'm thinking the maxConcurrentSessions setting in the host.json might help me, I could limit it to around 20 messages at a time. I could also look at the "Enforce Scale Out Limit" in the Function App, but I guess that wouldn't necessarily limit the number of messages processed at one time. Would maxConcurrentSessions be the best setting to go for, or are there other considerations?

You can use maxConcurrentCalls to limit, the maximum number of concurrent calls to the callback that the message pump should initiate. By default, the Functions runtime processes multiple messages concurrently. If you set to 1, it will process only a single queue or topic message at a time.

The structure is like below:

{
    "version": "2.0",
    "extensions": {
        "serviceBus": {
            "prefetchCount": 100,
            "messageHandlerOptions": {
                "autoComplete": true,
                "maxConcurrentCalls": 1,
                "maxAutoRenewDuration": "00:05:00"
            },
            "sessionHandlerOptions": {
                "autoComplete": true,
                "messageWaitTimeout": "00:00:30",
                "maxAutoRenewDuration": "00:55:00",
                "maxConcurrentSessions": 1
            }
        }
    }
}

At the same time, you can set the Maximum Scale Out Limit to a smaller value to prevent the function from expanding under high load.

Upvotes: 1

Alan
Alan

Reputation: 135

I could also look at the "Enforce Scale Out Limit" in the Function App, but I guess that wouldn't necessarily limit the number of messages processed at one time.

I would also suggest looking at the Singleton attribute as a way to control scale-out concurrency (see the Microsoft docs). It will provide control over concurrency down to the function level. The documents also discuss your scenario of using it with maxConcurrentSessions/Calls.

Upvotes: 0

Related Questions