The quality of service

MQTT protocol defines the message Quality of Service (QoS), which ensures the reliability of message delivery in different network environments. The design of QoS is the focus of MQTT protocol. As a protocol specially designed for the Internet of Things, MQTT operates not only on PC, but on a wider range of narrow bandwidth networks and low-power devices. If the transmission quality problem can be solved at the protocol layer, it will provide great convenience for the development of Internet of Things applications.

QoS values Bit 2 Bit 1 describe
0 0 0 Distribute at most once
1 0 1 At least once
2 1 0 Only once
1 1 Keep a

The two QoS bits of PUBLISH packets cannot be set to 1 [MQTT-3.3.1-4] at the same time. If the server or client receives an invalid PUBLISH packet with both QoS bits as 1, it uses the DISCONNECT packet containing the cause code 0x81 to close the network connection

The working principle of

QoS 0 – A maximum of once

When QoS is 0, message distribution depends on the capability of the underlying network. The publisher publishes the message only once, the receiver does not reply to the message, and the publisher does not store and resend the message. Messages have the highest transmission efficiency at this level, but may be delivered once or not at all.

Qos 1 – At least once

When the QoS is 1, the message can be delivered at least once. MQTT guarantees QoS 1 through a simple ACK mechanism. The publisher advertises a PUBACK message and waits for the response from the receiver. If the receiver does not receive the PUBACK response within a specified period of time, the publisher sets the DUP of the message to 1 and resends the message. When receiving a message with QoS of 1, the receiver should respond with a PUBACK packet. The receiver may receive the same message several times. Regardless of the DUP flag, the receiver regards the received message as a new message and sends a PUBACK packet to respond.

QoS 2 – Is distributed only once

When QoS is 2, the publisher and subscriber ensure that messages are delivered only once through two sessions, which is the highest level of quality of service, and message loss and repetition are unacceptable. There is an additional overhead associated with using this quality of service level.

After a publisher publishes a message with QoS of 2, it stores the published message and waits for the receiver to reply to the PUBREC message. After the sender receives the PUBREC message, it can safely discard the previous published message because it already knows that the receiver has successfully received the message. The publisher saves a PUBREC message and replies to a PUBREL, waits for the recipient to reply to a PUBCOMP message, and clears the saved state when the sender receives a PUBCOMP message.

When the receiver receives a PUBLISH message with a QoS of 2, it processes the message and replies with a PUBREC. When the receiver receives the PUBREL message, it discards all the saved state and replies to PUBCOMP.

Whenever packet loss occurs during transmission, the sender is responsible for retransmitting the previous message. This is true regardless of Publisher or Broker. Therefore, the receiver also needs to respond to each command message.

Packet ID

The MQTT protocol specifies that each time a message with QoS > 0 is published, an unused non-zero message identifier [MQTT-22.1-4] must be allocated. After the acknowledgement of the packet is processed, the packet identifier is released for reuse. A packet identifier cannot be used by multiple commands at a time.

Publishers and subscribers

MQTT publish message QoS is not end-to-end, it is between client and server. The QoS level at which a subscriber receives an MQTT message ultimately depends on the QoS of the published message and the QoS to which the topic subscribes.

QoS that publishes messages QoS subscribed to by the topic QoS for receiving messages
0 0 0
0 1 0
0 2 0
1 0 0
1 1 1
1 2 1
2 0 0
2 1 1
2 2 2

How to Select QoS

The higher the QoS level is, the more complex the process is and the more system resources are consumed. Applications can select an appropriate QoS level according to their own network scenarios and service requirements. For example, QoS 0 is often used for message interaction between services within the same subnet. QoS 1 is often used in real-time message communication through Internet. QoS 2 uses fewer scenarios and is suitable for more demanding scenarios such as payment requests.


For more information, please visit our official website emqx. IO, or follow our open source project github.com/emqx/emqx. For more details, please visit our official documentation.