As web applications evolve, offloading heavy processing (PDF generation, video transcode, webhook dispatch, email notifications) out of the HTTP request cycle into background workers is essential for maintaining snappy response times.

In the Node.js and TypeScript ecosystem, BullMQ backed by Redis is the premier choice. However, naive configurations frequently cause production headaches: duplicate jobs, memory exhaustion, and hung worker threads.

Here is our production hardening blueprint.


Architectural Isolation: Dedicated Queues & Concurrency

Never route all background tasks through a single catch-all queue. A surge in slow report exports will starve urgent transactional password-reset emails.

Architecture Schematic
[Web API]
    │
    ├──► [Queue: critical-auth] ──► Dedicated Workers (Concurrency: 10, Lock: 5s)
    ├──► [Queue: media-transcode] ─► Isolated GPU Workers (Concurrency: 2, Lock: 120s)
    └──► [Queue: webhooks] ────────► Rate-Limited Workers (Exponential Backoff)

Production Worker Hardening Pattern

typescript
import { Worker, Queue, Job } from "bullmq";
import Redis from "ioredis";

const connection = new Redis(process.env.REDIS_URL!, {
  maxRetriesPerRequest: null,
  enableReadyCheck: false,
});

export const documentProcessingWorker = new Worker(
  "document-processing",
  async (job: Job<{ documentId: string; matterId: string }>) => {
    // 1. Enforce distributed idempotency lock
    const lockAcquired = await acquireJobLock(job.id!);
    if (!lockAcquired) {
      console.warn(`Duplicate job ${job.id} detected; skipping execution`);
      return;
    }

    try {
      await processLegalDocument(job.data.documentId);
    } catch (err) {
      console.error(`Job ${job.id} failed on attempt ${job.attemptsMade}`, err);
      throw err; // Re-throw to trigger BullMQ backoff retry
    }
  },
  {
    connection,
    concurrency: 5,
    limiter: {
      max: 50,
      duration: 1000, // 50 jobs per second max
    },
    lockDuration: 30000, // 30 second lock before heartbeat renewal
  }
);

Avoiding Redis Memory Exhaustion

By default, BullMQ stores completed job metadata indefinitely unless instructed otherwise. In high-throughput systems, this will rapidly consume gigabytes of Redis RAM.

Always configure automatic job retention pruning:

typescript
export const defaultJobOptions = {
  removeOnComplete: {
    age: 3600 * 24, // Keep completed jobs for 24 hours only
    count: 1000,    // Keep maximum 1000 completed records
  },
  removeOnFail: {
    age: 3600 * 24 * 7, // Keep failed jobs for 7 days for forensic analysis
  },
  attempts: 4,
  backoff: {
    type: "exponential",
    delay: 2000,
  },
};