Definition
Retransmission is sending data again because the sender has not learned that an earlier copy arrived. Duplicate delivery is what happens when the earlier copy did arrive: the receiver gets the same message more than once. A receiver that treats each copy as a new request performs the request twice. A retry is told apart from a new operation by an operation identifier, a value that the sender assigns once to each operation and repeats in every copy, and that the receiver remembers after performing the operation.
The idea
An updater sends a device the request “increase the stored counter by 1”. The device performs it and sends back an acknowledgment, a short reply that confirms the request was handled. The acknowledgment is lost, or the connection drops just before it arrives. To the updater, a lost request and a lost acknowledgment look the same: silence. No protocol removes this doubt, because an acknowledgment can be lost too, and so can an acknowledgment of the acknowledgment. The updater can only give up or send the request again. If the first copy had arrived, a device that does not recognize the second copy increases the counter twice.
The key is to count two things separately: deliveries, the number of copies that reach the receiver, and effects, the number of times the receiver performs the operation. Retransmission makes delivery reliable by allowing extra deliveries, which is called at-least-once delivery. The receiver must turn at-least-once delivery into an exactly-once effect §8.32: every operation it accepts is performed once, however many copies arrive. A numbered bank check is a fair picture. A bank pays each check number once. A second copy of the same check is refused, and a new check with a new number is paid even if the amount is the same. The picture holds only while the bank remembers which numbers it has paid. A bank that forgets after a fire may pay the same check twice, and a device that forgets after a restart may perform the same operation twice.
How it works
In BLE §7.1 (Bluetooth Low Energy), retransmission happens at more than one layer, and each layer detects duplicates only within its own scope.
Link Layer §7.6. The Link Layer, the part of each BLE chip that controls the radio and its packets, marks each packet with a one-bit sequence number. The receiver acknowledges a packet by flipping a bit in its own next packet. A packet that is not acknowledged is sent again, and the receiver discards a copy whose sequence number it has already accepted. Within one connection, each packet is therefore passed up once and in order. When the connection ends, this numbering ends with it.
Attribute Protocol §7.9. ATT (Attribute Protocol) is the protocol through which one BLE device reads and writes values on another. For an ATT Write Request, the device sends a Write Response after it has handled the write. If the connection drops after the write was handled but before the response arrives, the sender cannot know whether the write took effect.
Application. After reconnecting, the updater sends the whole operation again. No lower layer can recognize this copy: it travels on a new connection, with a new Link Layer numbering.
Only the application knows what one operation is, so the application must tell retries from new operations. A common design has five steps.
Assign an identifier. When the sender creates an operation, it assigns an operation identifier, for example the next value of a counter or a random 128-bit number, and stores the identifier with the operation. Every retry, including one sent after a reconnection or after the sender restarts, carries the same identifier and the same content. A new operation gets a new identifier.
Protect it. The identifier is inside the authenticated part of the message, the bytes over which a message authentication code §5.7 (a short value that only a holder of the secret key can compute) is checked. An attacker therefore cannot change the identifier without detection.
Keep a record. The receiver keeps a record of completed operations. Each entry holds the identifier, a digest of the content (a short fixed-length value computed from the content by a cryptographic hash function §5.1), and the result the receiver returned.
Decide. For each arriving operation, the receiver looks up the identifier.
Not in the record: a new operation. Perform it, then record it.
In the record with the same digest: a retry. Do not perform it again. Send back the recorded result, so that the sender can finish.
In the record with a different digest: an error or an attack, never a retry. Reject it.
Commit the effect and the record together. The effect and its record entry are written in one atomic step, a step that after a power loss has either happened completely or not at all. If the device could restart after performing the effect but before recording it, a retry after the restart would be performed again. The Exactly-once effect entry treats this step in detail.
A compact record is common. Each sender numbers its operations 1, 2, 3 and so on, and sends a new one only after the previous one is acknowledged. The receiver then stores only the highest completed number and its result. An arriving number equal to the stored one is a retry, a smaller one is old and is rejected, and a higher one is a new operation. This is the same state that a receiver keeps against a replay attack §1.26, in which an attacker records a genuine message and sends it again later. The receiver does not need to know whether a copy came from the honest sender or from an attacker: in both cases the operation is performed at most once.
A second approach makes the operation idempotent §8.31, meaning that performing it twice has the same effect as performing it once. “Set the counter to 6” is idempotent; “increase the counter by 1” is not. Idempotent operations make duplicates harmless without a record of identifiers. They still need ordering: a delayed copy of an old “set the counter to 6” that arrives after a newer “set the counter to 7” sets the counter back. A version number that may only increase prevents this.
Worked example
A device stores a counter with the value 5. The updater sends operation X, “increase the counter by 1”, with identifier 41; let be the digest of its content. The device performs X, so the counter becomes . It records the entry (41, , result 6) and replies. The reply is lost. The updater reconnects and sends X again with identifier 41.
Without a record, the device performs X again: . One operation produced two effects.
With the record, the device finds identifier 41 with digest , leaves the counter alone, and replies “done, result 6”. Two deliveries produced one effect, and the counter is 6, as intended.
Same identifier, different content. A message with identifier 41 and the content “increase the counter by 2” has a different digest, so the device rejects it. Treating it as a retry would report the old result for an operation that was never performed.
A new operation. Later the updater wants another increase and sends identifier 42 with the same content, “increase the counter by 1”. The identifier is new, so the device performs it: . Two operations produced two effects, as intended, although the two messages carry identical content.
A record held only in RAM. Suppose the counter is stored in flash memory §6.6, which keeps its content without power, but the record is held only in RAM. The device writes 6 to flash and loses power before it replies. After the restart the record is gone, the retry with identifier 41 looks new, and the counter becomes 7. Writing the counter and the record entry in one atomic flash update closes this gap.
Stated precisely
For an operation , let be the number of copies of that reach the receiver, and the number of times the receiver performs the effect of . Retransmission makes as long as the sender keeps retrying and the network does not lose every copy, and it also allows . The receiver’s goal is for every operation it accepts, whatever the value of , and for every operation it rejects.
Let be the identifier and the content digest of an arriving operation, and let be the receiver’s record, a set of entries of the form (identifier, digest, result). The receiver’s rule is:
if no entry in has identifier , perform the operation, and add to in the same atomic step as the effect;
if holds , do not perform the operation, and return the stored result;
if holds an entry with identifier and a digest with , reject the operation.
The rule gives for every accepted operation when four assumptions hold.
The sender never reuses an identifier for a different operation, including after its own restart.
The identifier and the content are inside the authenticated part of the message.
The record survives every failure that the effect survives: if the effect is stored in flash memory, so is the record entry, written in the same atomic step.
The record keeps each identifier for as long as a retry of that operation can still arrive.
Limits and common mistakes
Reliable delivery at one layer does not give an exactly-once effect. A Link Layer acknowledgment says that the peer’s Link Layer received a packet. It does not say that the application performed anything.
A new connection does not mean a new operation, and a retry flag in the message does not mean a retry. A sender sets such a flag also when the first copy was lost, and an attacker can set it at will.
Identical bytes do not mean a duplicate, and a known identifier does not mean a duplicate either. Two genuine operations can have the same content, as identifiers 41 and 42 do in the example, and a reused identifier with new content is not a retry. The identifier and the content digest together decide.
Recording the identifier before performing the effect is wrong as well. If the effect then fails, every retry is answered “done” although nothing happened. Record the identifier together with the result, in the same atomic step as the effect.
The sender must store an operation’s identifier before sending it the first time. A sender that restarts and creates a new identifier for the same operation turns a retry into a new operation.
An effect outside the device, such as a motor movement or a payment made by another system, cannot be made atomic with a local record. An exactly-once effect there needs the other system’s cooperation, for example an identifier that the other system itself checks.
A record cannot grow without bound on a small device. A per-sender sequence number keeps it to one entry per sender, at the cost of allowing each sender only one unacknowledged operation at a time.
Questions answered
How is a retry distinguished from a new update operation?
A retry carries the same operation identifier and the same content as the original operation, and a new operation carries a new identifier. The sender assigns the identifier once, when it creates the operation, stores it, and repeats it in every retry, including retries after a reconnection or a restart. The receiver keeps a record of completed operations, each entry holding the identifier, a digest of the content and the result, written in the same atomic step as the operation’s effect. An arriving identifier that is not in the record marks a new operation, which the receiver performs. An identifier in the record with the same digest marks a retry: the receiver returns the stored result without performing the operation again. The same identifier with a different digest is rejected. The identifier must be inside the authenticated part of the message, so that no one can change it undetected.
Nothing below the application can make this distinction. Duplicate detection in the Link Layer, the part of a Bluetooth Low Energy chip that controls the radio and its packets, ends with the connection; a new connection does not mean a new operation; and two genuine operations can carry identical bytes. An idempotent operation, one whose repetition has the same effect as doing it once, such as “set the version to 7”, makes the distinction unnecessary for its effect, but it still needs ordering so that an old copy cannot undo a newer operation.
Related entries: Idempotency (§8.31), Exactly-once effect (§8.32), Replay attack (§1.26), Write request versus write command (§7.13), Protocol state machine (§7.37). Further reading: [91, 93].