Queue durability and message persistence
🎯 Introduction
A payment message has reached its queue, but RabbitMQ needs a maintenance restart before the order program processes it. Can the program still receive it afterward? This article explains the separate settings for queues and messages, then verifies them by sending, restarting the server, and finally receiving.
🧱 Retaining a queue does not retain every message
Persistence saves content to storage such as disk so it can be recovered after a program restarts. Distinguish the queue itself from the messages waiting inside it.
| Object | Setting | What survives a restart |
|---|---|---|
| Queue | durable: true in assertQueue() | Queue name and configuration: the queue definition |
| Message | persistent: true in publish() | The unconsumed message's content and properties |
| Exchange | durable: true in assertExchange() | Exchange definition, including name and type, for continued publishing |
This example requires persistent messages in a durable queue. Queue durability does not automatically set persistent on each message, and message persistence cannot restore a non-durable queue after a restart.
Exchange durability preserves the routing entry point rather than message bodies. Since both the exchange and queue are durable here, their binding is also restored. See the RabbitMQ queue documentation.
🛠 Create the payment exchange and order queue
Reuse the directory, dependencies, and connection configuration from installation and connection.
Use payment.completed for completed payments and order.fulfillment for order processing. New names avoid configuration conflicts with earlier non-durable resources.
import amqp from 'amqplib';
import { rabbitmqUrl } from './config.mjs';
const connection = await amqp.connect(rabbitmqUrl);
const channel = await connection.createChannel();
await channel.assertExchange('payment.completed', 'direct', { durable: true });
await channel.assertQueue('order.fulfillment', { durable: true });
await channel.bindQueue('order.fulfillment', 'payment.completed', 'success');
console.log('Payment exchange and order processing queue created');
await channel.close();
await connection.close();
node setup-durable.mjs
📤 Wait for storage after publishing
Setting persistent requests storage; you must also wait for the server to finish. This example adds persistent: true to the previous article's confirm and return checks. For persistent messages routed to durable queues, publisher confirms wait for the applicable persistence conditions. See the official acknowledgement documentation.
import amqp from 'amqplib';
import { rabbitmqUrl } from './config.mjs';
const paymentId = process.argv[2] ?? 'P7001';
const connection = await amqp.connect(rabbitmqUrl);
try {
const channel = await connection.createConfirmChannel();
let returned = false;
channel.on('return', (message) => {
returned = true;
console.error('Message returned', message.fields.replyText);
});
channel.publish('payment.completed', 'success', Buffer.from(paymentId), {
persistent: true,
mandatory: true,
});
await channel.waitForConfirms();
if (returned) throw new Error('No queue can receive this message');
console.log('RabbitMQ confirmed acceptance of', paymentId);
} finally {
await connection.close();
}
Stop consumers subscribed to order.fulfillment so the message remains queued. Supply the payment ID for this run, using a new ID for repeated experiments to distinguish older messages:
node publish-confirmed.mjs P7001
Expected output:
RabbitMQ confirmed acceptance of P7001
Restart only after receiving the confirm and letting the program exit normally. If publication fails, investigate first.
🔄 Restart RabbitMQ, then read the message
Keep the existing storage
Use the container and rabbitmq-course-data volume from environment setup. Restart without deleting or replacing storage. The volume provides a place for files; it does not replace durable and persistent settings.
docker restart rabbitmq-course
docker exec rabbitmq-course rabbitmq-diagnostics -q ping
If the node has not recovered, retry the check shortly. After Ping succeeded, verify the client connection:
node check.mjs
After “Connected successfully”, receive directly. Do not rerun setup-durable.mjs or publish another copy. This ensures you are testing recovery of the queue and message from before the restart.
Subscribe only to the recovered queue
import amqp from 'amqplib';
import { rabbitmqUrl } from './config.mjs';
const connection = await amqp.connect(rabbitmqUrl);
const channel = await connection.createChannel();
await channel.consume('order.fulfillment', (message) => {
if (message === null) return;
console.log('Received', message.content.toString());
channel.ack(message);
}, { noAck: false });
console.log('Waiting for order processing messages');
The receiver only subscribes to the existing queue, prints the ID, and acknowledges. It does not update order data.
node receive-saved.mjs
Apart from the startup message, expect:
Received P7001
Receiving this run's ID shows that the message survived this restart. Keep the consumer online so the server can process the acknowledgement. Messages removed after acknowledgement do not reappear at the next restart; persistence is not a historical message archive.
Restarting also disconnects other examples, and earlier non-durable resources may disappear. Recreate their resources and restart consumers when returning to those chapters. The course scripts do not implement automatic reconnection.
❓ Questions and answers
Q1: Does passing a normal restart test guarantee no message loss under any failure?
No. This only tests a normal restart with existing storage retained, not disk corruption or deleting the volume.
Q2: If persistent is false, will the message always disappear after a restart?
A lack of persistence guarantees does not mean every experiment must lose the message. Configure according to the queue type and documented guarantees rather than judging reliability from one observation.
🧪 Exercise
Let the current message finish processing, then stop the receiver. Predict the result and publish two distinct IDs:
node publish-confirmed.mjs P7002
node publish-confirmed.mjs P7003
After both publications are confirmed, restart the server, perform health and connection checks, and run the receiver directly. Expect P7002 and P7003.
Explain the separate roles of queue durability, message persistence, and waiting for publisher confirms.
🎉 You can now configure queue durability and message persistence separately and verify them after a restart!
