Schedule icon
Webhook icon
Assert icon
RedisCLI icon
Docker icon
Script icon
Set icon
If icon
Pause icon
Log icon
SlackIncomingWebhook icon

BullMQ Stalled Job Recovery, Requeue and Dead-Letter Quarantine Guard

Recover stalled BullMQ jobs in Redis. Detect crashed worker locks, requeue recoverable jobs, isolate poison pills to dead-letter storage, and audit recovery.

Categories
DataInfrastructure

Production Node.js and TypeScript microservices frequently rely on BullMQ and Redis for background job execution, including payment webhooks, notifications, PDF generation, and asynchronous AI inference pipelines. Under normal conditions, a worker process claims a job from the bull:<queue>:wait list, moves it to bull:<queue>:active, and acquires a lock key (bull:<queue>:<jobId>:lock) that it renews periodically until execution completes.

When a worker process crashes abruptly due to out-of-memory (OOM) killer terminations, unhandled promise rejections, native binary segmentation faults, or node redeployments, the worker vanishes without acknowledging or releasing the job. The lock key expires, but the job identifier remains stranded in bull:<queue>:active. Because BullMQ's built-in stalled-job recovery runs exclusively inside active worker processes, an outage that terminates workers leaves these jobs frozen indefinitely. Furthermore, if a job failed due to a poison-pill payload that crashes any worker attempting to process it, standard worker restarts trigger an infinite crash-and-restart loop.

This blueprint provides an external, declarative BullMQ stalled job supervisor. It inspects the active list, verifies worker lock status, classifies stranded jobs into recoverable versus poison-pill candidates, safely requeues recoverable items back to wait, isolates retry-exhausted poison jobs to the dead-letter failed storage, verifies post-remediation queue clearance, records audit history in Kestra KV, and alerts on Slack.

How it works

  1. Pre-flight Parameter Assertion: The validate_inputs task (io.kestra.plugin.core.execution.Assert) verifies that input arguments meet operational limits, including a non-empty queue name and positive thresholds.
  2. Active Job & Lock Inspection: The detect_stalled_jobs task (io.kestra.plugin.redis.cli.RedisCLI) executes an atomic Lua script that reads bull:<queue>:active, checks the existence of bull:<queue>:<jobId>:lock, and inspects each unleased job's metadata hash (processedOn, attemptsMade, and opts).
  3. Differential Trend Analysis & KV History: The evaluate_stalled_trend task (io.kestra.plugin.scripts.python.Script) correlates current unacknowledged jobs against historical snapshots stored in Kestra KV under bullmq_stalled_tracker_{queue_name}. It identifies whether stalled jobs have persisted across multiple audit cycles and partitions items into requeue_candidates and quarantine_candidates.
  4. KV Snapshot Persistence: The save_state_tracker_kv task (io.kestra.plugin.core.kv.Set) writes the updated snapshot into Kestra KV.
  5. Stalled Jobs Gate: The handle_stalled_jobs_gate task (io.kestra.plugin.core.flow.If) branches based on whether any unleased stalled jobs were identified.
  6. Approval Gate & Dry-Run Protection: When dry_run is disabled and auto_remediate is enabled, the workflow pauses execution (io.kestra.plugin.core.flow.Pause) for operator sign-off before modifying Redis queue keys.
  7. Safe Remediation Execution: The remediate_stalled_jobs task (io.kestra.plugin.redis.cli.RedisCLI) runs remediate.lua to remove stranded jobs from active via LREM. Recoverable jobs are prepended to bull:<queue>:wait with incremented attemptsMade, while retry-exhausted poison items are moved to the bull:<queue>:failed ZSET with failure metadata.
  8. Post-Remediation Verification: The verify_remediation task (io.kestra.plugin.redis.cli.RedisCLI) re-reads bull:<queue>:active and confirms that all remediated job IDs have been evacuated. The assert_remediation_success task fails the run if any targeted job remained in active.
  9. Audit Manifest & Slack Notification: The recovery manifest is archived into Kestra KV under bullmq_stalled_audit_{queue_name}, and prioritized incident alerts are dispatched to Slack (io.kestra.plugin.slack.notifications.SlackIncomingWebhook).

What you get

  • Automated detection of abandoned BullMQ jobs stranded in active state following worker crashes.
  • Elimination of recursive worker crash loops by quarantining poison pills to the failed dead-letter queue.
  • Differential trend tracking across consecutive runs using Kestra KV store to verify true worker abandonment.
  • Human-in-the-loop approval gate (io.kestra.plugin.core.flow.Pause) preventing unintended mutations.
  • Atomic Redis operations via Lua preventing race conditions with concurrently running workers.
  • Post-remediation verification ensuring zero lingering targeted jobs remain in active.

Who it's for

  • Platform Engineers and SREs maintaining production Redis and Node.js microservices infrastructure.
  • Backend Engineers operating BullMQ, Bull, or Celery asynchronous task queues.
  • Incident Response Teams triaging silent task processing delays and crashed worker workloads.

Why orchestrate this with Kestra

BullMQ's native stalled-check logic runs inside worker processes. If all workers on a queue crash or enter crash loops, BullMQ cannot heal itself from the outside. Writing standalone shell scripts or cron jobs creates unmonitored points of failure without execution audit logs, secret masking, approval gates, or differential history tracking. Orchestrating this guard with Kestra delivers a declarative workflow with native Redis CLI execution, atomic KV state tracking, interactive pause-for-approval mechanics, post-action verification, and automated Slack escalation.

Prerequisites

  • An accessible Redis instance (version 6.2 or higher, Redis 7.x recommended) reachable from the Kestra worker.
  • BullMQ queue keys created on the target Redis instance (e.g. bull:notifications:active, bull:notifications:wait).
  • A Slack Incoming Webhook URL to receive triage alerts.
  • Standalone queues without BullMQ Flows (parent/child job dependencies). This guard performs the minimal data-level mutation (list/ZSET membership, attempt counter, failure metadata) but does not replicate BullMQ's own internal event-stream emission or parent/child dependency bookkeeping, so QueueEvents listeners won't fire for its actions and a parent job waiting on a quarantined child's completion won't be unblocked by it.

Secrets

  • REDIS_PASSWORD: Secret authentication password for Redis server access (leave empty if unauthenticated).
  • SLACK_WEBHOOK_URL: Slack Incoming Webhook endpoint URL used for operational notifications and alerts.
  • BULLMQ_GUARD_WEBHOOK_KEY: Secret authentication token protecting the on-demand Webhook trigger.

Quick start

  1. Configure REDIS_PASSWORD, SLACK_WEBHOOK_URL, and BULLMQ_GUARD_WEBHOOK_KEY in your Kestra namespace secrets.
  2. Import this blueprint and set queue_name to match your BullMQ queue (e.g. notifications, orders-export, email-dispatch).
  3. Run a manual execution with dry_run: true to record an initial offset baseline and review detected active jobs.
  4. When stalled jobs are detected, set auto_remediate: true and dry_run: false to execute verified recovery.
  5. Enable the periodic_stalled_job_guard schedule trigger to monitor the queue continuously every 10 minutes.

How to extend

  • Automated Container Restart: Add an io.kestra.plugin.kubernetes.pod.Restart task to bounce crashed worker pods once poison jobs have been quarantined.
  • Dead-Letter Storage S3 Archival: Stream quarantined poison job payloads to Amazon S3 using io.kestra.plugin.aws.s3.Upload for developer debugging.
  • PagerDuty Incident Escalation: Integrate io.kestra.plugin.pagerduty.notifications.PagerDutyAlert to trigger high-priority on-call pages for persistent queue stalls.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.