What is the Quality of Service?
Quality of service levels is guaranteed between communication participants and the delivery of their messages.
So, for example, we have a client and a broker or a broker and a client and we really talk about the guarantees for sending and receiving messages between these participants and MQTT knows three different quality of service levels.
- QoS level 0 which means at most wants delivery
- QoS level 1 which means at least one delivery
- QoS level 2 which is exactly one’s delivery
But, why do we need QoS levels?
We have TCP, TCP gives us exactly one’s guarantees for as long as the same TCP connection is active.
The problem here with IoT, for example, a car that drives through a tunnel it could happen that you lose the connection and the MQTT guarantees are spend a longer time than the actual TCP connection.
So, a message can have for example an exactly-once delivery guarantee even if you have a device that drops connections frequently.
Quality of service 0
QoS level 0, which means at most wants delivery, this means fire and forget a client publishes something to a broker or the broker to the client and there is no way to get an acknowledge means, we send a raw packet and there is no acknowledge at once. So, a message can be lost for any reason and none of the participants recognize that, has happened.
Quality of service 1
QoS level 1, which means at least one delivery. We have a publisher which sends a message to the broker. The broker would then send a public packet that acknowledges the packet and as soon as a packet is sent to the client. The client knows that this was processed successfully by the broker and if something goes wrong for example, with a connection drop then it could happen that you don’t as a client see a pro bug and then you republish the call to service one message and in the worst case, you would have a duplicate so this means you can guarantee that a message is processed at least once.
Quality of service 2
QoS level 2, which means exactly-once delivery. MQTT makes sure by having a flow of four different MQTT packets. So, what happens, for example, the client wants to send a QoS level 2 message to the broker. So, it will send a publish packet then the broker would respond with a pubrec packet and then the client would send a pubrel packet and then the broker would respond with a pubcomp packet this means if anything goes wrong in between the flow is continued where it left off and makes sure that a message is really processed exactly once.
When to use QoS 0, QoS 1, and QoS 2?
When you don’t need message queueing at all, which means you don’t need the functionality that the message can survive multiple TCP connections if message loss is okay. QoS level 0 is great also for high frequent messages like if you have temperature data published every few seconds if a message is lost probably nobody cares. So, this might be a great way to use quality of service 0 or if you have the bandwidth at the premium because quality 0 really has the least overhead.
Use quality of service level 1 usually as a default. If you don’t know what you need QoS 1 is a default. It’s great for the trade-off between bandwidth and message guarantees.
If you don’t need too much performance or if you can live with lower performance and with a higher bandwidth the QoS 2 exactly one’s delivery used.
Also Read: Introduction of MQTT