Skip to main content
InterviewHubPRO
Section Overview
Q12

Question 12 of 50

Part 1: Basic & Foundational Concepts (Junior / Entry-Level)

24%
RabbitMQ & AMQP (50 Qs)•Part 1: Basic & Foundational Concepts (Junior / Entry-Level)

How do you implement a Work Queue (Task Queue) with Round-Robin dispatch?

junior LevelCode ExampleData Structures

In Simple Words: Multiple worker processes listen on the same shared queue. RabbitMQ distributes incoming tasks among them:

javascript
// Worker code in Node.js
import amqp from 'amqplib';

const conn = await amqp.connect('amqp://localhost');
const ch = await conn.createChannel();
await ch.assertQueue('task_queue', { durable: true });
ch.prefetch(1); // Fair dispatch

ch.consume('task_queue', async (msg) => {
  if (msg !== null) {
    const task = JSON.parse(msg.content.toString());
    await processTask(task);
    ch.ack(msg); // Acknowledge completion
  }
});