MQTT/Delivery/QoS 2, and each hop

QoS 2, and each hop

QoS 2 delivers exactly once and pays four packets for it, where QoS 0 pays one. And QoS is agreed hop by hop — a subscriber gets the lower of the level a message was posted at and the level it asked for.

QoS 2 is the only level that promises exactly once: never lost, never twice. It gets there with four packets for every message.

Four trips

  1. PUBLISH — the message.
  2. PUBREC — "received", from the receiver, which now holds it and will not pass on a second copy.
  3. PUBREL — "release", from the sender, which can now forget it.
  4. PUBCOMP — "complete", from the receiver.

After the second packet the receiver knows it has the message; after the fourth both sides know that the other knows. If the connection drops anywhere in the middle, a client that reconnects with its session kept picks up where it left off, and the message is never handed on twice.

Almost nothing needs this. QoS 1 with a command that is safe to receive twice does the same job in half the packets, and is easier for a small board to get right.

Two hands signing two halves of the same receipt across a counter
Exactly once: both sides sign, and both sides know the other has.Generated illustration

Each hop has its own QoS

A message travels in two hops — sender to broker, then broker to each subscriber — and each hop has its own QoS:

  • the sender chooses the QoS for the first hop when it posts;
  • each subscriber chooses the highest QoS it wants when it subscribes;
  • the broker delivers at the lower of the two.

Post at QoS 1 to a phone that subscribed at QoS 0, and the phone gets an ordinary letter. For careful delivery all the way, both ends have to ask for it.

Common mistakes

  • Setting QoS 2 on the sensor and nothing on the subscriber. The second hop is QoS 0, and the careful first hop bought nothing.
  • QoS 2 as a default. Four packets for every message, for a guarantee almost no reading needs.
  • Assuming the library supports it. Some small-board libraries publish at QoS 0 only, whatever you ask. Check before relying on a level.

Edit this page — content/fundamentals/mqtt/qos-2.mdx

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Fundamentals on the forum →