Ir para o conteúdo principal
Ir para o conteúdo principal

Concept

MQTT QoS

MQTT QoS (Quality of Service) defines a message's delivery guarantee at three levels: 0 (at most once), 1 (at least once) and 2 (exactly once).

MQTT QoS (Quality of Service) is the delivery guarantee attached to each MQTT message. There are three levels: QoS 0 delivers a message at most once, QoS 1 at least once and QoS 2 exactly once. Each step up adds acknowledgement packets and stored state, so it costs more traffic and memory in exchange for fewer lost or duplicated messages.

The three MQTT QoS levels

  • QoS 0, at most once: the sender sends a single PUBLISH packet. The receiver sends no acknowledgement and the sender never retries, so the message arrives once or not at all.
  • QoS 1, at least once: the receiver answers with PUBACK. Until then the sender keeps the message and resends it, with the DUP flag set, if the connection drops first. The message arrives, but it can arrive more than once.
  • QoS 2, exactly once: a four-packet exchange (PUBLISH, PUBREC, PUBREL, PUBCOMP) lets both sides agree that the message was delivered once and only once. It is the slowest level and keeps the most state.

The specification gives two examples: QoS 0 for ambient sensor data, where losing one reading doesn't matter because the next one follows soon, and QoS 2 for billing systems, where a lost or duplicated message could lead to a wrong charge.

QoS applies per hop, not end to end

QoS covers each connection separately: from the publisher to the MQTT broker, then from the broker to each subscriber. When the broker forwards a message, it uses the lower of two values: the QoS the message was published with and the maximum QoS granted to that subscription. A message published at QoS 2 reaches a subscriber that subscribed at QoS 0 as a QoS 0 message.

QoS 1 and 2 also depend on the session. If a client reconnects to a persistent session, both sides resend any unacknowledged messages. If it connects with Clean Start, the pending state is discarded.

Which QoS level to use

  • QoS 0 for frequent telemetry, where each reading replaces the previous one.
  • QoS 1 for most events and alarms, provided the receiver can discard duplicates, for example by checking a timestamp or a message ID.
  • QoS 2 when a duplicate has a real cost, such as in counting or billing, and the extra round trips are acceptable.

Frequently asked questions

What are the MQTT QoS levels?+

There are three: QoS 0 (at most once, no acknowledgement), QoS 1 (at least once, duplicates possible) and QoS 2 (exactly once).

Does QoS 2 guarantee end-to-end delivery?+

No. QoS works between a client and the broker on each hop, and the broker delivers at a lower QoS if the subscriber asked for less. Once the receiver acknowledges a message, the protocol makes no promise about what the application does with it.

Which MQTT QoS should I use for IoT sensors?+

For periodic readings, QoS 0 is often enough because the next reading makes up for a lost one. If every message counts, use QoS 1 and drop duplicates on the receiving side.

Related terms

Related articles