skip to content

In a field-inspection app's offline outbox, when is a queued write's idempotency key created, and how does that limit merging later edits into it?

level: middleimportance: should knowfreq 48%

answer

  1. the key names one payload
  2. minted once, stored with the entry
  3. a relaunch reuses the stored key
  4. attempted entries are frozen
  5. keys expire on the server

basics

~20 s

The key is minted once with the entry and stored in it, so every replay, even after a relaunch, sends the same key. Since a key stands for one payload, later edits may be merged only into an entry never attempted.

solid answer

~50 s

An idempotency key lets the server recognise a retry of the same request, so it must stay the same across every attempt - including attempts after the app was killed. The outbox therefore creates it once, when the entry is created, and stores it in the entry in the same transaction; a key minted per attempt, or kept only in memory, turns the replay of a request that already succeeded into a duplicate. The Idempotency-Key header draft also says a key must not be reused with a different payload. So coalescing - folding a later edit into a queued entry - is allowed only while the entry has never been attempted. Once a send has left the device, the payload is frozen and a later edit becomes a new entry, with its own key, behind it in the lane.

code

pseudocode · 18 lines
pseudocode
function queue_update(record_id, changes):
  begin transaction
    last = newest entry in outbox where record_id = record_id
    if last exists
       and last.state = "pending"
       and last.first_sent_at is empty
       and last.op in ("create", "update"):
      last.payload = merge(last.payload, changes)
      save last
    else:
      insert into outbox
        op = "update"
        record_id = record_id
        payload = changes
        idempotency_key = new_uuid()
        first_sent_at = empty
        state = "pending"
  commit

go deeper

for a junior

Recall that a retry must carry the same idempotency key as the first attempt, so the key is saved with the queued change rather than made up at send time each time.

for a middle

Explain why the key is stored in the entry's transaction, what a server following the draft returns for a completed, in-progress or mismatched retry, and when merging edits is allowed.

for a senior

Walk through a kill during a send and a later edit to the same record, and show where key expiry on the server stops protecting a long-offline phone.

for a principal

Agree with backend owners on key retention and client-generated ids for creates, measured against how long field devices actually stay offline, rather than treating keys as unlimited.

## The situation A technician's queue holds an `add_finding` entry: "Corroded flange on pump 3". Coverage flickers on in the car park. The app sends the entry, the server applies it, and the response is lost when the signal drops. Then the app is killed. Two hours later, still offline, the technician edits the finding's text. When the van reaches coverage, what goes out? The answer turns on **when the idempotency key was created** and **what the queue may do to an entry once it has been sent**. ## The key belongs to the entry, not to the attempt An **idempotency key** is a unique value a client sends with a non-idempotent request so the server can recognise a retry of the same request. The IETF draft for the `Idempotency-Key` header (draft-07, still an Internet-Draft) recommends a UUID or similar random value and says a key must not be reused for a request with a different payload. For a server that follows it: - a retry after the original completed should get the **original result** back, success or error; - a retry while the original is still being processed should get **409 Conflict**; - reuse of a key with a **different payload** should get **422 Unprocessable Content**. For the outbox that fixes one rule: the key is minted **once** - simplest when the entry is created - and stored in the entry in the same transaction. Every attempt sends the stored value, including attempts after a relaunch, a reboot or a week offline. A queue that mints a key per attempt, or holds it only in memory so that a relaunch makes a new one, turns the replay of a request that already succeeded into a second finding. After a timeout the client cannot know whether the server applied the request; a stable key makes that question safe to leave unanswered. How the server stores and looks up keys is a protocol topic; the client's half is never to lose or change one. ## An attempted entry is frozen **Coalescing** - folding several edits to one record into one entry before sending - saves requests, battery and server work. It is safe only while the server has never seen the entry. Once an attempt has left the device, the server may already hold that key against that payload. If the queue merges the new text into the same entry and replays it under the same key, the payload no longer matches: a server following the draft should answer 422, and a server that does not compare payloads simply returns the stored result of the first request, so the edit is silently lost. | Entry state | A later edit to the same record | Why | |---|---|---| | Never sent (pending, no `first_sent_at`) | Merge into the entry and keep its key | The server has never seen this key | | Attempted, outcome unknown | Append a new entry with its own key, after it | The old payload may already be applied under that key | | Acknowledged and removed | Append a new entry | The first change is finished | The same test governs the other coalescing rules: - several `set_item` edits to one checklist item collapse into the last value; - a create followed by edits folds into the create; - a create followed by a delete can cancel out and both entries be dropped - but only if the create was never attempted; otherwise both must go out. ## A relaunch in the middle of a send When the app is killed with an entry marked **in flight**, no request from the old process survives. On launch: 1. Reset in-flight entries to pending. Their `first_sent_at`, written in the same commit that marked them in flight - before the request left - keeps them closed to merging. 2. Send them again, in lane order, with their stored keys. 3. Read the answer. Either way a success comes back - the server's stored result if the first attempt landed, a fresh one if it never arrived - so the client need not know which. A 409 means the first is still being processed: wait and retry unchanged. 4. Only after a success is applied locally - in one transaction with deleting the entry - does the lane move on. ## Where the key stops helping Servers keep idempotency keys for a limited time; the draft says a resource should define and publish that expiry. A phone that sent a request, lost the response and then stayed offline longer than that window replays a key the server has forgotten, and the request looks new. For creates, the stronger protection is the record's own **client-generated id**: a server that refuses a second create for an id it already holds stays safe however long the phone was away, provided the queue treats "this id already exists with the same content" as success rather than as a rejection. The key then covers operations with no natural id, such as "add 1 to the count of defects", and the window in which retries are most likely.

  • The app is killed while an entry is in flight. What does the relaunch do?
    Nothing from the old process survives, so the queue resets the entry to pending and resends it with the stored key. If the first attempt landed, the server returns its stored result; if it never arrived, the request is processed normally; a 409 means the original is still being processed, so the queue waits and retries unchanged.
  • Can a create followed by a delete simply cancel out?
    Only if the create was never attempted. Then the server has never heard of the record and both entries can be dropped in one transaction. If the create was sent even once, the server may hold the record, so both entries must go out in order.
  • What if the phone stays offline longer than the server keeps idempotency keys?
    A replay after expiry looks like a new request. For creates, a client-generated record id gives a second guard: the server can refuse a create for an id it already has. For operations with no natural id, the honest answer is a server retention window agreed against how long field phones really stay offline.

A posted cheque carries a number. If you are not sure it arrived, you can send the same cheque again and the bank will refuse to pay that number twice. What you cannot do is change the amount on a cheque that may already have been cashed; you write a second cheque, with a new number, for the difference.

  • Generate a fresh idempotency key for each retry so the server treats it as new.
  • The key only matters within one session, so it can live in memory.
  • Any later edit can be merged into a queued entry, even one already sent.
  • A request that timed out failed, so resending it cannot duplicate anything.
  • The idempotency key lets the queue send entries in any order.