Search code examples
mqttconsumerbrokerproducerqos

Clarification on behavior of QoS 1 and 2 if subscriber is unavailable


The general flow of MQTT is: producer ---> MQTT Broker ---> consumer. Assume that the MQTT Broker is always up and running but consumer is temporarily unavailable.

If a producer sends a message to a MQTT broker with QoS 1, does the broker send an acknowledgement back to the producer since the broker did receive the message?

If a producer sends one message to a broker with QoS 1, the broker will keep trying to deliver message to the consumer. If the producer sends more messages, will the broker keep retrying to send the first message to the consumer? Does this mean that messages will be queued up in the broker? How long will the broker retry delivering a message to a consumer before moving on to the next message?

If a producer sends one message to a broker with QoS 2, will the broker keep retrying indefinitely since QoS 2 guarantees exactly one delivery?


Solution

  • If a producer sends a message to a MQTT broker with QoS 1, does the broker send an acknowledgement back to the producer since the broker did receive the message?

    Yes, there is no end-to-end to delivery notification in MQTT. Any handshaking for QoS 1 or 2 messages is always between 1 client and the broker.

    will the broker keep retrying to send the first message to the consumer?

    The broker does not try and send any QoS 1 or 2 messages to an offline client, messages are only sent to online clients with an active subscription to the topic. Messages will be stored for clients with valid subscription until they reconnect at which point they will be delivered.

    How long will the broker retry delivering a message to a consumer before moving on to the next message?

    Messages are queued forever or until the same clientid reconnects. It the client reconnects with the clean session flag set to the true then all queued messages will be discarded. In MQTT v5 there is now way to tell the broker how long to queue a message for before discarding it. (Some brokers can also be configured with a max queuing time at MQTT v3.x but this was not part of the spec)

    If a producer sends one message to a broker with QoS 2, will the broker keep retrying indefinitely since QoS 2 guarantees exactly one delivery?

    This is the same as before, the message will be queued for the client until it reconnects and then delivered. For QoS 2 to work, both the broker and the client are expected to have persistent storage to save state across restarts to track the stages of delivery.