Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Wake up" time of Azure Function triggered by Service Bus Queue

I have an Azure Function triggered by Azure Service Bus queue - Azure Function App is hosted in consumption plan.

How long does it take at most to wake up an Azure Function when new message in queue appears? (assuming there were no message during past 20 minutes).

Is it specified anywhere? I've found in the Documentation that:

For non-HTTP triggers, new instances will only be allocated at most once every 30 seconds.

Does it mean that I won't be able to process the first message in the queue faster than within 30 seconds?

Will adding Timer Triggered Azure Function to the same Function App (along with Service Bus triggered) help to keep the Azure Function instance up and running ?

like image 991
Szymon Tomczyk Avatar asked Sep 19 '25 20:09

Szymon Tomczyk


2 Answers

Another option to handle a "cold start" of the ServiceBusTrigger function is using an eventing feature of the Azure Event Grid, where the events are emitted immediately if there are no messages in the Service Bus entity and a new message arrives. See more details here.

Note, that the event is emitted immediately when the first message arrived into the service bus entity. In the case, when the messages are there, the next event is emitted based on the idle time of the listener/receiver. This idle (watchdog) time is periodically 120 seconds from the last time used the listener/receiver on the service bus entity.

This is a push model, no listener on the service bus entity, so the AEG Subscriber (EventGridTrigger function) will "balanced" receiving a message with the ServiceBusTrigger function (its listener/receiver).

Using the REST API for deleting/receiving a message from the service bus entity (queue), the subscriber can obtained it very easy and straightforward.

So, using the AEG eventing on the topic providers/Microsoft.ServiceBus/namespaces/myNamespace and filtering on the eventType = "Microsoft.ServiceBus.ActiveMessagesAvailableWithNoListeners", the messages can be received side by side with a ServiceBudTrigger function and to solve the AF problem on the "cold start".

Note, that only the Azure Service Bus premium tier is integrated to AEG.

The following code snippet shows an example of the AEG Subscriber for Service Bus using the EventGridTrigger function:

#r "Newtonsoft.Json"

using System;
using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public static async Task Run(JObject eventGridEvent, ILogger log)
{
    log.LogInformation(eventGridEvent.ToString());

    string requestUri = $"{eventGridEvent["data"]?["requestUri"]?.Value<string>()}";
    if(!string.IsNullOrEmpty(requestUri))
    {
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("Authorization", Environment.GetEnvironmentVariable("AzureServiceBus_token"));
            var response = await client.DeleteAsync(requestUri);

            // status & headers
            log.LogInformation(response.ToString());

            // message body
            log.LogInformation(await response.Content.ReadAsStringAsync());
        }
    }

    await Task.CompletedTask;
}
like image 200
Roman Kiss Avatar answered Sep 22 '25 11:09

Roman Kiss


First of all, when using consumption plan and idle for about 20 minutes, the function app goes into idle. And then if you use the function, you will experience cold start, which needs to take some time to wake up.

For your question:

Does it mean that I won't be able to process the first message in the queue faster than within 30 seconds?

It depends on following(link is here):

1.which language you're using(eg. function using c# is more faster than java), the following is picked up from the link above:

A typical cold start latency spans from 2 to 15 seconds. C# functions usually complete the start within 3 seconds, while JavaScript and Java have longer tails.

2.The number of dependencies in your function:

Adding dependencies and thus increasing the deployed package size will further increase the cold start durations.

will adding Timer Triggered Azure Function to the same Function App (along with Service Bus triggered) help to keep the Azure Function instance up and running ?

Yes, add a Timer Triggered azure function to the same function app would keep the others warm up(up and running).

like image 37
Ivan Yang Avatar answered Sep 22 '25 10:09

Ivan Yang