MQTT QoS 0 vs QoS 1 vs QoS 2: Key Differences Explained
QoS 0 delivers messages once with no guarantee, QoS 1 ensures delivery at least once but may duplicate messages, and QoS 2 guarantees exactly one delivery with no duplicates using a four-step handshake.Quick Comparison
Here is a quick table comparing MQTT QoS levels by key factors.
| Factor | QoS 0 | QoS 1 | QoS 2 |
|---|---|---|---|
| Delivery Guarantee | At most once (no guarantee) | At least once (possible duplicates) | Exactly once (no duplicates) |
| Message Duplication | Possible loss, no duplicates | Possible duplicates | No duplicates |
| Acknowledgment | No ACK | PUBACK | PUBREC, PUBREL, PUBCOMP |
| Network Overhead | Lowest | Medium | Highest |
| Use Case | Non-critical data | Important data, duplicates okay | Critical data, no duplicates allowed |
Key Differences
QoS 0 is the simplest level where messages are sent once without any acknowledgment. This means messages can be lost if the network fails, but it has the lowest overhead and latency.
QoS 1 adds reliability by requiring the receiver to send a PUBACK acknowledgment. The sender retries until it gets this ACK, so messages arrive at least once but may be duplicated if the ACK is lost.
QoS 2 is the most reliable and complex level. It uses a four-step handshake (PUBREC, PUBREL, PUBCOMP) to ensure the message is delivered exactly once without duplicates. This adds more network traffic and latency but guarantees no message loss or duplication.
Code Comparison
Example of publishing a message with QoS 0 using Python's Paho MQTT client.
import paho.mqtt.client as mqtt client = mqtt.Client() client.connect("broker.hivemq.com", 1883, 60) # Publish with QoS 0 client.publish("test/topic", "Hello QoS 0", qos=0) client.disconnect()
QoS 1 Equivalent
Publishing the same message with QoS 1 to ensure delivery at least once.
import paho.mqtt.client as mqtt client = mqtt.Client() client.connect("broker.hivemq.com", 1883, 60) # Publish with QoS 1 client.publish("test/topic", "Hello QoS 1", qos=1) client.disconnect()
When to Use Which
Choose QoS 0 when you want the fastest delivery and can tolerate message loss, like sensor data that updates frequently.
Choose QoS 1 when message delivery is important but duplicates are acceptable, such as notifications or commands where retry is better than loss.
Choose QoS 2 for critical messages where duplicates cannot be tolerated, like financial transactions or device configuration updates.