Learn

VISUAL GUIDE · APIS & QUEUES

When a job runs twice

Step 9 of 10: Or let the row say whether it is done

STEP 9 / 10ORIGINAL SKETCH
enqueuelease jobdebit order 42redeliverdebit order 42Checkout APIJob queuelease 30 sWorker AWorker BLedger DBwallets + done keys0 rows: already done
A checkout API enqueues a charge job; worker A leases it and writes to the ledger database but its acknowledgement is lost; the queue redelivers to worker B, which writes again unless an idempotency key stops it; an external card provider can also receive the key.
9 / 10
  1. Enqueue
  2. Lease
  3. Side effect
  4. Ack lost
  5. Timeout
  6. Redelivery
  7. At-least-once
  8. Dedupe key
  9. Conditional write
  10. External effects

STEP 9 OF 10 · CONDITIONAL WRITE

Or let the row say whether it is done

When the effect is a state change on one row, a conditional write works without a separate table: UPDATE orders SET status = 'charged' WHERE id = 42 AND status = 'pending', with the debit in the same transaction.

The first run changes one row. The retry changes zero rows, sees that, and skips the debit. Same outcome as the dedupe record: the retry is a no-op.

Why it matters. A state machine on the row is often the simplest idempotency key you already have. Pick whichever matches where the effect lives.