What an MQTT Session and Quality of Service Do

MQTT QoS 1 says the message arrived at least once; it can produce a copy. Session expiry says how long a dropped client’s subscription is kept.

What an MQTT Session and Quality of Service Do

In MQTT, quality of service is a contract about how many times a message may arrive. A larger number does not make the link "better". The contract gets heavier. OASIS MQTT 5.0 separates three levels. QoS 0 sends the message at most once; if it is lost, it is lost. QoS 1 arrives at least once; if the network retries, the same message arrives a second time. QoS 2 asks for an extra handshake so that it arrives exactly once.

A receiver on QoS 1 must not act on a copy of a message it already handled. The identifier on the message is compared with those already processed. Without that comparison a command runs twice. A door, a relay or a write is designed with the QoS choice in view. The sentence "we use QoS 1" does not show that the command ran once.

The session is a separate decision. MQTT 5.0 writes how long the session is kept in the session expiry interval. If the interval is zero, a dropped connection deletes the subscription and any queued message. If the client returns before the interval ends, the subscription is still there and messages queued during the gap are delivered. The interval is chosen from how long the device may be unreachable in the field. Keeping it forever accumulates memory on the broker.

Clean start wipes that interval. If every boot asks for a clean start, the device's previous subscription is ignored. That is a deliberate choice. It is not left as a default.

On a FenergyTech field link, QoS and the session interval are written separately for each kind of message. A command and a measurement are not put on the same level.

A command that arrives at QoS 1 can run twice. The door receives "open", it opens, and when the same identifier arrives again it tries to open again. If the identifier is not compared with those already handled, the second arrival is taken as a new command. The comparison is written after the message is handled and before the acknowledgement is sent. If it is not, a link that drops after the acknowledgement leaves and before the record is written runs the command a third time.

The session interval needs a number too. Session expiry in MQTT 5.0 is in seconds. Zero deletes the subscription when the link drops. 3600 keeps the subscription across an hour of silence and delivers what queued. If a field device wakes once a day, 3600 is not enough. The interval is longer than the wake gap and short enough not to fill the broker's memory. An unbounded interval grows the queue of a device that never returns.

If every boot asks for a clean start, the previous subscription is ignored. If that is on purpose, it is written down. It is not left as a default. A measurement and a command are not put on the same QoS. A copy of a measurement can be harmless. A copy of a command is not.

Sources: OASIS, MQTT Version 5.0, 2019, quality of service and session sections.

Share:

Planning a similar project?

We can talk through how the topic in this article applies to your product.

More articles

Browse our other technical articles