After MQTT PUBACK: Preventing Duplicate Business Processing
Separating MQTT 5.0 QoS 1/2 delivery hops, reconnect redelivery, and firmware retries from application-level idempotency and atomic outbox writes.

Why a Protocol Acknowledgement Does Not Mean Exactly-Once Processing
On the pet tracker project, I designed a backend framework for handling MQTT device messages. In analyzing unreliable device links, a common misconception is treating MQTT QoS 1 as proof that business logic executed once and only once. The following is an illustrative design grounded in that background rather than a line-by-line claim about a specific production codebase.
Consider a hypothetical scenario. A collar leaves an underground garage, connects with an active session, and publishes a QoS 1 packet reporting its location and low battery. The MQTT broker accepts the packet, forwards it toward downstream consumers, and sends PUBACK—but the cellular link drops before PUBACK reaches the collar. When the collar reconnects with Clean Start = 0 and resumes the existing session (Session Present), the MQTT protocol retransmits the unacknowledged PUBLISH packet with DUP = 1.
Under OASIS MQTT 5.0 (§4.4, [MQTT-4.4.0-1]), protocol-level retransmission of unacknowledged QoS packets occurs on reconnect when resuming an existing session, not via arbitrary timeout resends on an otherwise live connection. Once the handshake completes, the Packet Identifier can be reused; after a receiver has sent PUBACK, any subsequent PUBLISH with the same Packet Identifier must be treated as a new application message regardless of DUP. Packet Identifiers never identify business work. Separately, device firmware often implements its own application-level retry, which publishes a brand-new MQTT packet (typically DUP = 0 with a new Packet Identifier) carrying the same business event.
Broker Acceptance, Downstream Persistence, and QoS 2 Scope
Broker PUBACK should also not be conflated with application database commit. The broker acknowledges receipt independently of downstream persistence: there is no required ordering between broker PUBACK and database commit, and PUBACK does not prove a consumer transaction succeeded. Likewise, a downstream worker can commit a database row and crash before acknowledging its own message queue, triggering another delivery.
Upgrading to QoS 2 (PUBLISH, PUBREC, PUBREL, PUBCOMP) avoids duplicate delivery across each MQTT sender/receiver hop—including client-to-broker and, when negotiated, broker-to-subscriber—yet still stops at the protocol boundary. It cannot cover downstream consumer crashes or firmware application-level retries. Letting MQTT handle transport reliability while making business writes explicitly idempotent keeps each layer’s contract clear.
An Illustrative Design: Stable Idempotency Keys and an Atomic Outbox
In a simplified design, each payload carries a stable business identity combining the device ID, a firmware bootId (or session token), a monotonic msgSeq, and the sample timestamp reportedAt:
{
"deviceId": "collar-demo-01",
"bootId": "boot-7f3a",
"msgSeq": 4182,
"reportedAt": "2026-12-03T08:15:30Z",
"batteryPct": 14,
"payloadType": "telemetry"
}
Two constraints matter here. First, any field included in an idempotency key—such as reportedAt if used—must remain identical across retransmissions rather than being regenerated on retry. Second, because embedded devices reset msgSeq after reboots and device clocks can be unreliable, pairing bootId with msgSeq distinguishes post-reboot sequences without assuming accurate clocks.
To prevent a crash after database commit from losing a low-battery alert, independent deduplication on a notification service is not enough once the unrecorded intent vanishes. Recording telemetry and the notification outbox row in one atomic database transaction closes that gap, as illustrated in the pseudocode below:
// Pseudocode: atomic telemetry deduplication and outbox recording in one transaction
function handleTelemetry(msg):
db.beginTransaction()
try:
inserted = db.execute("""
INSERT INTO device_telemetry (device_id, boot_id, msg_seq, reported_at, battery_pct)
VALUES (:deviceId, :bootId, :msgSeq, :reportedAt, :batteryPct)
ON CONFLICT (device_id, boot_id, msg_seq) DO NOTHING
""")
if inserted == 1 and msg.batteryPct <= 15:
db.execute("""
INSERT INTO notification_outbox (dedup_key, device_id, kind, payload)
VALUES (:deviceId || ':' || :bootId || ':' || :msgSeq, :deviceId, 'LOW_BATTERY', :msg)
ON CONFLICT (dedup_key) DO NOTHING
""")
db.commit()
return inserted == 1 ? ACK_PROCESSED : ACK_DUPLICATE
catch err:
db.rollback()
throw err
Tradeoffs and Verification
Maintaining unique indexes and an outbox relay adds write overhead and slight delivery latency for asynchronous notifications. A short-TTL cache can absorb immediate repeats, but persistent uniqueness remains essential when offline devices flush buffered events long after cache expiry.
Three targeted tests verify the boundary end to end:
- Reconnect session redelivery: allow the broker to accept and forward a QoS 1 packet, drop the returning
PUBACK, disconnect, and reconnect withClean Start = 0to resume the existing session (DUP = 1); confirm one telemetry row and one outbox row exist. - Firmware retry and reboot sequence: resend an identical
(deviceId, bootId, msgSeq, reportedAt)as a fresh publication (DUP = 0) to confirm deduplication, then rotatebootIdwithmsgSeq = 1to confirm post-reboot messages are accepted. - Transaction and relay faults: inject a failure before commit to verify joint rollback, and fail the outbox dispatcher after commit to confirm retry delivers the notification without duplicating telemetry.