What is a work queue (task queue) pattern in RabbitMQ?
Learn the RabbitMQ work queue pattern: distribute background tasks across competing workers with fair dispatch, manual acks, and durability to scale reliably.
Expected Interview Answer
A work queue (task queue) pattern uses a single queue to distribute time-consuming tasks among multiple worker consumers, so jobs are processed in the background and shared across workers instead of done synchronously.
A producer publishes task messages to a queue, and several worker processes consume from it competing for messages, so RabbitMQ load-balances jobs across them. Combined with manual acknowledgements, a task is only removed once a worker confirms it finished, so a crashed worker's message is redelivered to another. Setting prefetch to 1 with durable queues and persistent messages gives fair dispatch and survives broker restarts, making the pattern reliable for scaling out heavy background work like sending emails or resizing images.
- Distributes heavy work across many workers
- Scales horizontally by adding consumers
- Decouples producers from slow processing
- Survives worker crashes via redelivery
- Smooths spikes by buffering tasks in the queue
AI Mentor Explanation
A work queue is like a bucket of balls at a net feeding several bowling machines: each machine grabs the next ball and delivers it, so the workload spreads across all of them. If one machine jams mid-delivery, the ball isn't lost — it goes back in the bucket for another machine, keeping the whole net session running without any ball dropped.
Step-by-Step Explanation
Step 1
Declare a durable queue
Create a queue that survives broker restarts so buffered tasks are not lost.
Step 2
Publish persistent tasks
Producers send task messages marked persistent so they endure until processed.
Step 3
Start multiple workers
Run several consumers on the same queue; RabbitMQ dispatches tasks among them.
Step 4
Set prefetch to 1
Give one unacked task per worker for fair dispatch so fast workers get more work.
Step 5
Ack after processing
Acknowledge only after a task finishes, so a crashed worker's task is redelivered.
What Interviewer Expects
- Definition of the work queue as competing consumers
- Role of manual acks for reliable processing
- How prefetch enables fair dispatch
- Durability and persistence for crash safety
- A real use case like emails or image resizing
Common Mistakes
- Using auto-ack and losing tasks on a crash
- Forgetting to mark queues durable or messages persistent
- Assuming tasks are duplicated to every worker
- Never setting prefetch, causing unfair distribution
- Confusing a work queue with a publish/subscribe fanout
Best Answer (HR Friendly)
“A work queue lets you hand off slow jobs to a shared line that several workers pull from, so tasks run in the background and across many machines. If a worker crashes, its job goes back for another worker, so nothing is lost and you can scale by adding more workers.”
Code Example
// Producer: publish a durable task
await channel.assertQueue('tasks', { durable: true })
channel.sendToQueue('tasks', Buffer.from(job), { persistent: true })
// Worker: compete for tasks with fair dispatch
await channel.assertQueue('tasks', { durable: true })
await channel.prefetch(1)
await channel.consume('tasks', async (msg) => {
await process(msg.content)
channel.ack(msg) // redelivered elsewhere if the worker dies first
}, { noAck: false })Follow-up Questions
- How does prefetch enable fair dispatch in a work queue?
- Why are manual acknowledgements important for task queues?
- How do durable queues and persistent messages improve reliability?
- How does a work queue differ from publish/subscribe?
- How would you handle a task that repeatedly fails?
MCQ Practice
1. In a work queue, how are tasks distributed?
Workers compete on one queue, so each task is delivered to just one available consumer.
2. What makes a work queue survive worker crashes?
With manual acks, an unacknowledged task from a crashed worker is redelivered to another consumer.
3. Which settings make tasks survive a broker restart?
A durable queue plus persistent messages ensures buffered tasks are not lost when the broker restarts.
Flash Cards
What is a work queue pattern? — One queue distributing time-consuming tasks among multiple competing worker consumers.
How are tasks shared? — Workers compete on the same queue; each task goes to one available worker.
How does it survive worker crashes? — Manual acks mean an unacked task is redelivered to another worker.
Which settings give crash safety across restarts? — Durable queues plus persistent messages, with prefetch 1 for fair dispatch.