QoS
QoS (Quality-of-Service) is an arrangement between the client and message broker with regard to guaranteeing of the message transfer. The QoS level must be specified by the client for both the publish and the subscribe process. There are three different QoS levels:
- 0 - message is sent at most once
- 1 - message is sent at least once
- 2 - message is sent exactly once
Interaction of publisher and subscriber QoS The MQTT specification (MQTT5, chapter 3.8.4) defines a few basic rules when publisher and subscriber QoS interact, especially when publisher and subscriber specify a different QoS level. |
QoS level 0
At this QoS level the receiver does not acknowledge receipt. The message is not sent a second time.
The following graphic illustrates this relationship once again, especially in comparison with the underlying TCP level.
QoS level 1
At this QoS level the system guarantees that the message arrives at the receiver at least once, although the message may arrive more than once. The sender stores the message internally until it has received an acknowledgment from the receiver in the form of a PUBACK message. If the PUBACK message fails to arrive within a certain time, the message is resent.
The following graphic illustrates this relationship once again, especially in comparison with the underlying TCP level.
QoS level 2
At this QoS level the system guarantees that the message arrives at the receiver no more than once. On the MQTT side this is realized through a handshake mechanism. QoS level 2 is the most secure level (from a message transfer perspective), but also the slowest. When a receiver receives a message with QoS level 2, it acknowledges the message with a PUBREC. The sender of the message remembers it internally until it has received a PUBCOMP. This additional handshake (compared with QoS 1) is important for avoiding duplicate transfer of the message. Once the sender of the message receives a PUBREC, it can discard the initial publish information, since it knows that the message was received once by the receiver. In other words, it remembers the PUBREC internally and sends a PUBREL. Once the receiver has received a PUBREL, it can discard the previously remembered states and respond with a PUBCOMP, and vice versa. Whenever a packet is lost, the respective communication device is responsible for resending the last message after a certain time.
The following graphic illustrates this relationship once again, especially in comparison with the underlying TCP level.