Fundamental Features of MQTT

Prerequisite – Message Queue Telemetry Transport Protocol (MQTT)

Quality of Service (QoS) Levels :
Quality of Service is a level of agreement between the client and the broker. This level helps the clients to ensure the reliability for the delivery of their message.

There are 3 QoS levels :

  • QoS 0: At most Once –
    When message is published with QoS level 0, the message will be delivered at most once. In other words, there is no guarantee that the message will be delivered or not. If delivered, then it will be delivered maximum once. In this QoS level, the sender publishes the message only once and then discards the message. There is no waiting for acknowledgments in this scenario. Hence if the network goes down, the message might not be delivered. If the network is stable the message will be delivered but only once.


    Figure – QoS level 0



    When to use ?

    • While developing applications that can afford the loss of few messages.
    • While developing applications that have reliable underlying network so that the messages published only once get delivered successfully.

    In the above figure, the Broker simply receives single PUBLISH message from the publisher client.


  • QoS 1: At least once –
    When message is published with QoS level 1, the message will be delivered at least once.
    In this QoS level, the sender publishes the message and also stores it until an acknowledgment is received from the receiver. Once an acknowledgment is received, the sender will discard the message. If the acknowledgment is not received in time, the message will be sent again. Hence in this QoS level, the message can be delivered, once and sometimes even more than once, to ensure its successful delivery.


    Figure – QoS level 1

    When to use ?

    • While developing applications that can not afford the loss of messages.
    • While developing applications that can handle the delivery of duplicate messages.
    • While developing applications that do not want additional communication costs.
    • While developing applications that do not have very reliable underlying network and hence require reliability measures to be implemented at the protocol level.



    In the above figure, the Broker receives PUBLISH message and sends back PUBACK to the publisher client.


  • QoS 2: Exactly once –
    When message is published by the client with QoS level 2, the message will be delivered exactly once. In this QoS level, the sender publishes the message, stores it, and waits for PUBREC. The receiver after receiving the message sends PUBREC to the sender. PUBREC is an indication that the message has been successfully delivered to the receiver. Hence after receiving PUBREC, the message will be discarded by the sender. It then sends PUBREL to the receiver. On receiving PUBREL, the receiver discards the saved states and sends PUBCOMP. When the sender receives the final PUBCOMP, it will also discard the previously saved states. Hence in QoS level 2, the additional messages used for communication ensure the successful delivery of the message exactly once.


    Figure – QoS level 2

    When to use ?

    • While developing applications that require every message to be delivered exactly once.
    • While developing applications that can afford the additional communication cost.
    • While developing applications that do not have very reliable underlying network and hence require reliability measures to be implemented at the protocol level.

    In the above figure, the Broker receives PUBLISH, sends PUBREC, receives PUBREL, and then sends PUBCOMP to the publisher client.

  • The distinction between Publisher and Subscriber QoS Levels :
    The QoS level for the message communication between the publisher client (sender) and the broker (receiver) will be decided by the level with which the message was published. The QoS level for the message communication between the broker (sender) and subscriber client (receiver) will be decided by the level with which the client has subscribed to the topic.

    The overall QoS level of communication is equal to the minimum of the two QoS levels on either side of the broker. This overall level determines the guarantee of message delivery between the publisher client and subscriber client.



    Publisher Client QoS Subscriber Client QoS Overall Qos for the Communication
    0 0 0
    0 1 0
    0 2 0
    1 0 0
    1 1 1
    1 2 1
    2 0 0
    2 1 1
    2 2 2

    Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


    Article Tags :
    Practice Tags :


    1


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.