Search code examples
c#.netmsmq

MSMQ: Is it possible to consume the message in the same order as they were being sent?


Let say I have a queue Q ,Q is the destination of message. I know that MSMQ guarantees that multi messages encompassed in the transaction are received in the same order in which they were sent.But my application send one message per transaction to Q(messages have same destination).Can messages order still being preserved when it reach Q?


Solution

  • Yes, you should be OK although it may depend on how flaky the network is. MSMQ uses internal ack messages to ensure messages are successfully delivered. The retry mechanism will handle any lost messages. It may be possible, depending on retry window size, etc., for a lost message to be delayed behind the following message.

    I can also imagine it may be possible for one message to get ahead of another - for example, a 1kb message following a 4MB message - due to the time it takes for a message to be persisted from the network stack to the disk.

    Both scenarios are edge cases, though.

    Bottom line is that order is not guaranteed outside a transaction so, if your app depends on it, make sure the messages contain a sequence number of some kind.