22  MQTT QoS Levels

In 60 Seconds

MQTT’s three QoS levels define the message handshake complexity: QoS 0 sends a single PUBLISH with no acknowledgment, QoS 1 adds a PUBACK for confirmed delivery (2 messages), and QoS 2 uses a 4-step handshake (PUBLISH/PUBREC/PUBREL/PUBCOMP) guaranteeing exactly-once delivery. Choosing QoS 0 over QoS 2 for battery-powered sensors can extend battery life from 6 days to over 500 days.

22.1 Learning Objectives

By the end of this chapter, you will be able to:

  • Analyze QoS Handshakes: Trace the message flow for QoS 0, 1, and 2 including all acknowledgment packets
  • Calculate Battery Impact: Quantify the power consumption differences between QoS levels using energy-per-message formulas
  • Select Optimal QoS: Apply systematic decision frameworks to choose the correct QoS level for any IoT message type
  • Diagnose Configuration Errors: Identify and prevent QoS mismatch and session configuration errors in production deployments
  • Evaluate Trade-offs: Assess the cost, latency, and reliability implications of each QoS level for a given deployment scenario

MQTT offers three QoS levels to match different needs. QoS 0 is like shouting a message across a room – fast but unreliable. QoS 1 is like sending a text and waiting for a thumbs up. QoS 2 is like a signed, witnessed contract – nothing is missed or duplicated. Each level costs more network resources but provides stronger guarantees.

“Let me show you each QoS level in action!” said Max the Microcontroller. “Sammy, send a temperature reading at QoS 0.” Sammy transmitted: “25.3 degrees!” Max nodded. “Done. One message sent. If the broker was busy and missed it – oh well, gone forever. That’s QoS 0: at most once.”

“Now QoS 1,” said Sammy, sending again. This time the broker replied with PUBACK. “See? The broker confirmed it arrived,” explained Max. “But what if the PUBACK got lost? Sammy would think the broker missed it and resend. Now the broker has TWO copies. That’s QoS 1: at least once – guaranteed delivery, possible duplicates.”

“For QoS 2, watch carefully,” said Lila the LED. The exchange went: PUBLISH, PUBREC, PUBREL, PUBCOMP. “Four messages, but the two-phase handshake makes it impossible to get duplicates. The broker and client agree on the exact state at each step. That’s QoS 2: exactly once.”

Bella the Battery concluded: “QoS 0 for weather data. QoS 1 for alarm triggers. QoS 2 for financial transactions. Match the level to the stakes!”

Foundations:

Deep Dives:

Hands-On:

22.2 Quality of Service (QoS) Levels

Time: ~15 min | Level: Advanced | Reference: P09.C25.U01

Key Concepts

  • QoS 0 (At Most Once): Fire-and-forget delivery with no acknowledgment — lowest overhead, message loss possible
  • QoS 1 (At Least Once): ACK-based delivery ensuring message arrives at least once — duplicate possible if PUBACK lost
  • QoS 2 (Exactly Once): Four-message handshake (PUBLISH/PUBREC/PUBREL/PUBCOMP) guaranteeing exactly one delivery
  • PUBACK: Broker acknowledgment for QoS 1 publish — triggers publisher to remove message from retry queue
  • QoS Downgrade: Broker silently delivers at subscriber’s QoS level if lower than publisher’s — common source of unexpected behavior
  • In-flight Messages: QoS 1/2 messages awaiting acknowledgment — window size limits concurrent unacknowledged messages
  • Message Ordering: MQTT guarantees ordered delivery within a client session but not across sessions or publishers

MQTT provides three QoS levels that balance reliability, performance, and resource consumption. Each level offers different delivery guarantees and overhead characteristics.

22.3 QoS Message Flow Comparison

The following diagrams illustrate the message handshake patterns for each QoS level, showing how reliability increases with complexity:

QoS 0/1/2 message flows

Sequence diagram comparing MQTT QoS 0, 1, and 2 message flows between publisher, broker, and subscriber. QoS 0 shows single PUBLISH with no acknowledgment. QoS 1 shows PUBLISH followed by PUBACK acknowledgment from broker and subscriber. QoS 2 shows four-way handshake with PUBLISH, PUBREC, PUBREL, and PUBCOMP messages ensuring exactly-once delivery.
Figure 22.1: MQTT QoS level message flows showing increasing reliability guarantees. QoS 0 provides no acknowledgment (fastest, may lose messages), QoS 1 uses PUBACK for confirmed delivery (may create duplicates if ACK is lost), and QoS 2 employs a four-way handshake (PUBREC->PUBREL->PUBCOMP) ensuring exactly-once delivery with no duplicates.

22.4 QoS 0: At Most Once (Fire and Forget)

QoS 0 fire-and-forget flow

Sequence diagram showing MQTT QoS 0 message flow. Publisher sends PUBLISH to Broker, Broker forwards PUBLISH to Subscriber. No acknowledgment messages are exchanged. Note indicates message may be lost.
Figure 22.2: QoS 0 message flow showing fire-and-forget delivery. The publisher sends a single PUBLISH message to the broker, which forwards it to subscribers. No acknowledgment is exchanged, making this the fastest but least reliable option.

Use case: Sensor data where occasional loss is acceptable (temperature readings every 10 seconds).

QoS 0’s “fire and forget” approach minimizes overhead. For a typical IoT sensor:

\[\text{Message overhead} = \frac{\text{Protocol headers}}{\text{Total message size}}\]

For a 10-byte temperature reading with QoS 0:

\[\text{Total} = 10_{\text{payload}} + 2_{\text{MQTT fixed}} + 14_{\text{variable}} = 26 \text{ bytes}\]

\[\text{Overhead ratio} = \frac{16}{26} = 62\%\]

This means 38% of each transmission carries actual data. With 288 messages per day (every 5 minutes), that is 7,488 bytes per day. Compare to QoS 2 which would send 4 messages per reading, increasing daily transmission to 29,952 bytes – a 4x increase that directly impacts battery life.

22.5 QoS 1: At Least Once (Acknowledged Delivery)

QoS 1 acknowledged delivery

Sequence diagram showing MQTT QoS 1 message flow. Publisher sends PUBLISH to Broker, Broker responds with PUBACK. Broker sends PUBLISH to Subscriber, Subscriber responds with PUBACK. Note indicates guaranteed delivery but may duplicate if ACK lost.
Figure 22.3: QoS 1 message flow showing acknowledged delivery. The publisher sends PUBLISH and waits for PUBACK acknowledgment from the broker. The broker then forwards to subscribers who also acknowledge. If PUBACK is lost, the publisher retransmits, potentially causing duplicates.

Use case: Commands where delivery matters but duplicates are harmless (motion sensor alerts, door open/close events).

22.6 QoS 2: Exactly Once (4-Way Handshake)

QoS 2 exactly-once handshake

Sequence diagram showing MQTT QoS 2 four-way handshake. Publisher sends PUBLISH to Broker, Broker responds PUBREC, Publisher sends PUBREL, Broker responds PUBCOMP. Note indicates guaranteed exactly once delivery with highest overhead.
Figure 22.4: QoS 2 message flow showing the four-way handshake between publisher and broker. PUBLISH initiates the exchange, PUBREC confirms receipt, PUBREL signals the publisher is releasing the message, and PUBCOMP confirms completion. This same handshake occurs between broker and subscriber.

Use case: Financial transactions, billing events, smart lock commands, medication dispensers.

22.7 QoS Comparison Summary

QoS Messages Guarantee Overhead Use When
0 1 May lose Minimal Loss OK (frequent telemetry)
1 2 At least once Low Duplicates OK (alerts)
2 4 Exactly once High Critical data (commands)
Knowledge Check: QoS Summary Quick Check

22.8 Interactive QoS Impact Calculators

Use these calculators to quantify how QoS level affects battery life, bandwidth, cost, and latency for your IoT deployment.

22.8.1 Battery Life Calculator

How to use: Adjust battery capacity and transmit interval, then compare QoS levels to see battery life impact.

22.8.2 Message Overhead Calculator

How to use: Set your payload size and message frequency to see bandwidth consumption across QoS levels.

22.8.3 Monthly Cost Calculator

Cost comparison: QoS 2 typically costs 4× more than QoS 0 for the same number of messages.

22.8.4 Latency Calculator

How to use: Adjust network RTT (typical: 20-50ms LAN, 100-300ms cellular) and processing delay to estimate total latency.

Tradeoff: QoS 0 vs QoS 1 vs QoS 2

Decision context: When choosing MQTT Quality of Service level for your IoT messages

Factor QoS 0 QoS 1 QoS 2
Battery impact Low (1 message) Medium (2 messages) High (4 messages)
Bandwidth Minimal Low Moderate
Latency Lowest Low Higher
Reliability Fire-and-forget At-least-once delivery Exactly-once delivery
Duplicates No (may lose) Possible (if ACK lost) Never
Message loss Possible No No
Complexity Simplest Simple ACK handling 4-way handshake

Choose QoS 0 when:

  • Data is sent frequently (every few seconds) and missing one reading is acceptable
  • Battery life is critical on constrained devices
  • Telemetry data where the next value replaces the previous (temperature, humidity)
  • High-volume sensor networks where some loss is tolerable

Choose QoS 1 when:

  • Every message should be delivered but duplicates are harmless
  • Alert/event notifications (motion detected, door opened)
  • Log entries or metrics where idempotent processing handles duplicates
  • Moderate reliability needed without extreme battery drain

Choose QoS 2 when:

  • Duplicates would cause problems (billing events, unlock commands)
  • Critical actuator commands (smart lock, valve control, medication dispenser)
  • Financial transactions or one-time triggers
  • Message ordering and exactly-once semantics are required

Default recommendation: QoS 0 for telemetry (90% of IoT traffic), QoS 1 for events/alerts, QoS 2 only for critical commands where duplicates cause harm

The Misconception: Higher QoS numbers mean better quality, so always use QoS 2 for important data.

Why It’s Wrong:

  • QoS 2 requires 4 messages (vs 1 for QoS 0) = 4x bandwidth
  • Each message needs acknowledgment = higher latency
  • Broker must store message state = more memory
  • For 1000 sensors sending every minute: QoS 2 = 4M messages/hour vs 1M for QoS 0

Real-World Example:

  • Temperature sensor sending every 5 minutes
  • Missing one reading is harmless (interpolate from neighbors)
  • QoS 2 overhead: 28% more bandwidth, 4x broker load
  • Result: Wasted resources for no benefit

The Correct Understanding:

  • QoS 0: Acceptable loss (most sensor data)
  • QoS 1: Delivery matters, duplicates OK (alerts)
  • QoS 2: Exactly-once critical (payments, door locks)

Match QoS to actual requirements, not perceived importance.

22.8.5 MQTT QoS Cost Analysis by IoT Scenario

Choosing the wrong QoS level has measurable cost. This table quantifies the impact for common IoT deployments:

Scenario Messages/day Recommended QoS Cost of Using QoS 2 Instead Correct Choice
Temperature sensor (5-min interval) 288 QoS 0 4x bandwidth (1,152 msgs), 3x battery drain Loss of 1 reading = interpolate from neighbors
Door open/close events ~50 QoS 1 2x messages (200 vs 100), negligible benefit Duplicate “door opened” is harmless; missing it is not
Smart lock unlock command ~10 QoS 2 Already optimal Duplicate unlock = security risk; missed unlock = user locked out
Industrial pressure alarm ~5 QoS 1 2x overhead for no gain (alarms are idempotent) Receiving same alarm twice is fine; missing it is dangerous
Electricity meter billing 1 QoS 2 Already optimal Duplicate billing event = overcharge customer
Connection: MQTT QoS meets LoRaWAN Confirmed/Unconfirmed Messages

LoRaWAN solves the same reliability problem as MQTT QoS but at the network layer. LoRaWAN unconfirmed messages are analogous to MQTT QoS 0 (fire-and-forget), while confirmed messages resemble QoS 1 (acknowledged delivery). However, LoRaWAN has no QoS 2 equivalent – exactly-once semantics must be handled at the application layer. See LoRaWAN Architecture for confirmed vs unconfirmed message details.

22.9 QoS Decision Tree

This decision flowchart provides an alternative approach to selecting the appropriate MQTT QoS level based on application requirements:

QoS decision flowchart
Figure 22.5: MQTT QoS decision flowchart guiding selection based on message loss tolerance, duplicate handling, device power constraints, and data frequency. QoS 0 suits frequent, loss-tolerant telemetry from battery devices. QoS 1 handles important alerts where duplicates are acceptable. QoS 2 is reserved for critical commands requiring exactly-once delivery.

22.10 Interactive: MQTT QoS Visualizer

Explore the differences between MQTT QoS levels interactively. Select a QoS level and click “Send Message” to see the message flow animation, including potential message duplication (QoS 1) and the latency trade-off (QoS 2).

How to use this visualizer:

  1. Select a QoS level using the radio buttons above
  2. Click “Send Message” to see the animated message flow
  3. Observe the differences: QoS 0 is fastest (1 message), QoS 1 adds acknowledgment (2 messages), QoS 2 uses a 4-way handshake (4 messages)
  4. Watch for duplicates: QoS 1 may show a duplicate warning (30% chance) demonstrating the “at least once” behavior
  5. Compare the metrics table to understand trade-offs between reliability, latency, and message overhead
QoS Doesn’t Guarantee End-to-End Delivery

A common misconception: QoS 2 means the message reaches the subscriber. Wrong! QoS only guarantees delivery between publisher-to-broker and broker-to-subscriber independently. If the subscriber is offline, QoS 2 ensures the broker stores the message (with Clean Session=false), but the subscriber might never come back online. For true end-to-end confirmation, implement application-level acknowledgments where the subscriber publishes a response message confirming it processed the data.

Knowledge Check: QoS Levels Quick Check

Concept: MQTT supports three Quality of Service (QoS) levels that balance reliability, performance, and network overhead. Choosing the right QoS level is crucial for IoT system design.

Concept Matching: Match each MQTT QoS term or concept to its correct definition or use case.

Process Ordering: Arrange the steps of a complete QoS 2 message delivery (publisher to broker leg) in the correct sequence.

22.12 Summary

This chapter covered the technical details of MQTT Quality of Service levels:

  • QoS 0 uses a single message with no acknowledgment, providing the fastest and most battery-efficient delivery at the cost of potential message loss
  • QoS 1 adds a PUBACK acknowledgment ensuring delivery, but retransmissions when ACKs are lost can create duplicates
  • QoS 2 uses a four-way handshake (PUBLISH->PUBREC->PUBREL->PUBCOMP) guaranteeing exactly-once delivery at the highest overhead cost
  • Decision frameworks help systematically select QoS: loss acceptable = QoS 0, duplicates OK = QoS 1, exactly-once required = QoS 2
  • Production checklist: Evaluate each topic individually – most deployments use QoS 0 for 70-80% of topics
  • Battery impact is significant: QoS 2 uses 4x more messages than QoS 0, reducing battery life proportionally
  • End-to-end delivery is not guaranteed by QoS alone; subscriber must be online or use persistent sessions

22.13 What’s Next

Chapter Focus Why Read It
MQTT Session Management Session persistence, Clean Session flag, offline message queuing Understand how QoS 1/2 messages are stored for offline clients and how session type interacts with QoS level
MQTT QoS Worked Examples Real-world QoS selection across IoT domains Apply the decision frameworks from this chapter to concrete scenarios: smart agriculture, industrial control, healthcare
MQTT Fundamentals MQTT architecture, publish-subscribe pattern, broker roles Review the foundational concepts that underpin QoS behaviour if any of the handshake explanations felt unfamiliar
MQTT Labs and Implementation Hands-on ESP32 MQTT projects with QoS experiments Measure real battery and latency differences between QoS levels on physical hardware using a Wokwi simulator
CoAP Fundamentals and Architecture CoAP CON vs NON messages, reliability mechanisms Compare MQTT QoS with CoAP’s confirmable/non-confirmable approach to see how different protocols solve the same reliability problem
LoRaWAN Architecture LoRaWAN confirmed vs unconfirmed uplinks See how the QoS trade-off (reliability vs overhead vs battery) plays out at the network layer in a constrained LPWAN context