Search code examples
mqttpaho

can mqtt msg delivery order be guaranteed in simplified case?


If there is only a single broker, a single publisher, a single topic and clean session, in this simplified case,

can msg delivery order on subscriber side be guaranteed to be the same as send order on publisher side? Will it be affected by QoS?


Solution

  • Section 4.6 from the MQTT 3.1.1 spec covers message ordering:

    4.6 Message ordering

    A Client MUST follow these rules when implementing the protocol flows defined elsewhere in this chapter:

    • When it re-sends any PUBLISH packets, it MUST re-send them in the order in which the original PUBLISH packets were sent (this applies to QoS 1 and QoS 2 messages) [MQTT-4.6.0-1]
    • It MUST send PUBACK packets in the order in which the corresponding PUBLISH packets were received (QoS 1 messages) [MQTT-4.6.0-2]
    • It MUST send PUBREC packets in the order in which the corresponding PUBLISH packets were received (QoS 2 messages) [MQTT-4.6.0-3] It MUST send PUBREL packets in the order in which the corresponding PUBREC packets were received (QoS 2 messages) [MQTT-4.6.0-4]

    A Server MUST by default treat each Topic as an "Ordered Topic". It MAY provide an administrative or other mechanism to allow one or more Topics to be treated as an "Unordered Topic" [MQTT-4.6.0-5].

    When a Server processes a message that has been published to an Ordered Topic, it MUST follow the rules listed above when delivering messages to each of its subscribers. In addition it MUST send PUBLISH packets to consumers (for the same Topic and QoS) in the order that they were received from any given Client [MQTT-4.6.0-6].

    Having read that I would conclude that messages will normally (unless the broker specifically set to use unordered topics) be sent in order, but if a high QOS message is not acknowledged properly it may be resent which could result in it being redelivered out of sequence.