This article is a continuation of the article IoT basics: Introduction to MQTT Protocol
Quality of Service (QoS) in MQTT messaging is an agreement between sender and receiver on the guarantee of delivering a message. As I had already made a post about this on my Instagram page @sudoc.dev, I will mostly use images in this blog article.
There are 3 levels of Quality of Service
- QoS 0 - at most once
- QoS 1 - at least once
- QoS 2 - exactly once
This is the simplest one. The minimal quality of service. This does not guarantee message delivery. it is often referred to as "fire and forget"
With this quality of service, the message sending is guaranteed. When the client sends the message, the receiver/broker sends the acknowledgment. However, if acknowledgment is not received properly for some reason, the client does not know if the message was actually sent, and retries to send the message. So, there might be a possibility that the message can end up in the broker more than once.
It provides the best quality of service. To put it in a simple way, there is a handshake between sender and receiver. This confirms that the acknowledgment is received and the message is sent exactly once. So, with this QoS level, the message sending is guaranteed as well as one can be sure that the message is only sent once.
Example message with QoS
When to use what QoS
QoS 0 can be used when you are testing your setup with a test client. Also, if you are sure about a stable connection between sender and receiver, you can use QoS 0.
QoS 1 can be used when you do not need overhead and can handle duplicate messages. If it is not a highly critical application, it is a good idea to use QoS 1.
QoS 2 can be used in highly critical applications where you have to send/receive the messages exactly once. However, one should also consider the overhead. In many cases, it is better to use QoS 1 and handle duplicate messages. It is more effective.
More articles on MQTT are on the way. Stay tuned!