What is Quality of Service in MQTT?
- February 20, 2020
- Posted by: Naveen PS
- Category: Learn Technology in 5 minutes
In this blog, we will get a basic overview of What is QoS in MQTT? Why should you care about QoS? And Finally, learn how QoS is implemented in MQTT?
Let’s relate QoS with something that all of us can identify with. Let’s imagine that we are in a traffic jam while going to our workplace. We will regularly be stopping and moving at a slow pace, but eventually, we will reach our destination.
Let’s say, while we are driving along, we hear sirens from the back. We look in our rearview mirror, and we see an ambulance coming. What do we do?
Yes, we all move over for the ambulance to pass. It is not a matter of life or death if we have to add a couple more minutes to our travel time.
The same concept is useful in the world of networking. Basically, all data is not equal. Some data, like voice or video streaming data, needs to reach its destination as soon as possible, while a mail data need not. For such scenarios, we need a technology that controls and manages network resources by setting priorities for specific types of data on the network. This is where Quality Of Service comes into the picture.
We hope that you have already watched our video on MQTT Protocol and also have a basic understanding of the TCP/IP Model. If not, please check out these links to get up to speed.
- What is MQTT Protocol? : https://www.youtube.com/watch?v=Eiw1RdtWlLk
- TCP/IP Model for beginners: http://xahlee.info/linux/tcp_ip_tutorial.html
- In Depth Understanding of TCP/IP Model: https://www.redbooks.ibm.com/redbooks/pdfs/gg243376.pdf
By now, you would know that MQTT is a Machine to Machine connectivity application protocol that runs on top of TCP. Even though TCP provides guaranteed data delivery, data loss can still occur.
This is due to the fact that the Internet Protocol used in the Network layer, like the IPv4, uses a QoS Model called Best Effort Delivery which does not guarantee message delivery nor acknowledgement to the sender on successful delivery. It is similar to a Postal Service.
Thus when the underlying transport of packets are unreliable, which is often the case with IoT Devices, MQTT adds 3 QoS levels in the application layer to ensure re-transmission of messages and guaranteed delivery. This makes QoS one of the most important features of MQTT.
As MQTT is based on the Publish-Subscribe Model, we will need to consider both the Publisher to Broker QoS & Broker to Subscriber QoS. For ease of learning, we will look at QoS Levels from the Publishing perspective.
Now let us take a closer look at the different QoS levels available in MQTT and how they work.
Quality of Service Level 0
QoS 0 is equivalent to the Best Effort Delivery Model, as discussed earlier. In this QoS, there is no guarantee of delivery as there is no Acknowledgment, nor retransmission by the sender. QoS 0 is used in the following two conditions.
- Data is sent at very short intervals, thus loss of some messages are acceptable – Like weather monitoring, Pollution Monitoring, etc
- You have a completely stable MQTT Connection and no chances of disconnected Client- Like connecting to an MQTT broker over Wired Connection.
Quality of Service Level 1
QoS 1 guarantees that a message is delivered at least one time to the Broker. The publisher stores the message until it gets an acknowledgment packet within a predefined time interval. If the time interval is expired, the publisher republishes the message again to the broker. It is possible for a message to be sent or delivered multiple times in QoS 1. So when should you use QoS 1?
- Your application must receive every message provided the application can handle and filter out duplicates. – like in the case of smart attendance system.
- Your application can’t handle the overhead of QoS 2 but needs reliable message delivery.
Quality Of Service Level 2
QoS 2 guarantees that each message is received and only once it is received by the intended client. In this case, the guarantee is achieved by a 4 part handshake between the client and the broker. Because of this, it is the slowest method to send the message.
In this QoS Level, the publisher sends the message to be published to the broker and waits for the acknowledgment. When the publisher receives the acknowledgment message (PUBREC), it then sends another acknowledgment message (PUBREL) back to the broker. When the broker receives this message, it will forward the published message to its subscribers. Next, the broker will send a publish complete message (PUBCOMP) to the publisher. When it receives the message, the process is complete and can delete the message from its outbound queue.
QoS2 is used in critical applications, where duplicate delivery can harm the application users or clients. For example, in accident detection, Aircraft, Oil & Gas plants, etc.
We have now covered the concept of QoS, the importance of this feature and its implementation in MQTT.