home / skills / hoangnguyen0403 / agent-skills-standard / nestjs-bullmq
This skill helps you implement robust, type-safe background jobs in NestJS using BullMQ with producer-consumer pattern.
npx playbooks add skill hoangnguyen0403/agent-skills-standard --skill nestjs-bullmqReview the files below or copy the command above to add this skill to your agents.
---
name: NestJS BullMQ Implementation
description: Standard workflow for implementing background jobs using BullMQ in NestJS.
metadata:
labels: [nestjs, bullmq, background-jobs, queue, redis]
triggers:
files: ['package.json', '**/*.module.ts']
keywords: [queue, backlog, background job, async task, worker]
---
# NestJS BullMQ Implementation
## **Priority: P0 (Standard)**
Implement type-safe, robust background jobs using the `Producer-Consumer` pattern with `@nestjs/bullmq`.
## Structure
```text
src/modules/{feature}/
├── {feature}-queue.service.ts # Producer: Schedules jobs
├── {feature}.processor.ts # Consumer: Processes jobs
└── {feature}.module.ts # Config: Registers queues
```
## Implementation Guidelines
- **Use Producers**: create a dedicated `QueueService` to wrap `queue.add()`.
- **Use Consumers**: extend `WorkerHost` and decorate with `@Processor(QUEUE_NAME)`.
- **Enforce Types**: Define interfaces for `JobData` to ensure payload safety.
- **Manage Lifecycle**: Set `removeOnComplete: true` to prevent Redis memory bloat.
- **Handle Errors**: Use `removeOnFail` limit or Dead Letter Queues (DLQ) for debugging.
- **Unique IDs**: Provide deterministic `jobId` if you need to cancel/deduplicate jobs later.
## Anti-Patterns
- **No Strings**: Define queue names as constants, not inline strings.
- **No Heavy Logic**: Do not put complex business logic in `QueueService`; only schedule.
- **Avoid Implicit cleanup**: Always configure job cleanup policies explicitly.
- **No Direct Redis**: Use `InjectQueue` annotations, avoided raw Redis commands.
## Reference & Examples
For complete code patterns: [references/patterns.md](references/patterns.md)
This skill describes a standard, type-safe approach for implementing background jobs in NestJS using BullMQ. It defines a clear Producer-Consumer pattern, module layout, and operational rules to keep job processing robust and maintainable. The guidance focuses on lifecycle, error handling, and avoiding common anti-patterns.
Create a QueueService per feature to act as the producer and push typed job payloads with queue.add(). Implement consumers by extending WorkerHost and decorating with @Processor(QUEUE_NAME) to process jobs. Register queues in the feature module and use lifecycle options like removeOnComplete and removeOnFail or a Dead Letter Queue to manage Redis state and failures.
How should I choose between removeOnFail and a Dead Letter Queue?
Use removeOnFail for short-term failures you don’t need to inspect; use a DLQ when you need to preserve failed payloads for debugging, replay, or manual handling.
Where should complex business logic live?
Keep complex logic out of the QueueService. Implement business rules in processors or dedicated application services invoked by the processor to keep producers simple and testable.