RabbitMQ & AMQP (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
How do Publisher Confirms work in RabbitMQ, and why are they essential for zero message loss?
senior LevelCode ExampleSystem DesignRabbitMQMicroservices
In Simple Words: By default, publishing is asynchronous—the producer doesn't know if a message actually reached disk or an exchange.
Publisher Confirms (AMQP extension):
- The client puts the channel into confirm mode (
channel.confirmSelect()). - The broker sends an
ackback to the producer once the message is written to disk or confirmed on all quorum replicas. - If an internal broker error occurs, the broker sends a
nack, allowing the producer to retry.
javascript
// Using ConfirmChannel in Node.js
const ch = await conn.createConfirmChannel();
ch.publish('orders.exchange', 'order.paid', Buffer.from(data), { persistent: true }, (err, ok) => {
if (err) {
console.error('Message nacked or failed to persist by RabbitMQ broker!', err);
} else {
console.log('Message successfully persisted to broker disk.');
}
});