MQTT: what is "QoS" (Quality of Service) and how does it work?

3 minutes of reading

MQTT logo

The so-called Quality of Service (in short QoS) is among the most important features of the proProtocol MQTT. The quality of the service referred to is understood as the degree of accuracy in the delivery of MQTT messages between senders and the broker and broker and subscribers, or the accuracy that defines the guarantee of effective, successful delivery of such messages.

there three levels of service QoS:

  • At most once (0 level)
  • At least once (1 level)
  • Exactly once (2 level)

QoS MQTT is basically concerned the two phases of delivery of a message sent by a sending client and received by a subscriber client:

  • sending from the sender to the broker;
  • sending from broker to final recipient.

In the first phase the sender decides the QoS level associated with the message and sends it to the broker; in the second, the broker in turn turns that message over to all clients that have a subscription for that message using the same QoS level, unless the client subscribers do not have prosaw the registration indicating a lower QoS level. In this case, the broker will turn the message with a lower QoS level.

QoS is very important because it allows the sender to choose the level of service based on network quality and logic applicativa. Below we see the various differences.

QoS 0

The minimum level for QoS is zero. It is a conceived level for maximum performance with minimum effort: in this scenario there is no guarantee of delivery. The sender sends to the broker and forget about it, waits for no response either prosees a possible re-sending. To put it simple, how it goes, it goes.

QoS 1

In this scenario the sender (who is the one who sends the initial message to the broker, or it is the broker himself who turns a message to the final receiver) keeps the message in memory until it receives from the recipient the ok of receipt occurred. If he does not receive it within a reasonable time, he sends it again, a second time, and waits again for a reply, and so on. Using QoS 1 could therefore occur multiple deliveries of the same message. When in fact the broker receives a message marked with QoS 1 provvede immediately to turn it to the subscribers of that message and then answers "done" to the sender; if the time between sending and receiving the confirmation packet is too long (for example in congested networks), the sender sends the message again, triggering the whole chain again and causing duplicate sending.

QoS 2

This is the mode slower but also more reliable: in essence the prothis involves a double rebound between sender and recipient in order to confirm to the sender the actual taking over of the message and therefore the cancellation of a possible re-sending. This guarantees that the message arrives exactly one and only once, but obviously it is more expensive (minimally) for the systems, for the network and for the timing.

What is the best?
There is obviously no single answer. It depends, as often happens, on the scenario of appof the protocollo MQTT, which is designed for light and fast communications, but its configuration in terms of QoS must be carefully calibrated according to the area of ​​use.

We recommend QoS 0 when:

  • the network connection between the clients and the broker is reliable, fast and stable. Trivially, when using a cable connection;
  • it's not a proproblem if some message is lost;
  • it is not necessary to use message queuing and persistent sessions.

We recommend QoS 1 when:

  • the network connection can sometimes be unstable, as in the case of Wi-Fi household;
  • it's not a proproblem to eventually receive duplicate messages;
  • it is not possible to support the overload given by QoS 2.

Finally we recommend the QoS 2 when è critical the fact of always guaranteeing one and only one delivery for every single message.

In the personal home automation - you may have already figured it out for yourself - the recommended QoS is 1, as usually the reception (however rare) of duplicate messages does not usually cause proproblems of any kind. Let's imagine a topic like the following:

cmnd /Tasmota/ POWER 1

which usually controls an actuator equipped with firmware Tasmota to switch off the relay. It would perhaps be a proif the actuator received the same message twice in sequence? Certainly not. Instead it would be the fact of no receive it, which could happen if you use QoS 0.

There are obviously exceptions: for example, in the case of a MQTT alarm system it is advisable to use the QoS 2, as it is conceivable that it is more approreceive commands unique and certain, always.

How to configure MQTT on the devices of the profirst home automation