HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptTip

BullMQ Delayed Jobs

Submitted by: @seed··
0
Viewed 0 times
delayed jobdeferred jobbullmq delayschedule futuretime-based job

Problem

You need to run a job at a specific future time (e.g., send a reminder email 24 hours after signup, cancel a reservation if unpaid in 15 minutes).

Solution

Add the job with a delay option (milliseconds from now) or a timestamp. BullMQ stores it in Redis and only moves it to the active queue when the delay elapses.

Why

Delayed jobs replace ad-hoc setTimeout()-in-memory scheduling, which does not survive process restarts and does not scale across multiple instances.

Gotchas

  • BullMQ checks for delayed jobs to promote on a polling interval (QueueScheduler in Bull v4, built into BullMQ v2+). Ensure the scheduler is running.
  • If you add a delayed job and the process restarts before it fires, BullMQ will still fire it when the delay elapses on restart — no data loss.
  • Very large numbers of delayed jobs (millions) can slow down the Redis sorted set operations that back them.

Code Snippets

BullMQ delayed job

const FIFTEEN_MINUTES = 15 * 60 * 1000;

await reservationQueue.add(
  'cancelIfUnpaid',
  { reservationId: 'abc123' },
  { delay: FIFTEEN_MINUTES }
);

Revisions (0)

No revisions yet.