-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Messages ordering when receiving batch of messages from Service Bus #43026
Comments
Thank you for your feedback. Tagging and routing to the team member best able to assist. |
Hi @junaidahmed92. Thanks for reaching out and we regret that you're experiencing difficulties. The client will always return messages to you in the order that the Service Bus returns them. What you need to be aware of, however, is that the Service Bus service itself does not guarantee message ordering unless sessions are used. Assuming no errors and normal operation, you'll usually see messages streamed to the client in the order that they were received by the service. However, messages which are locked by a reader are not sent by the service and, thus, may appear out-of-order if the holder of the lock does not complete the message. For example, This is also true if your application has the message, but the lock expires before you complete it. For example, As for choosing a session identifier, anything that is unique and can be used to group related messages together would work. More information can be found in First-in, first-out (FIFO) pattern. |
Hi @junaidahmed92. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text "/unresolve" to remove the "issue-addressed" label and continue the conversation. |
Thanks @jsquire for tbe detailed answer. |
Correct, assuming that you're not receiving/processing in parallel and prefetch is not enabled. |
No, I'm not processing in parallel. I am fetching a batch of messages and processing them in loop. |
If strict ordering guarantees are needed, yes. There's a known issue in the AMQP transport library that we use which causes prefetch + sessions to ignore ordering in a rare edge case. The team that owns the transport is investigating, but the behavior has only been observed in one application and is non-deterministic so they're still attempting to find the root cause. |
Thanks @jsquire for the clarification. I've changed my implementation. However I've got a question, what if I've only receiver who is responsible to receive all messages and if there's an error or a lockdown expires, the same receiver will receive and process it again. Are using sessions still preferable in this scenario? |
I've got couple of more questions:
|
|
Thanks for the clarification. But when I wss not using sessions the delivery count was completely fine. With ServiceBusReader i mean. Also can you confirm if I only have a single ServiceBusReceiver, is tbe messaging ordering still a problem without sessions. |
Delivery count is a broker-owned field and actions taken are by the service; the Azure SDK clients don't play a part in how it works - sessions or no. It's all service behavior, so there's nothing that I can offer beyond what I've already shared.
Yes, for the reasons that I mentioned above. Any scenario where the lock expires, you have an exception in your processing, your application or its host crashes/reboots/migrates can cause messages to be delivered out of order. They're edge cases, but not uncommon ones. The salient point is that the Service Bus service makes no guarantees about strict ordering unless sessions are used. Here, again, the client has no insight nor influence; we just return what the service gave us in the order that it gave it to us. |
Alright, thanks. Closing this issue now. |
Library name and version
Azure.Messaging.ServiceBus 7.15.0
Query/Question
I'm receiving messages in batch from Azure Service Bus:
IReadOnlyList<ServiceBusReceivedMessage> receivedMessages = await serviceBusReceiver.ReceiveMessagesAsync(maxMessages: maxMessages, maxWaitTime: TimeSpan.FromSeconds(1), cancellationToken: cancellationToken);
It works fine but I am unsure if it also consider the ordering (FIFO). I know what we can achieve this behaviour using Sessions. But I want to ask if I need to use sessions when receiving batch of messages?
I've a single receiver that receives and process messages in batches. But I want to ensure ordering as I am receiving devices info e.g last seen etc and I need to make sure that I've updated the correct info.
Also if Sessions is the only way then should I just set the SessionId to DeviceId and that will make sure that the all the messages linked to a single device get processed in order?
I've done a quick test and I can see that message are received in sequence. I've added batch of 150 messages to the topic:
After this I added a receiver method that receives and logs the message to the console:
After multiple tests, I get the messages in sequence. I'm unsure if I've missed any scenario or is this expected.
Environment
.Net Core 3.1
Visual Studio 2022
The text was updated successfully, but these errors were encountered: