0
0
Drone-programmingComparisonBeginner · 3 min read

MQTT QoS 0 vs QoS 1 vs QoS 2: Key Differences Explained

MQTT 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.

FactorQoS 0QoS 1QoS 2
Delivery GuaranteeAt most once (no guarantee)At least once (possible duplicates)Exactly once (no duplicates)
Message DuplicationPossible loss, no duplicatesPossible duplicatesNo duplicates
AcknowledgmentNo ACKPUBACKPUBREC, PUBREL, PUBCOMP
Network OverheadLowestMediumHighest
Use CaseNon-critical dataImportant data, duplicates okayCritical 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.

python
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()
Output
Message published to 'test/topic' with QoS 0 (no acknowledgment expected)
↔️

QoS 1 Equivalent

Publishing the same message with QoS 1 to ensure delivery at least once.

python
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()
Output
Message published to 'test/topic' with QoS 1 (PUBACK expected, retries if no ACK)
🎯

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.

Key Takeaways

MQTT QoS 0 sends messages once without delivery guarantee or acknowledgment.
QoS 1 ensures messages arrive at least once but may cause duplicates due to retries.
QoS 2 guarantees exactly one delivery with a four-step handshake to avoid duplicates.
Use QoS 0 for non-critical, fast data; QoS 1 for reliable but duplicate-allowed messages; QoS 2 for critical, no-duplicate messages.