Python MQTT Test Shows QoS 1 Needs Flow Control Under Link Failure
A paho-mqtt test with 300 readings per run found that QoS alone did not prevent loss when a broker’s in-flight message limit was exceeded. An application-level window delivered all readings in the reported QoS 2 run.

Key takeaways
In the detailed failure run, QoS 0 lost 92 of 300 readings, while QoS 1 delivered all unique readings but added 20 duplicates.
Uncontrolled QoS 2 replay lost 51 readings after the broker returned 51 `Quota exceeded` replies.
An application flow-control window produced no loss with QoS 1 and exactly-once delivery in the reported QoS 2 run.
A boot identifier, sequence number and SQLite primary key let the collector recognize redelivered readings and distinguish device restarts.
A Python MQTT telemetry test shows that delivery guarantees depend on more than the QoS number. When a connection first stopped acknowledging messages and then failed completely, the broker’s limit on unfinished messages shaped the result as strongly as the selected QoS level.
The experiment used the same publisher five times, sending 300 temperature readings in each run. In the detailed run, QoS 0 delivered 208 readings and lost 92. It produced no duplicates, but that outcome is consistent with its at-most-once delivery model.
QoS 1 delivered 320 messages containing 300 unique readings. It lost nothing, but 20 messages were duplicates. The source’s broader results show that the exact counts can shift with reconnect timing, while the QoS 1 duplicate count remained 20 in the reported runs.
QoS 2 performed unexpectedly poorly in the unmodified test: 249 messages arrived, 51 readings were missing, and 51 Quota exceeded replies were returned. The source attributes this to the client replaying its entire backlog after reconnecting. Mosquitto allowed only 20 unfinished QoS 1 or QoS 2 messages per client, so excess messages were refused, while the client did not inspect that reason code.
Application flow control changes the result
The test then added an application-level window to limit how many data messages could remain unacknowledged. With QoS 1, all 300 unique readings arrived, with 19 duplicates and no loss. With QoS 2, all 300 arrived exactly once in the reported run, with no duplicates or loss.
The source says the flow-control approach keeps the outstanding data below the broker’s allowance. Its implementation reads ReceiveMaximum from the connection response and reserves capacity for a status message, keeping at most 19 data messages plus one status message unacknowledged.
Design details that support recovery
The example uses paho-mqtt sensors, Mosquitto and SQLite. Sensors publish JSON readings and retained status messages; a collector subscribes to home/flat1/# and stores data locally. Each payload includes a device identifier, boot value, sequence number and device timestamp.
SQLite uses (device, boot, seq) as its primary key with INSERT OR IGNORE, allowing redelivered messages to be discarded. The boot value matters because a device restarts its sequence at 1 after reboot. The sensor also publishes a retained QoS 1 offline Last Will and a retained online status after connecting.
For paho-mqtt 2.x, the source uses CallbackAPIVersion.VERSION2, whose callbacks receive reason codes and properties for MQTT 3.1.1 and MQTT 5. VERSION1 remains available but is deprecated. The project includes experiments and 18 tests, with separate broker state and fresh processes recommended for repeatable runs.
Why it matters for your role
Pick the building you work in.

For software engineers
MQTT QoS does not remove the need to respect broker-side in-flight limits. Client implementations should inspect reason codes, bound outstanding publishes and design payload identifiers for safe redelivery.

For QA engineers
The reported results vary with reconnect timing, so link-failure tests should use isolated broker state, fresh processes and assertions for both missing readings and duplicates.
Based on reporting by DEV Community (source article).
Written with AI assistance and reviewed under our editorial standards. Spotted an error? Tell the desk.









Comments