Skip to content
pushqdocs
Get started

This pattern turns a dead-letter queue (DLQ) into a parking lot: tasks that run out of retries are kept in a paused DLQ, where nothing is delivered, until you have fixed the cause and decide to replay them or throw them away. It covers setting up the DLQ, inspecting dead-lettered tasks, the three ways to replay, and cleaning up.

Create the DLQ first, with the same target as the source queue, and pause it. Then point the source queue at it.

const target = { baseUrl: "https://api.example.com", signingSecret: process.env.PUSHQ_SIGNING_SECRET };
// 1. The DLQ: same target as the source, paused so nothing is delivered until you decide.
await pq.upsertQueue("emails-dead", { target, retry: { maxAttempts: 3 } });
await pq.pauseQueue("emails-dead");
// 2. The source queue: tasks that exhaust their retries are copied to the DLQ.
await pq.upsertQueue("emails", {
target,
retry: { maxAttempts: 5 },
deadLetterQueue: "emails-dead",
});

The examples assume a client named pq (see install and configure an SDK). The text uses JSON field names; the SDKs use their language’s casing (deadLetterQueue in TypeScript and Java, dead_letter_queue in Python, DeadLetterQueue in Go and .NET).

Use this when failed work must not be lost and must not be retried blindly: payments, order fulfilment, outbound webhooks to customers, anything where you want a human to look before it runs again.

Running this setup again is safe: upsert never changes a queue’s state, so the DLQ stays paused.

Why the dead-letter queue must match the source target

Section titled “Why the dead-letter queue must match the source target”

When a task fails for the last time, pushq copies it into the DLQ as a new task: same url or path, method, headers and body, plus three X-Pushq-Deadletter-* headers. The copy does not take the source queue’s target or the task’s own retry override:

  • A path-based task (/hooks/send-welcome) is resolved against the DLQ’s target.base_url. Without one, every delivery fails with a config error.
  • The DLQ’s target.headers and signing_secret are used, not the source’s. If your endpoint checks a static header or a webhook signature, give the DLQ the same ones.
  • The DLQ’s retry settings apply to the copy.

That’s why the setup reuses one target for both queues. See what is copied to the dead-letter queue for the full list.

List the DLQ with the full view to see bodies and headers. The original task is still in the source queue in the failed state (for 7 days on a default self-hosted server), with its attempt history, which tells you why it failed.

const page = await pq.listTasks("emails-dead", { view: "full", pageSize: 100 });
for (const t of page.items) {
const source = t.headers?.["X-Pushq-Deadletter-Source-Queue"]!;
const originalId = t.headers?.["X-Pushq-Deadletter-Task-Id"]!;
console.log(t.id, source, originalId, t.headers?.["X-Pushq-Deadletter-Final-Status"]);
const attempts = await pq.listAttempts(source, originalId);
for (const a of attempts) console.log(" ", a.number, a.responseStatus ?? a.errorKind, a.errorSnippet);
}

The three dead-letter headers:

Header Meaning
X-Pushq-Deadletter-Source-Queue The queue the task failed in.
X-Pushq-Deadletter-Task-Id The original task’s ID in that queue.
X-Pushq-Deadletter-Final-Status The last attempt’s HTTP status (500), or its error kind when there was no response (timeout, connect, dns, tls, config, lease_lost).

To alert when tasks start dead-lettering, watch the DLQ’s pending count in queue stats. On a paused DLQ, every task is pending until you act.

Fix the cause first: deploy the handler fix, restore the downstream service, or correct the queue config. Then pick one of three ways to replay.

POST /v1/queues/emails-dead/tasks/{id}:run delivers one DLQ task now, even though the DLQ is paused. Use it to check that the fix works before replaying the rest.

Resume the DLQ. Its pending tasks are delivered to the DLQ’s target (the same endpoint, given the setup above) at the DLQ’s rate limits, with the DLQ’s retry settings. When the DLQ’s pending count reaches zero, pause it again so it goes back to collecting.

await pq.runTask("emails-dead", "0193a1b2-7c4d-7e8f-9a0b-1c2d3e4f5a6b"); // try one
await pq.resumeQueue("emails-dead"); // then all of them
// ...when queueStats("emails-dead").pending is 0:
await pq.pauseQueue("emails-dead");

While the DLQ is running, tasks that newly fail in the source queue are delivered from the DLQ right away too. Replayed copies that fail again follow the DLQ’s retry settings and end up failed in the DLQ (or in the DLQ’s own dead_letter_queue, if you set one).

To replay with the source queue’s rate limits and retry settings, read each DLQ task and create a new task in the source queue with the same url or path, method, headers and body, then delete the DLQ copy. Deriving the new ID from the DLQ copy’s ID (replay-<id>) makes the script safe to run again after a crash: a task that was already re-created comes back as already_exists.

import { PushqError } from "pushq";
const page = await pq.listTasks("emails-dead", { view: "full", pageSize: 100 });
for (const t of page.items) {
const source = t.headers!["X-Pushq-Deadletter-Source-Queue"];
try {
await pq.createTask(source, {
taskId: `replay-${t.id}`,
...(t.url.startsWith("/") ? { path: t.url } : { url: t.url }),
method: t.method,
headers: t.headers,
body: t.body,
});
} catch (err) {
if (!(err instanceof PushqError && err.code === "already_exists")) throw err;
}
await pq.deleteTask("emails-dead", t.id);
}

This replays the first 100 tasks; loop over next_page_token for more, or run it again until the DLQ is empty. The X-Pushq-Deadletter-* headers come along with the other headers, so your handler can tell a replay apart (they are replaced, not duplicated, if the task dead-letters again).

To throw away everything left in the DLQ (tasks you decided not to replay), purge it. Purge deletes every task created up to that moment and keeps the queue and its paused state, so it keeps collecting new failures.

await pq.purgeQueue("emails-dead");

To drop only some tasks, delete them one by one with DELETE /v1/queues/emails-dead/tasks/{id}.

  • DLQ created after the source queue starts failing. Copies made before the DLQ existed are lost. Create the DLQ first.
  • DLQ without the source’s base_url, headers or signing secret. Replays by resume fail with config errors or are rejected by your endpoint.
  • Resuming and forgetting. A running DLQ delivers new failures immediately. Pause it again after the replay.
  • Replaying before fixing. The tasks fail again and use up the DLQ’s retries. Try one with run first.
  • Using upsertQueue with a partial config on the source queue. Upsert replaces the whole config, and an omitted dead_letter_queue is removed. Always include it (see per-tenant queues).