Queue producer and consumer
Local baseline: Node.js 22.16.0, ECMAScript modules, synthetic data. Run each complete example independently as node main.mjs in a disposable directory. Static ByteMind blocks do not imply live Node execution. SQLite is experimental in this baseline. Third-party integrations below are staged candidates, not installed or validated dependencies.
Process a finite receipt queue
const queue=[{id:'r1',items:2},{id:'r2',items:4}];const completed=[];while(queue.length){const job=queue.shift();completed.push({id:job.id,total:job.items*5});}console.log(JSON.stringify(completed));console.log(queue.length);Expected output:
[{"id":"r1","total":10},{"id":"r2","total":20}]0Two synthetic jobs produce results and the in-memory queue is empty.
How it works
Give each job a stable identity, a small validated payload and an explicit lifecycle: waiting, active, completed or failed. Acknowledge only after the required business effect is durable. A worker can crash after making a change but before acknowledgment, so consumers should tolerate repeated delivery. Limit concurrency to protect downstream systems and stop accepting new work before graceful shutdown. BullMQ uses Redis-backed jobs; RabbitMQ uses broker messages and acknowledgments, while Kafka uses partitioned logs and offsets. These differ in ordering, retention and delivery semantics. The models below have no persistence, leases or distributed workers.
Bound asynchronous worker concurrency
import assert from 'node:assert/strict';const jobs=[1,2,3,4,5];let active=0,peak=0;async function processJob(id){active++;peak=Math.max(peak,active);try{await Promise.resolve();return id*2;}finally{active--;}}const output=[];for(let i=0;i<jobs.length;i+=2)output.push(...await Promise.all(jobs.slice(i,i+2).map(processJob)));assert.equal(active,0);console.log(JSON.stringify(output),peak);Expected output:
[2,4,6,8,10] 2At most two jobs are active in the controlled waves; this is not a continuous production worker pool.
BullMQ finite receipt job (setup excerpt)
NOT RUN — bullmq (candidate; reviewed Redis fixture and QueueEvents completion/cleanup harness missing). This integration requires reviewed pinned dependencies and a disposable fixture. Do not execute against shared services.
import {Queue, Worker} from 'bullmq';// Reviewed disposable Redis only, not a shared or production instance.const connection={host:'127.0.0.1',port:16379};const queue=new Queue('bm-v8-receipts',{connection});const worker=new Worker('bm-v8-receipts',async job=>({total:job.data.items*5}),{connection,concurrency:1});try { await queue.add('receipt',{items:2},{jobId:'r1',attempts:2}); // Wait for completion using reviewed QueueEvents before asserting result.} finally {await worker.close();await queue.close();}Intended result (not observed): No verified output; partial setup excerpt, not a completion test.
Common mistakes
Do not put secrets or large files in payloads. Do not equate adding a job with finishing it. A local Promise queue is not proof of broker delivery guarantees.
Practice and next steps
Design a receipt job containing only a receipt ID and version. Describe how a client checks the job result and how a worker handles a missing receipt without retrying forever.
Further reading: https://docs.bullmq.io/guide/retrying-failing-jobs. Related staged reference: node-queue-producer-and-consumer-lookup (route not verified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Queue producer and consumer — knowledge check
Apply the lesson to a fresh scenario and identify the failure boundary.
What does a successful enqueue mean?
Why limit worker concurrency?
When should a durable business operation be acknowledged?