Back to results for “Error Handling”
Node.js › Background Jobs & Queues

What is a dead-letter queue (DLQ), and how should a Node.js job-processing system use one?

Advanced

Answer

A dead-letter queue holds jobs that have permanently failed after exhausting all configured retry attempts, rather than discarding them silently -- this preserves the failed job's data and error context for manual inspection, alerting, or later reprocessing once a root cause is fixed, preventing 'silent data loss' where a systematically failing job type (due to a bug or a bad input) would otherwise just vanish after its retries ran out.
const worker = new Worker('orders', processOrder, {
  connection,
  settings: { maxStalledCount: 3 },
});

worker.on('failed', async (job, err) => {
  if (job.attemptsMade >= job.opts.attempts) {
    await deadLetterQueue.add('failed-order', { originalJob: job.data, error: err.message });
    await alertOncall(`Job ${job.id} moved to DLQ: ${err.message}`);
  }
});
Real-world example An order-fulfillment system routes any order job that fails all five retry attempts into a dead-letter queue and triggers a Slack alert to the on-call engineer, who can inspect the specific failure reason and either fix a data issue and manually replay the job, or confirm it should be permanently discarded.

Common follow-ups: How do you decide an appropriate number of retries before a job should be considered permanently failed and moved to the DLQ?;What operational tooling or dashboard would you build around monitoring and reprocessing a dead-letter queue?