azureazureservicebus

Retry delay on Function with Azure ServiceBus queue trigger


I have an Azure Function that is triggered by a Service Bus queue. When the function fails, it is retried immediately (after 1s each) up to 10x. However, I would like to retry it every five minutes. Even after reading the docs, it was not clear to me how this is supposed to be implemented.

[Function("Foo")]
public async Task Run(
  ServiceBusTrigger("FooQueue",
  Connection = "ServiceBusConnection")]
  Foo foo,
  int deliveryCount,
  DateTime enqueuedTimeUtc,
  string messageId)

The docs say that the retry behavior can be configured in the host.json. However it also says The clientRetryOptions settings only apply to interactions with the Service Bus service. They don't affect retries of function executions.

At the same time, I did not find a setting in the service bus queue with which the retry behavior can be configured.

What is the "official" way of implementing a retry strategy? Is it supposed to be implemented in the function itself, using delays like Task.Delay along with the DeliveryCount parameter?


Solution

  • As you've already found out, Service Bus trigger can only perform immediate retries by leveraging MaxDeliveryCount. Delayed retries are not supported neither by the Functions SDK nor the Azure Service Bus.

    For delayed retries with Isolated Worker SDK functions, you could implement a custom solution enabled by the middleware option the new Functions SDK supports. I captured the idea in a blog post. The TLDR version is to leverage delivery count for immediate retries and issue a cloned message for delayed retries, using message headers to keep a track of the retry attempt.