Quality of Service (QoS) in MQTT is the level of guarantee of delivery of a specific message. The client sets a QoS level, each time you subscribe to a topic or publish a message to a topic on a broker. There are 3 MQTT QoS levels.

See also:
Fundamentals of MQTT
Guide: Paho, a Python MQTT Client

MQTT QoS Level 0 – At Most Once

When the client sends a message to a broker with the QoS level set to 0, it means the client sends the message only once and the broker does not acknowledge that the message is received. There is no guarantee that the broker receives the message sent by the client.
This service level should used if:

  • The Internet is reliable.
  • Message loss on a small scale does not matter.
  • Messages need to be delivered fast.

To get a better understand of QoS Level 0, read about QoS Level 1 & 2.

MQTT QoS Level 1 – At Least Once

When the client publishes a message with QoS level 1, the broker sends an acknowledgement(PUBACK) message back to the client. This informs the client that the message that the broker has received the message. The client stores the original message until it gets a PUBACK message from the broker. If the client does not receive the PUBACK message, it will resend the original message.

mqtt qos level 1

If a client subscribes to a topic on a broker with QoS level 1 and if a message is published to that topic, then the broker sends that message to all the clients and expects a PUBACK message from the client. This ensures that the subscribing client receives all the messages that is published to that topic.

In both cases, if the sender does not receive a PUBACK message within some amount of time, it will republish the original message to the recipients with a duplicate flag(DUP). The DUP flag is not processed by the client or the broker but can be used based in your program to handle duplicate message.

While QoS level 1 guarantees that the message is being received, it can cause duplicate messages to get republished if the PUBACK is not recieved on time. QoS Level 1 is slower than Level 0.

This service level should used if:

  • The client or broker must receive all messages.
  • Duplicate messages can be handled properly.

MQTT QoS level 1 is used in commercial MQTT brokers like AWS IoT, Azure etc. Most commercial MQTT brokers do not support QoS level 2 as its slow and consumes more resources.

MQTT QoS Level 2 – Exactly Once

MQTT QoS Level 1 can cause duplicate messages. Instead of building a mechanism to handle message duplication, we can use QoS Level 2. This service level guarantees that the message is being received only once!

The steps involved in QoS Level 2 are as follows:

mqtt qos 2

1. When a receiver receives a message with QoS Level 2, it replies with a PUBlish RECeived(PUBREC) message that acknowledges that the message has been received. If the sender does not receive the PUBREC message, it will republish the message with a DUP flag.

2. The sender removes the first publish message after receiving the PUBREC message and sends a PUBlish RELease(PUBREL) message to the receiver.  If the sender does not receive the PUBREL message, it will resend the PUBREL message with a DUP flag.

3. The receiver sends a PUBlish COMplete(PUBCOMP) message to the sender. If the sender does not receive the PUBCOMP message, then the PUBREL message is resent.

4. The sender removes the message from the queue.

QoS Level 2 is much slower than Level 1 or 0 because of the number of steps involved.

This service level should be used if:
  • Messages can be delivered slowly.
  • Message duplication causes issues.
Most commercial brokers like AWS IoT & Azure IoT do not support QoS Level 2 as on this post’s date.

Frequently Asked Questions

If a client publishes with QoS Level 2 and another client is subscribed to that topic with QoS Level 0, which QoS Level will be used?

mqtt qos faq1

The QoS Level is restricted to client to broker or broker to client communication. So if I publish a message with QoS Level 2, the message guarantee is only applicable from THAT client to the broker. If another client is subscribed to a topic on that broker with QoS Level 0, then the broker will send messages to that client only with QoS Level 0.

 

 

 

Does message queuing work with QoS Level 0?

No message queuing will not work when a client publishes to or is subscribed to a topic with QoS Level 0. 

See also:
Fundamentals of MQTT
Guide: Paho, a Python MQTT Client