Back to the guide

SYSTEM DESIGN SKETCHBOOK / VISUAL GUIDE RECAP

When a job runs twice

APIs & queues · free to copy and share

What to remember

  • A lease (visibility timeout) hides a job instead of deleting it, so a crashed worker's job comes back.
  • A lost acknowledgement after the side effect turns into a second run of that side effect.
  • At-least-once delivery is the normal case; the side effect is the operation that must be safe to retry.
  • Write a dedupe record in the same transaction as the effect, or use a conditional write, so the retry changes nothing.
  • Use a key tied to the business action, set by the producer, and pass it to any external service that performs the effect.
enqueuelease jobredelivercharge + keyCheckout APIJob queuelease 30 sWorker AWorker BLedger DBwallets + done keysCard providersame key: same result
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.

Redraw from memory (fold the page so the figure is hidden)

Without looking, draw the queue, two workers and the database. Mark where the acknowledgement is lost, then draw where duplicate work is prevented. Next to it, write which operation must be safe to retry and why the queue cannot do it for you.

Check afterwards: The lease or visibility timeout on the queue, with the job hidden rather than deleted. · The lost acknowledgement after the side effect, and the redelivery to a second worker. · An idempotency key tied to the business action, not to the delivery attempt. · The dedupe record and the effect inside one transaction (or a conditional write). · The retry acknowledged as a no-op instead of failing forever. · A note on external effects: pass the key to the provider.

Apply it: Design a message broker with per-message acknowledgement (No. 19 in the question bank)