DEV Community

Kiran Rongali
Kiran Rongali

Posted on

2 2 2 2 1

๐Ÿ” How Azure Service Bus Handles Retries Part- 1

In the post below, we discussed how Azure Service Bus integrates with a .NET application and the benefits it provides.
https://dev.to/kiranrongali/why-use-azure-service-bus-how-to-integrate-in-net-13f9

But what happens if a failure occurs during message processing? How does retry behavior work, and how many times will it attempt to process the message again? This post will help you understand that process.

When your message processor (receiver) throws an exception or fails to call CompleteMessageAsync, Azure Service Bus automatically retries processing that message, based on its configured max delivery count.

Retry Behavior (Default)
If your handler fails or throws an exception โ†’ message is re-delivered

This retry continues until:
The message is successfully completed (CompleteMessageAsync)
The max delivery count is exceeded

What Happens After Max Retries?
Once the message exceeds the MaxDeliveryCount (default: 10), it is moved to the Dead-Letter Queue (DLQ).

Dead-Letter Queue
This is a special sub-queue where failed messages are stored for inspection, manual or automated reprocessing.

You can access the DLQ like this:

#csharp
var receiver = client.CreateReceiver("your-queue-name", new ServiceBusReceiverOptions
{
    SubQueue = SubQueue.DeadLetter
});
Enter fullscreen mode Exit fullscreen mode

What You Need to Do

  • Proper Error Handling Wrap your message processing in try/catch:
#csharp
processor.ProcessMessageAsync += async args =>
{
    try
    {
        var body = args.Message.Body.ToString();
        Console.WriteLine($"Processing: {body}");

        // Your logic here

        await args.CompleteMessageAsync(args.Message);
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error: {ex.Message}");
        // Do NOT complete the message โ†’ it will retry
        // Optionally log or notify
    }
};
Enter fullscreen mode Exit fullscreen mode
  • Configure MaxDeliveryCount In Azure Portal โ†’ Service Bus Queue โ†’ Properties, you can set MaxDeliveryCount.

Or via ARM/CLI:

#json
"maxDeliveryCount": 5
Enter fullscreen mode Exit fullscreen mode
  • Monitor the Dead-Letter Queue

Use: Azure Portal (Explore Dead-letter queue)

Azure Monitor / Alerts

Code-based reprocessing via SubQueue.DeadLetter

Optional: Auto Retry with Delay
Azure Service Bus doesnโ€™t support native delayed retries (e.g., exponential backoff), but you can:

Use Scheduled Enqueue Time to requeue a message after a delay

Move failed messages to a custom retry queue with delay

Example of scheduling a retry:

#csharp
var retryMessage = new ServiceBusMessage("retry this")
{
    ScheduledEnqueueTime = DateTimeOffset.UtcNow.AddMinutes(5)
};
await sender.SendMessageAsync(retryMessage);
Enter fullscreen mode Exit fullscreen mode

Best Practices:

  • Use try/catch to Prevent app crashes & enable retry logic
  • Use Monitor DLQ to Avoid silent message losses
  • Use custom retry queues for control over backoff and limits
  • Handle poison messages Log, alert, or notify for manual review

That's it for this Part1 and Part 2 will be covered in a separate post, focusing on the retry flow and monitoring the Dead-Letter Queue (DLQ).

Runner H image

New Hire? Let AI Handle Onboarding Tasks

From welcome docs to checklists to calendar invites โ€” Runner H automates the entire onboarding workflow across Notion, Google Calendar, and forms.

Try Runner H

Top comments (0)

Heroku

Build AI apps faster with Heroku.

Heroku makes it easy to build with AI, without the complexity of managing your own AI services. Access leading AI models and build faster with Managed Inference and Agents, and extend your AI with MCP.

Get Started

๐Ÿ‘‹ Kindness is contagious

Explore this insightful write-up, celebrated by our thriving DEV Community. Developers everywhere are invited to contribute and elevate our shared expertise.

A simple "thank you" can brighten someoneโ€™s dayโ€”leave your appreciation in the comments!

On DEV, knowledge-sharing fuels our progress and strengthens our community ties. Found this useful? A quick thank you to the author makes all the difference.

Okay