Communications

MQTT : Quality of Service ( QoS )

2 min read
iotbasics
MQTT

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

QoS 0

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" 5.png

QoS 1

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. 6.png

QoS 2

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. 7.png

Example message with QoS

example-qos.png

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!

More Posts

Git commit best practices
General

This post explains how to write good commit messages and how not to write bad commit messages. Also, you will learn some more best practices...

What is an RTOS?
Device Software

RTOS or a Real-Time Operating system is an operating system usually running on an embedded device that can run time-constrained applications...

Generic IoT device architecture
Device Hardware

In this article, you will learn about the generic Internet of Things device architecture. Certain specific components are necessary to consi...