Imagine a NestJS app where CSV imports used to run inside the HTTP request. They timed out on big files, so the team moved them to a BullMQ queue. The API now responds immediately, and imports run in the background. A month later, three problems show up. One customer's import created every row twice. Forty failed jobs have been sitting in Redis for weeks without anyone noticing. And a deploy at lunchtime killed an import halfway through.
None of those are bugs in BullMQ. They come from treating a queue like a function call. A background job needs its own design for failure.
Reliable background jobs come down to seven habits:
- Assume every job can run more than once, and make it safe to repeat.
- Retry failures that might succeed later, and stop retrying ones that can't.
- Keep CPU-heavy work from stalling jobs.
- Configure Redis for a queue, not a cache.
- Keep failed jobs where someone will see them.
- Shut down workers without killing jobs mid-flight.
- Tell the user what's happening with a status they can check.
What belongs in a job
Move work to a queue when it is slow, can fail and be retried, or talks to something outside your control: sending emails, generating reports, processing uploads, calling webhooks, syncing with a third-party API. Keep work in the request when the user needs the result immediately to continue.
Keep job payloads small. Pass IDs and look up the data in the worker, rather than copying whole records into the job. Job data is stored in Redis as plain data and can stay there long after the job finishes, so keeping personal data out of payloads is simpler than cleaning it up later. That matters for teams handling EU personal data, where keeping copies you don't need is hard to justify.
The NestJS setup in brief
The NestJS queues documentation uses the @nestjs/bullmq package. You register a connection with BullModule.forRoot, register each queue with BullModule.registerQueue, and write a worker as a class decorated with @Processor that extends WorkerHost and implements process(). Worker events, such as a job failing, can be handled with @OnWorkerEvent.
Two things to know before you start:
- Version changes matter. As of November 2026, BullMQ is on version 6. The v5 to v6 migration guide replaces the older repeatable-jobs API with job schedulers, so tutorials written for earlier versions may not work as written.
- Workers don't have to run in the API process. Running them in a separate deployment lets you scale and restart them independently, and keeps heavy jobs from slowing down HTTP responses. The BullMQ NestJS guide covers the module setup.
Assume every job can run twice
The BullMQ documentation says every job is delivered to a worker at least once, and tells you to design processors to be idempotent. "At least once" means a job can run again after a crash, a stalled lock, or a retry that happens after the work actually succeeded.
Making a job safe to repeat usually means one of these:
- A unique constraint in the database. For example, one row per import ID and line number, so a second run can't insert duplicates.
- A recorded outcome. Store "email sent for order 123" and check it before sending again.
- An idempotency key for external calls. Many payment and messaging APIs accept a key so that repeating a request doesn't repeat the effect. It's the same idea used to stop duplicate form submissions.
The other side of this is when you add the job. If your API inserts a database row and enqueues a job in the same request, either can succeed without the other. If the transaction rolls back after the job is added, a worker may process data that doesn't exist. If the job is added only after the commit and the process crashes in between, the job is lost. Adding the job only after the commit works for many cases. When losing a job isn't acceptable, the transactional outbox pattern stores the job in the same database transaction and publishes it afterward.
Retries that help, not hurt
According to BullMQ's guide to retrying failing jobs, a failed job is not retried unless you set attempts. Set it on each job or as a queue default, and pair it with backoff:
- Fixed backoff waits the same time between attempts.
- Exponential backoff waits longer each time, which gives an overloaded service room to recover.
- A custom backoff strategy can choose the delay based on the error.
Not every error deserves a retry. A malformed file, a deleted record, or a permanently invalid email address will fail every time. BullMQ's guide to stopping retries describes UnrecoverableError: throw it, and BullMQ moves the job straight to the failed set without further attempts. Retry timeouts and rate limits; fail fast on bad input.
Stalled jobs: when a busy worker runs a job twice
This is the failure most tutorials skip. While a worker processes a job, it holds a lock and renews it periodically. If the lock expires, BullMQ assumes the worker died and treats the job as stalled. The stalled jobs guide explains that a stalled job is moved back to waiting to be processed again, and fails after it stalls too many times.
The trap is CPU-heavy work. BullMQ's sandboxed processors page explains that keeping the Node.js event loop busy prevents BullMQ from extending job locks, which leads to stalled jobs. Parsing a huge CSV synchronously or resizing images in the same thread can block long enough for the lock to expire. Then a second worker picks up the job while the first is still running it.
The defaults, as listed in the v6 worker options in November 2026:
lockDuration: 30,000 ms.stalledInterval: 30,000 ms.maxStalledCount: 1.concurrency: 1.
Ways to avoid it:
- Break CPU-heavy work into chunks and yield to the event loop between them, or stream the input instead of loading it all at once.
- Use a sandboxed processor, which runs the job in a separate process or worker thread so the main loop can keep renewing locks. In NestJS, a sandboxed processor runs outside the dependency injection container, so it can't use your injected services directly.
- Raise the lock duration with
lockDurationonly if you understand that a crashed worker's job will then wait longer before being retried.
And because a stalled job can run again, this is one more reason every job must be idempotent.
Redis settings for a queue
Redis is often set up as a cache, and a cache is allowed to throw data away. A queue is not. BullMQ's going to production guide calls it very important to set maxmemory-policy to noeviction, so Redis returns errors when memory is full instead of silently deleting keys. The same guide recommends AOF persistence so jobs survive a Redis restart.
Two connection settings matter, per the connections guide:
- Workers: with ioredis, set
maxRetriesPerRequesttonull, so a worker keeps waiting through a Redis blip instead of erroring out. - Producers in HTTP handlers: keep a finite value, so an API request fails fast if Redis is down instead of hanging.
On Azure, check the defaults. Microsoft's documentation for Azure Managed Redis and for Azure Cache for Redis lists volatile-lru as the default eviction policy, not noeviction. Change it before you put a queue on it. Azure Managed Redis is clustered by default, and BullMQ's Redis Cluster guidance says to use a queue prefix with a hash tag, such as wrapping the prefix in braces, so a queue's keys land on the same node. Data persistence on Azure Managed Redis requires high availability, per its persistence documentation.
If you're choosing a Redis service now, note Microsoft's retirement FAQ: Azure Cache for Redis Basic, Standard, and Premium retire on September 30, 2028, and the Enterprise tiers on March 31, 2027, with Azure Managed Redis as the replacement.
Don't keep jobs forever, and don't lose the failures
By default, BullMQ keeps completed and failed jobs indefinitely. The auto-removal guide shows removeOnComplete and removeOnFail, which can keep a set number of jobs or keep them for a set age. Without them, Redis memory grows until something breaks, and with noeviction that something is new jobs being rejected.
Treat the two differently:
- Completed jobs: keep a short history for debugging, then let them go.
- Failed jobs: keep them longer. The failed set is your dead-letter list: the place to see what failed, why, and to retry once the cause is fixed. Removing them quickly hides the problems you most need to see.
One caveat if you use custom job IDs to prevent duplicates. The job IDs guide explains that a job with the same ID is ignored only while the original job still exists. Once the original is removed, the same ID can be added again. So retention settings affect how long that duplicate protection lasts. BullMQ also has a separate deduplication option with its own time window.
Seeing failures before customers do
A queue fails quietly unless you watch it. The signals worth alerting on:
- Failed jobs: a rising count in a queue, or any failure in a critical queue.
- The oldest waiting job: a backlog that keeps aging means workers are down or too slow. The total count alone can look normal while jobs wait for hours.
- Overdue delayed jobs: the job getters guide notes that waiting counts don't include delayed jobs, even ones whose delay has passed, so check them separately.
- Stalled events: frequent stalls point to blocking work or overloaded workers.
For tracing, BullMQ's telemetry guide describes OpenTelemetry support, which connects a job to the request that created it. For a dashboard, Bull Board has a NestJS adapter. It can show job data and retry or delete jobs, so put it behind authentication; it isn't something to expose publicly.
Shutting down without killing jobs
Deploys and scale-downs stop workers. If a worker stops in the middle of a job, the job eventually stalls and runs again, which is safe only if the job is idempotent.
A graceful shutdown lets current jobs finish. BullMQ's graceful shutdown guide describes worker.close(), which stops taking new jobs and waits for active ones. In NestJS, the BullMQ module closes workers when the application shuts down, but Nest only runs those hooks on a signal like SIGTERM if you call enableShutdownHooks(). The lifecycle events documentation notes that shutdown hooks are disabled by default.
The other half is the platform's grace period. docker stop sends SIGTERM and then SIGKILL after 10 seconds by default on Linux, per the Docker CLI reference. Kubernetes waits 30 seconds by default, per its pod termination documentation. A job that takes five minutes won't finish in either. Options:
- Raise the grace period for worker containers to cover a typical job.
- Split long jobs into smaller steps that each finish quickly.
- Save progress as the job goes, so a rerun continues from the last checkpoint instead of starting over.
Showing progress in React
When the API hands work to a queue, it shouldn't pretend the work is done. HTTP status 202 Accepted, defined in RFC 9110, means the request was accepted for processing but hasn't completed. The specification suggests the response point to a status monitor.
A simple pattern:
- The API adds the job and responds with 202 and a job ID or status URL.
- The worker reports progress with
job.updateProgress(). - A status endpoint returns the job's state and progress. React polls it every few seconds, or subscribes through server-sent events or a WebSocket fed by BullMQ's queue events.
- When the job completes or fails, React shows the result or a clear error, with a retry option if it makes sense.
Return your own status model rather than BullMQ's raw job object, so you can change queue details later without breaking the frontend.
Checklist
- Keep payloads small: IDs, not full records or personal data.
- Make every job idempotent: unique constraints, recorded outcomes, or idempotency keys.
- Add jobs after the commit, or use an outbox when losing a job isn't acceptable.
- Set attempts and backoff, and throw
UnrecoverableErrorfor errors that can't succeed. - Keep CPU-heavy work off the main thread, or chunk it, to avoid stalled jobs.
- Set Redis to noeviction and enable persistence. Check the Azure defaults.
- Set worker connections to retry indefinitely and producer connections to fail fast.
- Use hash-tag prefixes on clustered Redis.
- Set removeOnComplete and removeOnFail, keeping failures longer than successes.
- Alert on failures, the oldest waiting job, overdue delayed jobs, and stalls.
- Protect any queue dashboard with authentication.
- Call enableShutdownHooks and match the container grace period to your jobs.
- Return 202 with a status endpoint, and show progress in React.



