Junior — Middle
What is the main role and purpose of the RabbitMQ message queue system?
sobes.tech AI
Answer from AI
RabbitMQ is a message broker that provides asynchronous message exchange between different system components. Its main role is to offload applications by allowing them to send and receive messages through queues, which enhances the system's scalability, reliability, and resilience.
Purposes of RabbitMQ:
- Buffering messages for processing at a convenient time.
- Separating system components to reduce coupling.
- Ensuring guaranteed message delivery.
- Supporting various messaging models (queues, publish/subscribe).
Example of usage in Node.js:
const amqp = require('amqplib');
async function sendMessage() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const queue = 'task_queue';
const msg = 'Hello RabbitMQ';
await channel.assertQueue(queue, { durable: true });
channel.sendToQueue(queue, Buffer.from(msg), { persistent: true });
console.log(" [x] Sent '%s'", msg);
setTimeout(() => {
connection.close();
process.exit(0);
}, 500);
}
sendMessage();