Azure Service Bus - how to preserve message ordering with retries when re-scheduling messages

Pedro Ferreira 25 Reputation points
2025-11-12T20:35:12.61+00:00

Hi everyone,

I’m working on a background task that listens to a topic for card updates and then publishes messages to an Azure Service Bus queue so that each card can be updated independently. Each message in this queue triggers a call to an external third-party API to process a specific card.

Use case

I’m using sessions in Service Bus so that all messages for a single card (session) are processed in strict order. The consumer (queue listener) picks up each message and calls the external API.

If the external API is down, I need to retry the messages until the system is back online - but still keep the message order and make sure no messages are lost.

Problem

If I reschedule a failed message (for example, by sending it back to the queue with a delay), it gets a new enqueue time and new sequence number. This means newer messages for the same session can be processed before the retried one (when the API comes back up), which breaks the ordering.

I also thought about abandoning the message instead of rescheduling, but that depends on the lock duration and the max delivery count (which is limited to 2000). If the external system is down for a longer time - even though that’s unlikely - the message could end up in the dead-letter queue (DLQ), which I’d like to avoid.

Question

I’m not an expert on Service Bus internals, so I’d really appreciate some advice on the best way to handle this. How can I:

  • Keep message ordering within a session
  • Safely retry failed messages when the external API is down
  • Avoid hitting the max delivery count or losing messages to the DLQ

I’m already using session-enabled queues, and I’m wondering if there are any built-in mechanisms or recommended design patterns to handle this situation better than manually rescheduling messages.

Thanks a lot for any help or suggestions - I’m still learning the best practices for this.

Pedro

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Vinodh247 40,031 Reputation points MVP Volunteer Moderator
    2025-11-13T06:50:42.56+00:00

    Hi ,

    Thanks for reaching out to Microsoft Q&A.

    Do not requeue messages. Instead, use in session retries with lock renewal or pause session processing using a circuit breaker. This preserves message order and avoids DLQs.

    =============================================================

    To preserve message ordering in azure service bus with retries:

    1. Keep messages in the same session - You already do this which ensures ordering. Never re-enqueue failed messages manually as it breaks seq numbers.
    2. Use session lock renewal + backoff retries - Instead of rescheduling, keep the session lock active and retry the failed message inmemory with exponential backoff (or until a timeout). Use the RenewSessionLockAsync() method periodically
    3. If the external API is down, stop processing messages for that session. Complete none of them until the API is available. This maintains order and avoids DLQ.
    4. Detect when the external API is unavailable, pause processing across all sessions, and resume only when healthy.
    5. Adjust max delivery count + DLQ monitoring : Set higher delivery count limits and use DLQ alerts for visibility but avoid relying on rescheduling

    Please 'Upvote'(Thumbs-up) and 'Accept' as answer if the reply was helpful. This will be benefitting other community members who face the same issue.


1 additional answer

Sort by: Most helpful
  1. Pedro Ferreira 25 Reputation points
    2025-11-14T12:07:57.48+00:00

    (please delete this comment)

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.