Search code examples
azureazureservicebusazure-servicebus-queues

Azure Service Bus trigger not firing sometimes


I created function app v1 with one function in it. The function is a service bus trigger.

After publishing it to Azure i see this strange behaviour. Sometimes the function gets triggered and sometimes it's not. There are no errors in logs at that time, but i see messages were put into dead letter queue with "delivery count excided" error.

Now, if i try to resend failed messages - it fails again. But if i go to portal, refresh function app and send the messages again - they get picked up and processed as usual.

Here's how i send messages:

var client = new QueueClient(connectionString, queueName);
var bytes = Encoding.UTF8.GetBytes(msgStr);
var message = new Message(bytes);
await queueClient.SendAsync(message);
  • I don't see this issue when run function locally.
  • New messages (<5) arrive every 15-20 minutes
  • I'm using Consumption plan
  • host.json is empty

Please help.


Solution

  • Cold Start

    On consumption plan your function app goes to sleep after 10-15mins if it's idle. Any upcoming trigger awakes your function app which may take couple of seconds called cold start.

    The problem in my opinion

    Looking at the stats you shared in my opinion when your service bus tries to deliver message the function app is sleeping and is unable to deliver considering it message failed to deliver. When you restart your function app your function app instance is warmed up explicitly and is able to handle the messages for that window but it would go again to sleep after idle period.

    Possible solutions

    Keep instance warmed

    Keep your function app warmed up all the time. You can simply create another time triggered function which keeps hitting your service bus triggered function (function that is handling messages) every 5 mins or so, it will keep the instance running all the time.

    Increase retry count

    You need to check your retries count and increase to greater number so when function apps wake up the service bus is still able to deliver the messages.

    Use App service plan

    Another way is to use app service plan instead consumption plan. This will keep the function app warm up all the time to handle the messages. considering your workload (<5) it could be bit expensive option ,since in consumption plan you get 1Million req/month free of cost.