Module P-6·22 min read

How BullMQ maps job lifecycle to Sorted Sets, Lists, and Hashes. Worker polling, delayed job scheduling, stalled job detection via heartbeat, the rate limiter internals, and choosing BullMQ vs raw Streams.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

P-6 — BullMQ Internals: The Redis Data Structures Behind the Job Queue

Who this module is for: You use BullMQ (or Bull) for job queues and have run into issues — jobs that get stuck, queues that slow down under load, stalled job detection that is too aggressive or not aggressive enough. This module explains the Redis data structures BullMQ uses for every queue state, so you can reason about its behaviour, tune it correctly, and debug it at the Redis level.


Why Understanding BullMQ Internals Matters

BullMQ is a job queue built on Redis. Most engineers treat it as a black box — they add jobs with queue.add() and process them with a processor function passed straight into new Worker(queueName, processorFn). But when queues misbehave (jobs stay in "active" forever, delayed jobs fire late, rate limits fail), you cannot diagnose or fix the problem without understanding the Redis layer.

Every BullMQ behaviour maps to specific Redis operations. Knowing this lets you:

  • Query queue state directly with redis-cli without going through BullMQ's API
  • Understand why a job is "stuck" and fix it
  • Tune TTL, stall checks, and rate limiter settings appropriately
  • Identify Redis memory usage caused by large queues

The Key Schema

BullMQ uses a namespaced key prefix. For a queue named emails:

text

Job Lifecycle in Redis

Adding a Job (queue.add)

javascript

What happens in Redis:

  1. INCR bull:emails:id → generates job ID, e.g., 42
  2. HSET bull:emails:42 with all job fields:
    • id: "42"
    • name: "send-welcome"
    • data: '{"userId":"1001","email":"j@example.com"}'
    • opts: '{"attempts":1,"delay":0,...}'
    • timestamp: "1717000000000"
    • delay: "0"
    • priority: "0"
  3. RPUSH bull:emails:wait 42 → add job ID to the wait list
  4. XADD bull:emails:events * event added jobId 42 → emit event to the events stream

The job data (step 2) is stored in a Hash for O(1) field access. The queue lists and sorted sets store only the job ID — the actual data is always in the Hash.

Adding a Delayed Job

javascript

Instead of RPUSH bull:emails:wait, BullMQ uses:

ZADD bull:emails:delayed {runAt_timestamp_ms} {jobId}

A scheduler process (the QueueScheduler class — a BullMQ-specific concept used before BullMQ 2.0, since merged directly into the Worker itself) polls the delayed sorted set with:

ZRANGEBYSCORE bull:emails:delayed 0 {now_ms} COUNT 100

When jobs become ready (their score ≤ current timestamp), the scheduler moves them to bull:emails:wait via LPUSH and ZREM.

Adding a Priority Job

javascript
ZADD bull:emails:prioritized {priority_score} {jobId}

Workers preferentially consume from prioritized before wait.

Processing a Job (worker picks up)

The worker calls:

LMOVE bull:emails:wait bull:emails:active RIGHT LEFT

This atomically moves the job ID from the tail of wait to the head of active. If no jobs are waiting, the worker calls:

BLMOVE bull:emails:wait bull:emails:active RIGHT LEFT 5

Blocking for up to 5 seconds. When a job arrives, the BLMOVE completes and the job ID is in active.

The worker then reads the job data:

HGETALL bull:emails:{jobId}

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.