MQTT QoS Levels Explained (QoS 0, QoS 1, and QoS 2)
Introduction
Imagine an ESP32 monitoring the temperature in your home.
Every few seconds, it publishes sensor readings to an MQTT broker. A Node-RED dashboard subscribes to the topic and displays the data in real time.
But what happens if the Wi-Fi connection briefly drops?
Will the message be lost?
Will it be delivered again?
Can the broker guarantee delivery?
These are important questions when building IoT systems, especially when reliability matters.
This is where MQTT Quality of Service (QoS) comes into play.
QoS defines how messages are delivered between MQTT clients and the broker. It allows developers to choose the right balance between reliability, bandwidth usage, and performance.
In this tutorial, you’ll learn the three MQTT QoS levels, how they work, their advantages and disadvantages, and when to use each one in real-world IoT projects.
What is MQTT QoS?
QoS stands for Quality of Service.
It is a message delivery mechanism that determines how reliably an MQTT message is delivered from a publisher to a subscriber.
MQTT provides three QoS levels:
- QoS 0 — At Most Once
- QoS 1 — At Least Once
- QoS 2 — Exactly Once
Each level offers a different balance between:
- Reliability
- Network traffic
- Processing overhead
- Message delivery guarantees
Choosing the correct QoS level is important for creating efficient and dependable IoT applications.
Why Does MQTT Need QoS?
IoT devices often operate on:
- Wi-Fi networks
- Cellular networks
- Low-power wireless networks
- Unstable internet connections
Messages can sometimes be:
- Lost
- Delayed
- Duplicated
- Interrupted during transmission
QoS helps ensure that critical messages reach their destination as expected.
For example:
Temperature Sensor
Missing one reading may not be a problem.
Door Lock Command
Losing a lock or unlock command could be a serious issue.
Different situations require different levels of reliability.
MQTT QoS Levels Overview
| QoS Level | Delivery Guarantee | Possible Duplicate Messages | Network Overhead |
|---|---|---|---|
| QoS 0 | At Most Once | No | Lowest |
| QoS 1 | At Least Once | Yes | Medium |
| QoS 2 | Exactly Once | No | Highest |
Let’s examine each level in detail.
QoS 0 – At Most Once
QoS 0 is the simplest MQTT delivery method.
The publisher sends the message once and does not expect any acknowledgment from the broker.
Communication flow:
Publisher
↓
Broker
If the message is successfully delivered, great.
If the message is lost, MQTT does not attempt to resend it.
Characteristics
- Fastest delivery
- Lowest bandwidth usage
- No acknowledgment packets
- No retransmission
- Message loss is possible
Example
ESP32 publishes: temperature = 25.4°C
If that reading is lost due to a temporary network issue, the next reading will arrive shortly afterward.
For many sensor applications, this is acceptable.
Typical Use Cases
- Temperature monitoring
- Humidity monitoring
- Environmental sensors
- Weather stations
- Non-critical telemetry
ESP32 Example
client.publish( "home/temperature", "25.4", false );
Default MQTT publishing typically uses QoS 0.
Advantages
- Fastest performance
- Minimal network traffic
- Low memory usage
Disadvantages
- Messages may be lost
- No delivery confirmation
QoS 1 – At Least Once
QoS 1 guarantees that the broker receives the message at least once.
When a publisher sends a message:
- Publisher sends message.
- Broker receives message.
- Broker sends acknowledgment (PUBACK).
- Publisher confirms delivery.
Communication flow:
Publisher --> Message --> Broker <-- PUBACK
If the acknowledgment is not received, the publisher sends the message again.
Characteristics
- Guaranteed delivery
- Message may be duplicated
- Moderate network overhead
Example
ESP32 sends:
Relay = ON
If the acknowledgment is lost:
ESP32 sends again
The subscriber may receive:
Relay = ON
Relay = ON
twice.
Applications must be able to handle duplicate messages.
Typical Use Cases
- Relay control
- Smart home commands
- Device status updates
- Alarm notifications
- Configuration updates
Advantages
- Reliable delivery
- Suitable for most IoT applications
Disadvantages
- Duplicate messages are possible
- Additional bandwidth required
QoS 2 – Exactly Once
QoS 2 provides the highest level of reliability.
A message is guaranteed to arrive exactly once.
Neither loss nor duplication is allowed.
To achieve this, MQTT uses a four-step handshake.
Communication flow:
Publisher -> PUBLISH -> Broker -> PUBREC -> PUBREL <- PUBCOMP
Only after the complete process finishes is the message considered delivered.
Characteristics
- Highest reliability
- No duplicate messages
- Highest network overhead
- More processing required
Example
Consider a payment system:
Charge Customer = $100
Receiving this command twice would be unacceptable.
QoS 2 ensures it is processed only once.
Typical Use Cases
- Financial transactions
- Billing systems
- Industrial automation
- Critical control systems
- High-value operations
Advantages
- Guaranteed single delivery
- Maximum reliability
Disadvantages
- Highest bandwidth usage
- Increased latency
- More processing overhead
Visual Comparison of QoS Levels
Reliability increases as the QoS level increases, but so does network traffic.
Which QoS Level Should You Use?
Use QoS 0 For
- Sensor readings
- Weather data
- Telemetry
- Frequently updated values
Examples:
Temperature Humidity Pressure Battery Voltage
Use QoS 1 For
- Home automation commands
- Device status updates
- Notifications
- Relay control
Examples:
Turn Light ON Turn Fan OFF Open Gate Close Door
Use QoS 2 For
- Critical industrial operations
- Financial systems
- High-value transactions
Examples:
Machine Shutdown Payment Processing Security Operations
Most IoT projects use QoS 0 or QoS 1.
QoS 2 is rarely needed in typical ESP32 and Raspberry Pi projects.
QoS in ESP32 Using PubSubClient
Publish using QoS 0: client.publish( "home/temperature", "25.5" );
The PubSubClient library primarily supports QoS 0 publishing.
For advanced QoS features, developers often use:
- ESP-IDF MQTT Client
- Async MQTT libraries
- Other MQTT client implementations
QoS and Node-RED
Node-RED allows you to select QoS directly inside MQTT nodes.
Available options:
0 = At Most Once 1 = At Least Once 2 = Exactly Once
Simply edit the MQTT node and choose the desired QoS level from the configuration menu.
Common Beginner Mistakes
Using QoS 2 Everywhere
Many beginners assume QoS 2 is always better.
In reality:
- It increases network traffic.
- It increases latency.
- It consumes more resources.
Most IoT projects do not need QoS 2.
Using QoS 0 for Critical Commands
Commands such as:
Unlock Door Emergency Stop Disable Alarm
should not rely on QoS 0 because messages can be lost.
Ignoring Duplicate Messages
QoS 1 can deliver duplicate messages.
Applications should be designed to handle repeated commands safely.
QoS and Retained Messages
QoS and retained messages are different MQTT features.
QoS
Determines how reliably a message is delivered.
Retained Message
Determines whether the broker stores the latest message.
You can use both together.
Example:
Retained = True QoS = 1
The broker stores the message and guarantees delivery.
Conclusion
MQTT QoS levels allow developers to choose the appropriate balance between performance and reliability. While QoS 0 provides the fastest communication, QoS 1 offers reliable delivery for most IoT projects, and QoS 2 guarantees exactly-once delivery for critical systems.
Understanding when to use each QoS level is essential for building efficient, reliable, and scalable MQTT-based applications.
For most ESP32, Raspberry Pi, and Node-RED projects, QoS 0 and QoS 1 will cover nearly every requirement.
Frequently Asked Questions
What does QoS mean in MQTT?
QoS stands for Quality of Service and defines how reliably MQTT messages are delivered.
Which QoS level is fastest?
QoS 0 is the fastest because it does not require acknowledgments.
Which QoS level is most commonly used?
QoS 1 is commonly used because it provides reliable delivery with reasonable overhead.
Does QoS 1 guarantee no message loss?
Yes, but duplicate messages may occur.
Does QoS 2 prevent duplicate messages?
Yes. QoS 2 guarantees that a message is delivered exactly once.
Should I use QoS 2 for ESP32 projects?
In most cases, no. QoS 0 or QoS 1 is usually sufficient for typical IoT applications.
Related MQTT Tutorials:
- What is MQTT? A Complete Beginner’s Guide
- Install Mosquitto MQTT Broker
- Raspberry Pi MQTT Broker Setup
- ESP32 MQTT Publisher and Subscriber
- Node-RED MQTT Dashboard
- MQTT Security with TLS: Secure Mosquitto MQTT Broker
- MQTT-Based Smart Home Automation Using ESP32 and Node-RED
- MQTT Retained Messages Explained with Examples
- Smart Room Temperature Monitor Using ESP32, DHT22, MQTT and Node-RED
- MQTT Learning Path
