MQTT Message Ordering Across Topics

Hi everyone,
I have a question regarding message ording with Mosquitto.
Does Mosquitto gurantee the following: If the same client sends two messages (one after the other with the synchronous client) on two different topics, is it guranteed that the messages arrive in this order at other clients that are subscribed to both topics?
How does the property max_inflight_messages influence this behaviour? Can the order be guaranteed when I set QoS to 1 (or 2) and max_inflight_messages to 1?

Thanks for your help.

According to the MQTT-Standard the broker has to guarantee the delivery order of message only for a single client within a single topic and a single QOS level. This means there is no ordering guarantee for messages published by the same client to different topics or with different QOS levels.
To create an application design, which relay on message coming in order on different topics is therefore in general a bad design. Even if the Mosquitto broker may be configured in a way to behave exactly to your need does not guarantee this behavior will not change in the future. The Mosquitto Broker will always follow the MQTT standard requirements.
With this desclaimer above some more notes on the actual implementation inside Mosquitto broker (which is subject to change any time…):
Right now the Mosquitto Broker is implemented as a single threaded message distribution engine. Each incoming message will be processed immediately inside the broker. If a subscriber has not reached his max_inflight_messages limit the message will be sent out to the client immediately. If the subscriber has reached his limit the handling will depend on the messages QOS. A message with QOS=0 will be dropped for this client (as the client is considered overloaded). A message with QOS>0 will be placed at the end of the queue for this client, if there is still space available for this client depending on the configuration of max_queued_messages/max_queued_bytes. Later free slot in the in-flight queue of this client will be filled in order from the message queue.
So this the current implementation the messages should arrive in order at the subscriber. But to repeat myself: This is an implementation detail, which is subect to change on any time in the future.
So I would highly recommend to implement a different solution design, which does not depend on the ordering of messages in different topics.

1 Like

Hi Norbert!

Thanks for your information! Concerning this topic message order issue - Can one rely on receiving messages from different topics in order if the subscription contains wildcards and encompasses these topics?

Is message order maintained across wildcard topics? Suppose I have two devices publishing to:
living_room/temp and

Will a subscriber to
receive the messages in the original publishing order?

I’m aware it will work that way for now due to your explanation above - But my question is whether section 4.6 of the MQTT-spec would require such a behaviour according to your understanding?

“By default, a Server MUST treat every Topic as an Ordered Topic when it is forwarding messages on Non‑shared Subscriptions”.

Kind regards