3

If you send messages to an Azure Service bus topic/queue using SendAsync(...) is the message ordering guaranteed?

For example if I had an application with one thread that was sending lots of messages using SendAsync, would the order in which the method SendAsync was called be maintained? Is the Service Bus client library smart enough to know that although the messages will be sent asynchronously, that the order in which they are added to the topic is maintained throughout?

Geoff Smith
  • 585
  • 1
  • 5
  • 14
  • Does this answer your question? [How do you support FIFO message ordering with Azure Service Bus partitioned queues/topics?](https://stackoverflow.com/questions/28114288/how-do-you-support-fifo-message-ordering-with-azure-service-bus-partitioned-queu) – Michael Freidgeim Nov 13 '21 at 00:14

2 Answers2

2

Async or not, trying to extract an ordering guarantee from a message bus is a bit of a slippery slope. You are at the mercy of the network which is always going to be unreliable.

Note that Azure service bus doesn't offer any ordering guarantees by default. You can introduce some first-in-first-out ordering through message sessions but this is not enough to guarantee the order in which your messages will be processed. There's a good piece that sums up the difficulties with message ordering here:

http://blog.jayway.com/2013/12/20/message-ordering-on-windows-azure-service-bus-queues/

In general, I try to design messages so that ordering does not matter. Failing that I would recommend adding sequence numbers to the messages which can be interpreted by the message consumers.

rrrr-o
  • 2,447
  • 2
  • 24
  • 52
Ben Morris
  • 585
  • 3
  • 6
  • Yes you make a good point. It is a complicated area, and I have been reading a lot about the intricacies, in terms of ensuring messages are processed in order. However for now, I just want to at least see I can get them added to the queue in the right order, which I can with 1 process and synchronous Send() calls. What I am trying to establish is whether this is possible with SendAsync()... – Geoff Smith Jan 26 '15 at 10:44
  • 1
    I have a test app I use to send many messages to a Topic using a loop that does 5 sendAsyncs at a time. I can tell you at least on startup that the first sendAsync message has never been the first message into the Topic. – Mikee Feb 06 '15 at 17:36
  • Before resorting to sequence numbers, could one just wait N secs before sending to a topic thus guaranteeing that each message has enough head-start to get there before the next? The question remains: what is a fail-safe value for N. – GGleGrand Nov 25 '15 at 20:26
0

It's not the sends that aren't happening in order. It is likely the receives. If the topic is partitioned (the default now) we will shard the messages across many machines.