Multiple consumers and prefetch control

Share one queue's tasks between two consumers and observe how prefetch limits unacknowledged messages.

🎯 Introduction

Earlier examples used delay(5000) to simulate five seconds of order processing so we could observe delivery and acknowledgement. When tasks accumulate, another identical consumer can share the work. First we will verify how two programs receive tasks from one queue, then change prefetch to observe how many unacknowledged messages one consumer can hold.


🧱 How two consumers share tasks

Both order programs read work.order. RabbitMQ delivers each message to one consumer for processing; starting two programs does not give each a copy. The earlier publish/subscribe article used multiple queues to give different business systems their own copies. Here, consumers share the same set of order tasks.

worker.mjs already sets this before subscribing:

await channel.prefetch(1);

The prefetch count limits how many unacknowledged messages a consumer can hold at once. With 1, RabbitMQ stops delivering to that consumer after one message, until its acknowledgement frees a slot.

This example uses manual acknowledgements and creates one consumer per process:

SetupMaximum unacknowledged messages held at once
One consumer with prefetch 11
Two consumers, each with prefetch 11 each, 2 total
One consumer with prefetch 33

This limit does not cap the total messages waiting in the queue. We use RabbitMQ's default per-consumer limits; see the official prefetch documentation.


🛠 Share three tasks between two programs

Reuse worker.mjs, send-work.mjs, and config.mjs from consumer acknowledgements, without rewriting send or receive code. Finish old tasks, then stop all workers.

Temporarily change delay(5000) in worker.mjs to delay(30000), retaining prefetch(1) and noAck: false. Thirty seconds gives you time to start the second program and inspect logs.

With no workers running, submit three tasks:

node send-work.mjs P2001
node send-work.mjs P2002
node send-work.mjs P2003

Then run the same command in two terminals, starting the second program before the first prints “Completed”:

node worker.mjs

Both programs prefix logs with their process ID, identifying which received each task. Observe three stages:

StageExpected observation
Both programs started, neither task completeEach prints one “Started”; the third task remains queued
One program completes and acknowledgesThe consumer with a free slot receives the third task and prints another “Started”
All three tasks finishThree “Completed” lines in total across both terminals

No particular ID must go to a particular program, and strict alternation is not required. Just check that P2001, P2002, and P2003 each finish once during this normal run. If the second program starts too late, finish the round and repeat with fresh IDs.


⚖️ What happens when one consumer prefetches three messages?

Wait for all three tasks to finish and stop both workers. Change prefetch(1) to prefetch(3), keeping the thirty-second wait. Submit three new tasks:

node send-work.mjs P2011
node send-work.mjs P2012
node send-work.mjs P2013

This time start only one worker:

node worker.mjs

You will usually see three “Started” lines in succession, then three “Completed” lines about thirty seconds later. Two things explain this: prefetch 3 allows RabbitMQ to deliver three unacknowledged messages, and asynchronous waits let the other callbacks begin while the first waits.

Prefetch controls how many messages may be received; actual processing also depends on the code. It creates no new JavaScript threads. Increasing it does not automatically run thread-blocking calculations simultaneously.

Higher prefetch is not always better. If every task accesses a database, more concurrent tasks may increase database load. Compare task throughput with downstream load before increasing it.

After this round, stop the worker and restore prefetch to 1 and the wait to delay(5000). File changes take effect on the next start.


❓ Questions and answers

Q1: Does prefetch 0 mean no messages are received?

No. Zero means there is no limit on the unacknowledged message count.

Q2: Do multiple consumers always finish in sending order?

No. Task durations vary, so a later delivery may finish first. Do not depend on completion order across two programs to enforce business ordering.


🧪 Exercise

Stop all workers, set prefetch to 2, and set the wait to thirty seconds. With no old tasks remaining, send three fresh tasks, then start only one worker:

node send-work.mjs P2021
node send-work.mjs P2022
node send-work.mjs P2023
node worker.mjs

Expect two “Started” lines first. The third appears only after at least one task completes and is acknowledged. Explain why the third cannot be received immediately.

After all three finish, stop the worker and restore prefetch to 1 and the wait to five seconds for the next article.


🎉 You can now distinguish consumer count from prefetch count and observe how they affect task distribution!