Skip to main content

Design Background Jobs That Can Retry Without Duplicating Work

Design a menu-import job with stable identity, retries, and a clear user-facing recovery path.

Two retry paths feed one job receipt, with the hook Retry Without Duplicates.

At 12:03, a restaurant’s printer accepts ticket 482. Before the worker can save the response, its connection drops. The queue delivers the job again. From the worker’s point of view, a timeout before the print and a timeout after the print look identical. From the kitchen’s point of view, they’re the difference between no lunch order and cooking the same order twice.

Make the retry represent the original action. The job can run more than once. The print, charge or notification needs its own identity.

Walk through the failure before choosing an implementation

What the worker knowsWhat might have happenedAppropriate next step
Request was rejected before work beganNothing was doneRetry a transient rejection with backoff
Request timed out after it was sentThe receiver may already have actedLook up the outcome or retry with the original provider idempotency key
Receiver confirmed success and the job result was savedThe action is completeReturn the saved result
A new version of the order genuinely needs another ticketThis is different workCreate a new action with a new key

Put the identity at the business boundary. A key like ticket:482:kitchen:v1 survives redelivery, worker replacement and a manual retry. A random queue message ID won’t help if the same request can be enqueued twice. An order ID alone is too broad because one order can have separate tickets for the bar and the kitchen. Scope the uniqueness to the tenant too.

One durable record per intended action

If the order lives in PostgreSQL, write the order and the intent to print in the same transaction. The table that holds pending work needs a uniqueness constraint on (venue_id, action_key). The following is the producer side of an outbox. The worker can pick up the committed row later.

sql
INSERT INTO dispatch_outbox (venue_id, action_key, payload, status)
VALUES ($1, $2, $3, 'pending')
ON CONFLICT (venue_id, action_key) DO NOTHING;

Store the result or provider reference on that record. If two requests reuse the same key with different payloads, surface the conflict instead of silently treating the second request as the first. If multiple workers claim work, claim a row atomically and give the claim an expiry so a crashed worker can be replaced. Write completion only while you still own the claim.

The database constraint prevents a second intent record. It cannot guarantee that an external printer, email service or payment provider saw only one request. Use the provider’s own idempotency mechanism where available. If the provider cannot deduplicate or confirm a result, send ambiguous cases for reconciliation. A button labeled Retry now must not quietly invent a fresh key.

Rehearse the incident with one real workflow

  1. Replay the same event twice and check there is one intent record.
  2. Stop the worker after the receiver accepts the request but before completion is recorded. Restart it and check whether the original outcome can be recovered.
  3. Start a second worker while the first is processing. Verify it can’t complete the same claim.
  4. Send a genuinely different action for the same order. Verify it can still run.
  5. Run the case where the receiver is unreachable for several minutes. Check how backoff, the attempt limit and the operator’s next step appear in the run history.

The useful log entry is the one you can use during service: the order, action key, attempt number, last known outcome and provider reference. If someone can answer “Did we already do this?” without rummaging through a queue dashboard, the design is doing its job.

Sources

Build a project

© 2026 All rights reserved.