MQTT QoS 2 prevents duplicate delivery of one Application Message across a single MQTT sender-receiver relationship while its protocol exchange is active. It uses a Packet Identifier and the PUBLISH, PUBREC, PUBREL, and PUBCOMP sequence to manage ownership and suppress retransmitted copies.
That guarantee ends at the protocol boundary. If an application turns the delivered payload into a robot action, MQTT does not know whether the action started, committed, completed, or was repeated after an application restart.
## The four-packet exchange controls delivery
The sender assigns an unused Packet Identifier and transmits PUBLISH at QoS 2. Until PUBREC arrives, a retransmitted PUBLISH can use the same identifier. The receiver acknowledges repeated copies with PUBREC and must not cause duplicate onward delivery for that exchange.
After PUBREC, the sender sends PUBREL with the same identifier. The receiver answers with PUBCOMP. Once the sender receives PUBCOMP, the Packet Identifier becomes available for reuse. A later PUBLISH with that reused identifier is a new Application Message.
This state machine is valuable when packet loss, reconnect, or acknowledgement loss would otherwise duplicate delivery. It has more overhead and state than QoS 0 or 1.
## Delivery is not physical execution
A robot command handler might receive a QoS 2 message, enqueue work, and advance the MQTT handshake. The process can then crash after the arm moves but before the application records completion. On restart, a separate retry from an upstream service may submit the same business operation in a new MQTT message with a new Packet Identifier.
MQTT correctly delivers each Application Message once, yet the physical operation can happen twice because the two messages represent the same intent. Packet Identifier is scoped to MQTT protocol state and eventually reused; it is not a durable operation key.
The reverse failure is also possible. The receiver can complete QoS 2 while later rejecting the command for an expired deadline or wrong robot state. Protocol completion does not prove application acceptance.
## Broker hops create separate exchanges
When a broker receives a QoS 2 publication, it accepts ownership through one exchange. Forwarding to each subscriber is another delivery relationship with its own Packet Identifier and QoS selected by subscription rules. The publisher's identifier does not travel end to end as a transaction ID.
Bridges and middleware may republish payloads as new Application Messages. Every hop can meet its MQTT obligations while an application-level duplicate still crosses the system. End-to-end operation identity must be carried in the protected payload or application metadata.
## Add a durable operation record
Give every robot command a globally unique or appropriately scoped operation_id. Include target identity, parameters, issued time, deadline, expected state version, and requester identity. Authenticate these fields.
Before scheduling motion, the robot service atomically checks and records the operation ID with its parameters and initial status. If the same ID and same parameters arrive again, return the stored status. If the ID is reused with different parameters, reject it as a conflict.
Advance the record through explicit states such as received, validated, scheduled, running, completed, failed, and cancelled. Tie the transition to controller feedback or process evidence where required. The MQTT acknowledgement point and the application commit point should be separately documented.
For operations that cannot be made naturally idempotent, use a state-machine precondition. A command can specify an expected job version or starting pose class, and the executor rejects it once the process has advanced. Safety decisions still require the robot's validated safety architecture.
## Session recovery is not business recovery
With Clean Start 0 and Session Present 1, MQTT resends unacknowledged PUBLISH or PUBREL packets using original Packet Identifiers as required. This repairs protocol state. It does not restore an in-memory application record lost by a crash.
Persist application operation records independently of the MQTT library's Session State. On restart, reconcile running or uncertain operations with controller feedback before accepting retries. Report an explicit unknown state when evidence cannot establish completion.
Message Expiry can prevent starting delivery of stale messages, but once delivery begins it does not replace application deadlines. Correlation Data can pair a response, but it does not provide idempotency. These features complement, rather than replace, the operation record.
## Test crash windows
Drop PUBREC, retransmit PUBLISH with the same Packet Identifier, and verify only one application delivery occurs. Drop PUBCOMP and verify PUBREL repetition does not redeliver the payload. Resume a stored Session and confirm protocol state continues.
Then crash the robot service after recording an operation but before execution, during execution, and after physical completion but before final status persistence. Retry the same operation ID as a new MQTT message. Confirm the service resumes, returns recorded outcome, or reports uncertainty without blindly repeating motion.
Send the same Packet Identifier after the earlier QoS 2 exchange has completed and verify it is treated as a new MQTT message. Send a different Packet Identifier with the same operation ID and verify application deduplication catches it.
QoS 2 provides exactly-once delivery within an MQTT protocol exchange. Reliable robot behavior still depends on durable operation identity, atomic records, state preconditions, controller evidence, and carefully defined recovery for every crash boundary.