Introduction to MQTT (Chapter 3: QoS)

Introduction to MQTT (Chapter 3: QoS)

MQTT Quality of Service

The ability to send messages that are handled in different ways is one of the differentiating factors that set Message Queue Telemetry Transport (MQTT) apart from other communication protocols. This facility is called Quality of Service (QoS) in MQTT. This article will take an in-depth look at what QoS is and how it improves network communication. We will also outline some best practices for determining which of the three QoS levels to use when sending messages between publishers, brokers, and clients.

What is QoS?

Quality of service is an attribute that is assigned to an individual message when it is published. QoS is essentially an agreement between sender and receiver that defines the way in which a message is delivered. It enables clients to select a level of service that takes into account network reliability and programming logic. MQTT is capable of retransmitting messages and guaranteeing delivery so the addition of QoS facilitates communication using unreliable networks.

QoS defines the lengths to which the broker or client will go to ensure a message is received. The initial QoS level is set by the client that is publishing the message before it is sent to the broker. The QoS level can be downgraded by message subscribers. Brokers deliver messages based on the QoS defined by the subscriber, not the publisher.

Here is an overview of the three QoS levels in MQTT.

QoS 0

When the QoS is set to 0, a message is delivered at most one time. It may not be delivered at all. There is no acknowledgment of delivery at this QoS level. The message is not stored and can be lost if the client disconnects or the server fails.

This QoS level is sometimes referred to as “fire and forget” and is the fastest mode of data transfer in an MQTT implementation. Messages published at QoS 0 are not required to be forwarded to a client. A message may be discarded by the server if the intended client is disconnected when the server receives the publication.

QoS 1

QoS 1 is MQTT's default transfer mode and indicates that the message is always delivered at least once. Receipt of the message must be acknowledged. Failure to receive an acknowledgment results in the message being resent with the DUP flag set. This process repeats until the message is acknowledged and can lead to the same message being sent and processed multiple times.

Messages set at QoS 1 are stored locally at the sender and receiver until they are processed. A receiver deletes the message after it has processed it. When the receiver is a broker, the message is published to the appropriate subscribers. Clients that receive a message deliver it to the subscribing application. Upon message deletion, the receiver sends an acknowledgment to the sender. Senders delete a message at QoS 1 after they have accepted confirmation from the receiver.

QoS 2

Messages sent at QoS 2 are always delivered exactly once. It is the most reliable and slowest mode of transfer in an MQTT network. Messages are stored locally at the sender and receiver until they are processed. At least two pairs of transmissions are performed between the sender and receiver before a message is deleted from the sender. The message can be processed by the receiver after the first or second transmission is completed as long as it does not process it more than once.

The first pair of transmissions encompasses the sender transmitting the message and obtaining acknowledgment that the receiver has stored it. If the sender does not receive confirmation, messages are resent with the DUP flag set until an acknowledgment is received. In the second pair of transmissions, the sender informs the receiver it can complete processing the message by sending a PUBREL message. The PUBREL message will be sent repeatedly until its receipt is acknowledged, at which point the sender deletes the message.

When the message receiver is a broker, it performs its function and publishes the message to subscribers. A client receiver delivers the message to the subscriber application. When the receiver is finished processing a message, it sends a completion message to the sender. QoS 2 is used to ensure that messages are not lost or processed more than once.

QoS in Action

QoS 0 provides for best-effort delivery with no guarantee of success. There is no acknowledgment that messages are received, and they are not stored or re-transmitted in the event of a failure.

QoS 1 guarantees that a message is delivered successfully to the receiver at least once. A sender stores a message until the receiver acknowledges receipt by returning a PUBACK packet. The packet identifier of the PUBLISH packet is used in the creation of the PUBACK packet. The PUBLISH packet is resent if the sender does not receive the PUBACK packet in a reasonable time frame.

A four-part handshake is required to complete the processing of messages sent at QoS level 2. The packet identifier of the original PUBLISH message is used to coordinate delivery. The flow of messages conforms to the following pattern.

A receiver gets a PUBLISH packet from a sender and replies with a PUBREC packet acknowledging receipt of the original packet. The absence of a PUBREC packet causes the message to be resent with a DUP flag. When the PUBREC packet is received, the sender can delete the initial PUBLISH packet. The sender then stores the PUBREC packet and replies with a PUBREL packet. Upon receipt of the PUBREL packet, the receiver discards all stored states and answers with a PUBCOMP to the sender. The receiver stores a reference to the packet identified in the original PUBLISH message until the PUBCOMP packet is sent, to avoid processing the message a second time.

Best Practices for Choosing a QoS Level

The point of having multiple QoS levels is that they can be used to address different situations or requirements. Here are some guidelines on when to use each quality of service level.

?     QoS 0 can be used when there is a reliable and stable connection between the sender and receiver, such as over a wired network. The system must be able to tolerate occasional missed messages. There is no message queueing with this level QoS.

?     QoS 1 should be used when every message must get through and duplicates can be handled by the receivers. This is the most commonly used service level since message delivery is guaranteed and it is faster than QoS 2.

?     QoS 2 used when an application needs to ensure it only receives a message once. The overhead of this service level needs to be considered when it is selected, as message processing will be slower than with lower levels.

The availability of different QoS levels enables systems to be fine-tuned based on the requirements of the underlying applications and network reliability. Being able to select how messages are delivered is one of the most appealing features of the MQTT protocol and makes it an excellent choice for IoT and industrial automation implementations.





GARRETT SCHMIDT

Automating the Power Grid of the Future | Wireless & 4G/5G | Industrial Protocols | Master Tinkerer | Ghostbuster

3 年

Mike Nager, Chantal Polsonetti, Guillermo Rybnik, P.Eng, MBA, Ira Sharp Jr we're going further down the rabbit hole!

要查看或添加评论,请登录

GARRETT SCHMIDT的更多文章

社区洞察

其他会员也浏览了