Reputation: 6365
I wonder if the message sent order is preserved. That is, when a publisher sends a sequence of messages, is each subscriber guaranteed to receive the same sequence as the publisher had sent it? For both clean and persistent sessions?
Upvotes: 39
Views: 22788
Reputation: 10450
when a publisher sends a sequence of messages, is each subscriber guaranteed to receive the same sequence as the publisher had sent it?
This question has already been answered and well accepted but I see an issue with the following statement in the accepted answer.
QoS 2 messages will be delivered in order
As per the documentation, it is mentioned the sequence of packets PUBLISH
,PUBREC
, PUBREL
, PUBCOMP
will be maintained per topic across QOS 2
level messages. However, a subscriber can still receive in different order than published by publisher (possible but rare). The same logic is also applicable for QOS 1
.
Let's see how:
PUBLISH packet has been send by broker for message m1.
PUBLISH packet has been send by broker for message m2.
PUBREC packet has been send by subscriber for message m1.
PUBREC packet has been send by subscriber for message m2.
PUBREL packet has been send by broker for message m1. But it got dropped.
PUBREL packet has been send by broker for message m2.
PUBCOMP packet has been send by subscriber for message m2.
Timeout for PUBREL packet at the broker has occurred for message m1. Broker will retry for message m1.
Broker re-transmits PUBREL packet for message m1.
PUBCOMP packet has been send by subscriber for message m1.
By above sequence, there is an possibility of the message m2 being processed first at the receiver. However, m1 was published before m2.
See this answer for further details.
Picture taken from u-blox.
Upvotes: 8
Reputation: 10117
A summary of the message ordering capabilities in MQTT 3.1.1 can be found in the specification itself here.
In summary:
QoS 1 ordering can be guaranteed if the client/broker only allow a single message inflight at any time.
Upvotes: 60