Dead-letter queue
Cap crash-recovery with maxRecoveryAttempts so a poison-pill run moves to the terminal dead status instead of crash-looping forever, then route dead runs with engine.onDead or a DLQ workflow — an inline @DeadLetter() method, a per-workflow deadLetterWorkflow reference, or the module-level default — to alert, compensate, or queue for review.
Most failures are transient and recover cleanly. But occasionally a run is a poison pill: it crashes the process every time recovery picks it up — a deserialization bug, a non-deterministic change, an infinite loop in a step. Left alone, crash-recovery would resume it, crash, resume it again on the next boot, and loop forever, taking the whole instance down with it each time. The dead-letter queue breaks that loop.
The walkthrough below follows one poison-pill run the whole way: it crashes, crash-recovery keeps
resuming it until it hits maxRecoveryAttempts, the engine moves it to the terminal dead status —
and then a @DeadLetter() handler picks it up as a durable run of its own to page and file a ticket.
Note the two lanes: the poisoned run ends on its lane — dead, in red — while the DLQ handler is a
separate run living on the lane below.
1// a poison-pill run, and the handler that catches it2@Workflow({ name: 'pipeline', version: '3' })3export class PipelineWorkflow {4 constructor(5 private readonly alerts: AlertsService,6 private readonly tickets: TicketService,7 ) {}8 9 async run(ctx: WorkflowCtx, input: PipelineInput) {10 const extracted = await ctx.step('pipeline.extract', input);11 // a deserialization bug here throws on every recovery — a poison pill12 return ctx.step('pipeline.transform', extracted);13 }14 15 // Auto-registered as 'pipeline.dlq' — a durable workflow of its own16 @DeadLetter()17 async onDead(ctx: WorkflowCtx, dl: DeadLetter<PipelineInput>) {18 await ctx.step(this.alerts.page, { runId: dl.deadRunId, error: dl.error?.message });19 const ticket = await ctx.step(this.tickets.create, { payload: dl.input });20 return { ticketId: ticket.id };21 }22}And as a live system — inject a poison pill and watch it crash-loop, get dead-lettered, and hand off to the DLQ handler while the healthy traffic never stops:
recovery ×N counter), and past maxRecoveryAttempts it drops to the dead tray — parked, inspectable — which starts the pipeline.dlq handler run to page and file a ticket. Meanwhile the healthy traffic above never stops flowing.Capping recovery: maxRecoveryAttempts
Crash-recovery is self-healing and periodic: while a run executes its worker renews a recovery lease,
so a crashed worker's lease expires and recovery (engine.recoverIncomplete(), called every tick by the
TimerPoller) reclaims the orphaned run within ~leaseMs — on any instance, not just on the next boot.
That's what makes a genuine poison pill visible: it gets picked up again and again. Every time
crash-recovery picks up a still-running run, the engine increments that run's recoveryAttempts
before resuming it (so a crash mid-resume still advances the counter). Set
maxRecoveryAttempts — an engine/module option — and once a run exceeds it, instead of resuming yet again
the engine moves the run to the terminal dead status with a max_recovery_attempts error, releases its
lease, and stops touching it:
DurableModule.forRoot({
store,
transport,
maxRecoveryAttempts: 5, // after 5 crash-recoveries, dead-letter the run instead of looping
});A workflow turn dispatched to a worker (a co-located or remote worker over BullMQ) spends the same
budget when it is lost. While its turn is on the broker the run waits suspended, not running, so
crash-recovery does not see it; the timer poller judges it instead, once the turn's re-drive deadline
(remoteAdvanceSilenceMs, else reconcileMs) has passed with no decision and no heartbeat:
- Counted once per turn. The count marks the awaited turn as lost, so it is not counted again however long the re-drive waits (on the run queue, say); the re-drive dispatches the next turn.
- Only after a worker took the task. A task still waiting unstarted in the broker (a backed-up queue) is not a lost turn: it is neither counted nor dispatched again, and its deadline is pushed out one window. A task a worker started (running, or stalled back to the queue for redelivery), or one the broker no longer has, counts. A transport that cannot tell counts every lapse. During a rolling deploy from an earlier release, a task still queued under the job id that release gave it reads as one the broker no longer has, so it is counted once.
- Only by an instance that re-drives it. An instance that cannot resolve the workflow counts nothing and leaves the run's unregistered grace clock running.
- Not right after a restart. Within one turn window of an engine's boot, a deadline that went stale while it was down is pushed out instead, so live workers' heartbeats can reach the new engine first. Once per turn: if the fleet restarts again before then, the next lapse is judged normally, so restarts cannot postpone a lost turn for ever. A heartbeat from the turn's worker gives the grace back, so a long turn that stays alive across several deploys is graced at each one instead of counted.
- Turns lost in a row. An applied decision resets
recoveryAttemptsto 0, so a long-lived run that loses a turn now and then (a deploy, say) is not dead-lettered for losses spread over its life.
Past the limit the run ends dead with the same max_recovery_attempts error and the same
announcement as an inline run; the write is conditional on the run still being suspended, so a
cancel that lands meanwhile wins. A turn counted as lost whose decision arrives late, before the
re-drive dispatches the next turn, is still applied, and gives the attempt back; a re-drive wake
still waiting on the run queue then does nothing. A decision that arrives while a pass holds the run's
lease (to count, grace or leave a queued turn waiting) is dropped (a narrow race): the next lapse
counts the turn and runs it again. A decision that arrives in time, a worker that keeps beating, and
the checkpoints a healthy turn writes never count.
The awaited-turn marker is the dispatched task id (<runId>:wf:<8 hex>) plus up to 11 characters
(:grace:lost), so runId plus 23 in all. With a store that bounds the column, such as TypeORM's
varchar(191), a run whose id is 169 to 179 characters long is dispatched but cannot be graced or
counted.
If the dead run is a child, its parent is notified at once, as for a failed child: ctx.child
throws, and ctx.all / ctx.allSettled record it as failed. The parent does not wait for its
reconcile wake.
Omit maxRecoveryAttempts for unlimited recovery (the default). A dead run is terminal but not lost:
it stays fully inspectable in the dashboard — its history, its checkpoints, its error — and it can be
retried from there once you've shipped a fix. The point is that one poison pill no longer keeps the process
crashing for every other run.
An unregistered workflow
A run also goes dead when its workflow@version is registered on no instance. During a rolling
deploy, a pod that lacks the version skips the run on purpose (skew protection), and the pod still
on the prior version picks it up within seconds. When a run is still unresumable well after that,
its workflow was renamed or deleted, and nothing will ever run it. Once it has been due for
unregisteredWorkflowGraceMs (counted from its wakeAt for a parked run, from its last update
otherwise), the engine moves it to dead with a workflow_not_registered error instead of retrying
and logging it every tick:
DurableModule.forRoot({
store,
transport,
unregisteredWorkflowGraceMs: 6 * 60 * 60 * 1000, // default 24h; 0 skips such runs forever
});Raise it if a pool can legitimately stay down longer than that while another app sharing the
store, with no namespace of its own, polls its runs.
Handling dead runs: engine.onDead
Parking a dead run is the floor, not the ceiling. Subscribe with engine.onDead to be notified the moment a
run is dead-lettered — the listener receives the dead WorkflowRun (status dead, with its error) — so you
can do something active: page an on-call engineer, push to a real message queue, or kick off a workflow that
handles it.
@Injectable()
export class DeadLetterAlerts implements OnModuleInit {
private readonly logger = new Logger(DeadLetterAlerts.name);
constructor(
private readonly engine: WorkflowEngine,
private readonly pager: PagerService,
) {}
onModuleInit() {
this.engine.onDead((run) => {
this.logger.error(`run ${run.id} (${run.workflow}) dead-lettered: ${run.error?.message}`);
void this.pager.alert('durable-poison-pill', { runId: run.id, workflow: run.workflow });
});
}
}onDead returns an unsubscribe function. It fires on the instance that dead-letters the run.
Routing to a DLQ workflow
The most powerful option is to route dead runs into a workflow of their own — a durable handler that gets
all the reliability machinery (retries, steps, its own observability) to triage the failure. The handler is
started with a typed DeadLetter<TInput> payload, idempotent by a dlq:<runId> id so a run is never
double-handled:
import type { DeadLetter } from '@dudousxd/nestjs-durable';
interface DeadLetter<TInput = unknown> {
deadRunId: string; // the dead run — inspectable + retriable in the dashboard
workflow: string; // the workflow whose run died
input: TInput; // the original input it was started with
error?: StepError; // the failure that moved it to `dead`
}Dead-lettering is naturally per-workflow — a dead checkout run wants a different handler than a dead
pipeline run — so the target is resolved per dead run, in this order:
- an inline
@DeadLetter()method on the workflow class — the co-located default; - the workflow's
@Workflow({ deadLetterWorkflow })reference to another registered workflow; - the module-level
deadLetterWorkflowdefault, for everything that declares neither.
Inline @DeadLetter() — one file
The simplest case: a method on the workflow class, marked @DeadLetter(). It runs as a durable workflow in
its own right (auto-registered as <name>.dlq, with checkpointing and a dashboard run), and it shares the
class's injected dependencies — so the handler lives right next to the workflow it protects:
@Workflow({ name: 'pipeline', version: '3' })
export class PipelineWorkflow {
constructor(
private readonly alerts: AlertsService,
private readonly tickets: TicketService,
) {}
async run(ctx: WorkflowCtx, input: PipelineInput) {
const extracted = await ctx.step('pipeline.extract', input);
const transformed = await ctx.step('pipeline.transform', extracted);
return ctx.step('pipeline.load', transformed);
}
// Runs when a `pipeline` run is dead-lettered. Typed to the workflow's own input, same deps.
@DeadLetter()
async onDead(ctx: WorkflowCtx, dl: DeadLetter<PipelineInput>) {
await ctx.step(this.alerts.page, {
event: 'durable-dead-letter',
runId: dl.deadRunId,
workflow: dl.workflow,
error: dl.error?.message,
});
const ticket = await ctx.step(this.tickets.create, {
title: `Dead-lettered run ${dl.deadRunId} (${dl.workflow})`,
body: dl.error?.message ?? 'unknown error',
payload: dl.input, // typed PipelineInput, ready to replay after a fix
});
return { ticketId: ticket.id };
}
}@Workflow({ deadLetterWorkflow }) — a separate workflow
When the DLQ handler is large, reused across workflows, or just deserves its own file, point at another registered workflow — by class (refactor-safe) or by name (for a cross-runtime handler). Register it as a normal provider like any other workflow:
@Workflow({ name: 'pipeline', version: '3', deadLetterWorkflow: PipelineDlqWorkflow })
export class PipelineWorkflow {
/* ...run... */
}
@Workflow({ name: 'pipeline-dlq', version: '1' })
export class PipelineDlqWorkflow {
async run(ctx: WorkflowCtx, dl: DeadLetter) {
/* same handler body as above */
}
}Module-level default
Set the module's deadLetterWorkflow to catch every workflow that declares neither of the above — a single
backstop DLQ for the whole app:
DurableModule.forRoot({
store,
transport,
maxRecoveryAttempts: 5,
deadLetterWorkflow: 'global-dlq', // fallback for workflows without their own
});A workflow that declares both an inline @DeadLetter() method and a deadLetterWorkflow reference is a
configuration error — two targets for one workflow is ambiguous, so the module fails fast at boot rather than
silently picking one. Pick one.
Because every start is idempotent by dlq:<runId>, a run that gets dead-lettered (and re-detected) more than
once still triggers exactly one DLQ run.
The dashboard links the two ends of this relationship: from a dead run you can see the DLQ run it spawned,
and the DLQ run carries the originating deadRunId — so you can walk from a poison pill to its handler and
back, retry the original once it's fixed, and confirm the DLQ run did its job.
On a tenant
On a DurableModule tenant (a connection, no store) the same declarations work. The tenant's
worker serves an inline @DeadLetter() method as <name>.dlq and announces it as the workflow's
route (else its deadLetterWorkflow reference, else the tenant's module-level default). When a run
of that workflow is dead-lettered on the control plane, the control plane starts the route in the
run's partition as dlq:<runId>, so the handler runs on the tenant, next to the workflow. The
control plane's own deadLetterWorkflow default does not apply to a workflow whose workers announce
a route. See What a tenant gets.
Routing a run's failure — @OnError
Route a failed run to a handler workflow by error code, and have the run settle with the handler's outcome — orchestrated by the engine, so it works the same for inline, in-app-worker and remote (Python) bodies, and a fan-out over the workflow keeps going when one item fails.
Stalled runs
Get paged about stranded runs instead of hearing about them from a customer. engine.onStalled (or the DurableModule onStalled option) plus stalledAfterMs turn "suspended forever" and "dispatched and lost" into an alert, once per stranded episode; listStalledRuns, findStalledRuns and durable inspect --stale are the same query on demand.