Avoid a duplicate event after a retry
Check units and version numbers as you type. · 201 words
A retry resends a request after a timeout or network error. The original may have succeeded even though its confirmation was lost. For example, a weather station uploads a rain reading. The server stores it, but the acknowledgment disappears. The station retries and could create a duplicate. An idempotency key identifies one intended action. The server checks it: a new key causes the action and stores the key with the result. An existing key returns the saved result. "Idempotent" means repeating the operation has the same effect as doing it once. The key and result must be saved atomically so simultaneous retries cannot both pass the check.
Consider buying a museum tour ticket. The phone creates key ticket-7f21 and sends the order. The server books it, but the reply is lost, so the phone retries with the same key. The server finds ticket-7f21, returns the original booking, and does not charge twice. If the client generated a new key for the retry, the server could treat it as a new action and duplicate the purchase. Use one stable key across retries of the same action, but a new key for a genuinely new action, such as intentionally buying a second ticket.
Check your recall
The museum-ticket client times out and retries. What must stay the same, and what must the server do?
Check your answer
The client must reuse the same idempotency key. The server must find the stored result for that key and return it without booking or charging again.
Sources and credits
Original KeyTouch lesson. Background reading · Google · Rules of Machine Learning.