Orofold Labs

Launching soon

Notes Get in touch

Operations and security 3 min read Updated

Background queues, retries and dead-letter jobs

In short

Moving slow emails, PDF invoices or webhook syncs off the HTTP request thread is only step one. Make workers idempotent, space out retries with jitter and route permanent failures to a dead-letter queue.

Sending confirmation emails, generating PDF invoices or pushing stock updates to external APIs inside a web request ties user-facing page speed to the slowest third-party dependency. Moving slow work to a background job queue keeps checkout and dashboards fast—provided the queue handles crashes, retries and poisoned payloads cleanly instead of silently dropping or duplicating tasks.

Assume at-least-once delivery and make jobs idempotent

Unless you accept lost work on crashes, a practical queue delivers jobs at least once. If a worker sends a payment webhook or creates an invoice and dies before marking the job complete, the queue hands the same job to another worker after the visibility timeout expires. Every handler must therefore be idempotent (see Idempotency and Stripe’s idempotent requests guide):

  • Attach a unique business key (such as invoice_generate:order_1042) to each job payload.
  • Check a database unique constraint or state column before calling an external side effect.
  • Enqueue jobs inside or right after committing the database transaction (the transactional outbox pattern) so a worker never starts before the order row exists.

Exponential backoff with jitter

As explained in the AWS Builders’ Library article on timeouts, retries and backoff with jitter, immediate or fixed-delay retries make outages worse. When a downstream API goes down for thirty seconds and 500 queued jobs retry every 60 seconds in lockstep, the spikes crush the dependency just as it tries to recover.

Use exponential backoff with random jitter for transient errors (HTTP 429, 502, 503, network timeouts), and do not retry permanent 4xx validation errors—fail fast instead so broken payloads do not waste worker capacity.

Separate retryable faults from dead-letter jobs

Failure typeExamplesQueue action
Transient faultNetwork timeout, rate limit (429), upstream 503Retry with exponential backoff + jitter up to a fixed attempt limit
Permanent payload or logic errorMissing customer tax ID, deleted record, 400 validation errorMove directly to dead-letter queue (DLQ) without retrying
Poisoned / crash-loop jobOut-of-memory crash on malformed file or repeated timeoutAfter max attempts (e.g. 5–8), move to DLQ and alert

A dead-letter queue is useless if nobody reads it. Route DLQ growth to an operational alert (Monitoring and alerts), keep enough error context to inspect the failure, and give operators a safe script or admin button to replay jobs once the root cause is fixed.

Visibility timeouts, concurrency and backpressure

  • Set visibility timeout above job runtime: if an import job takes up to 90 seconds, a 30-second visibility timeout causes two workers to process the same job in parallel.
  • Isolate slow queues from fast queues: keep password-reset emails and transactional receipts in a high-priority queue separate from bulk CSV exports or nightly feed syncs (see Cron jobs).
  • Cap worker concurrency per external target: respect partner API rate limits so scaling up workers does not trigger 429 bans across your whole integration.

A simple queue setup for small products

You do not need heavy distributed brokers on day one. For modest volumes, a relational database jobs table with SELECT ... FOR UPDATE SKIP LOCKED (in PostgreSQL or MySQL 8+) or a lightweight Redis queue—paired with structured logs (Logging), backoff with jitter, and a DLQ view—keeps operations observable with minimal moving parts, following Google SRE operational simplicity.

Always store a max_attempts counter and an available_at timestamp on queued jobs. Unbounded retries turn a single bad record into an infinite background CPU and billing loop.

Sources and limits

Replacing brittle background scripts or slow synchronous endpoints? Describe the workload through our enquiry form.

Common questions

Why should retries use exponential backoff with jitter instead of fixed intervals?
Fixed-interval retries cause all failed jobs to hit a recovering downstream service at the exact same second (a thundering herd). Exponential backoff with random jitter spreads retry attempts out so the external dependency can recover.
What goes into a dead-letter queue and what happens next?
Jobs that exceed the maximum retry count or fail validation permanently move to a dead-letter queue instead of blocking active workers. An operator inspects the error, fixes the bug or bad payload, and replays or discards the job.
Why must every queued job handler be idempotent?
Most job queues guarantee at-least-once delivery: if a worker crashes after calling an external API but before acknowledging the job, another worker runs the same job again.

Sources

  1. Timeouts, retries, and backoff with jitter, Amazon Builders’ Library (AWS). Accessed 24 November 2026
  2. Idempotent requests, Stripe Docs. Accessed 24 November 2026
  3. Site Reliability Engineering: Table of Contents, Google SRE Book. Accessed 24 November 2026

Further reading

Have an idea that should exist? Or a working product that needs more customers, a new website or a complete rework? Bring us anything. Apply with your idea

All notes