DevPath
Foundation lesson · 13 min

Design a job that can safely retry

Treat queue delivery as repeatable and keep side effects observable.

A job may run twice

A worker can fail after making a change but before acknowledging completion. A retry then sees the same payload. Give the business action a stable idempotency key and record its outcome. Check that record before applying the side effect again.

Separate failures

Use bounded retries with backoff for temporary network failures. Invalid payloads and permanent authorization failures should fail clearly instead of looping. Log a correlation ID and enough context to investigate without exposing secrets or personal data.

if (ProcessedJob::where('key', $eventId)->exists()) return;
// Apply the change and store the processed key in one transaction.

Choose the deduplication point

Check and record the operation key in the same transaction as the database effect. A read followed by a write without a unique constraint still races when two workers start together. Put a unique index on the key and treat a duplicate-key result as a replay. For an external payment call, pass its idempotency key to the provider when supported.

Test the awkward timeline

Simulate a worker that completes the effect, then crashes before acknowledging the job. Run the job again with the same event ID and assert that the effect happened once. Then test a transient timeout, a permanent malformed payload, and exhausted retries. These tests prove the behavior users care about rather than merely checking the retry count.

Check your understanding

Why should a queued email or payment action have a stable operation key?

Show explanation

A retry may execute after the first attempt already caused the side effect. The key lets the system detect and suppress duplicate work.

Try it at foundation level

Trace a queued email job delivered twice after a worker crash. Add an idempotency key and a failure record to the design.

Check your work

The second delivery does not repeat the business effect, and an operator can tell whether the first attempt completed.

Self-guided practice. This does not record assessed mastery.

Put this lesson into practice

A related activity in this module at mid level.

Try: Deduplicate a payment webhook

Further reading

Primary references for this module. Check the current version when behavior or framework guidance may have changed.