patternjavascriptTip
BullMQ Priority Queues
Viewed 0 times
priority queuebullmq priorityjob priorityhead of line blockingqueue ordering
Problem
All jobs in the queue are treated equally, so a low-priority background task (e.g., generating a monthly report) can block a high-priority user-facing task (e.g., sending a password reset email).
Solution
Add jobs with a priority value (lower number = higher priority in BullMQ). Workers process higher-priority jobs first. Separate critical tasks into separate queues with dedicated workers for hard isolation.
Why
Priority queues prevent head-of-line blocking where low-priority long-running jobs delay critical user-facing ones.
Gotchas
- BullMQ priority is a soft guarantee under high load — if workers are all busy, high-priority jobs still wait. For hard guarantees, use separate queues with dedicated workers.
- Priority ranges from 1 (highest) to 2,097,152 (lowest) in BullMQ. Do not use 0.
- Mixing prioritized and non-prioritized jobs in the same queue can cause unexpected ordering.
Code Snippets
Adding jobs with priority
// Critical: send password reset
await queue.add('sendEmail', { type: 'password-reset', userId }, { priority: 1 });
// Low priority: marketing newsletter
await queue.add('sendEmail', { type: 'newsletter', userId }, { priority: 100 });Revisions (0)
No revisions yet.