Skip to content
Fullstack Launchpad

Durable Queues and Persistent Messages

Restart RabbitMQ 4.4 and count which queues and messages survive, why quorum queues keep everything, and why durable plus persistent is still not enough.

Lesson 7By Published 10 min readRabbitMQ: The Complete Guide
A heavy hand-drawn safe with an orange combination dial, a handle and a yellow plate that reads kept

In the previous lesson three workers shared Darkroom's resize jobs fairly. Acknowledgements and prefetch protect a job while a worker holds it. They do nothing for the jobs still waiting in the queue when the broker itself stops. In this lesson you fill two queues, restart RabbitMQ, and count what is left. Then you kill the broker in the middle of a publish and see what durable queues and persistent messages do not cover.

The problem: the broker restarts with jobs in the queue

A user uploads 200 photos at night. The resize workers are slow, so most jobs wait in the queue. Then the broker restarts: a version upgrade, a host reboot, a container that the orchestrator moved. When RabbitMQ comes back, which of those jobs are still there?

The answer depends on two settings that you choose in code, one on the queue and one on each message. RabbitMQ 4.3 also changed what you are allowed to choose (4.4 keeps it), so tutorials written for 3.x give you the wrong picture.

The big picture: one switch on the queue, one on the message

A queue is either durable or transient. RabbitMQ keeps the definition of a durable queue on disk and brings the queue back when the node starts. A transient queue does not come back.

Messages carry their own flag. In AMQP 0-9-1 it is the delivery mode property, where 2 means persistent and 1 means transient. amqplib exposes it as the persistent publish option. The API reference does not state a default, but the amqplib source does. In lib/api_args.js at v2.4.2, a comment says undefined will default to 1 (non-persistent). So if you leave the option out, your messages are transient.

The queues docs give the general rule, and for classic queues it holds as written. A durable queue is recovered "including messages in them published as persistent". Messages published as transient "will be discarded during recovery, even if they were stored in durable queues". The storage section adds that "transient messages will still be stored on disk but will be discarded during the next node restart".

Quorum queues, the type the first Node.js lesson used for darkroom.resize, are the exception, described in the quorum queues guide. They are "always durable", and the feature matrix lists their per message persistence as "always".

Run it: fill two queues and restart the broker

Use the broker container from the Docker lesson, named rabbitmq. In the project folder from the earlier lessons (ESM, amqplib 2.4.2), save this as restart-lab.js:

js
// restart-lab.js: what survives a RabbitMQ restart?
// Usage: node restart-lab.js fill       declare queues, publish 6 jobs to each
//        node restart-lab.js transient  try a non-durable, non-exclusive queue
//        node restart-lab.js count      after the restart: count what is left
//        node restart-lab.js clean      delete the lab queues
import amqp from 'amqplib';

const URL = process.env.AMQP_URL ?? 'amqp://127.0.0.1';
const QUEUES = {
  'darkroom.lab.classic': { durable: true, arguments: { 'x-queue-type': 'classic' } },
  'darkroom.lab.quorum': { durable: true, arguments: { 'x-queue-type': 'quorum' } },
};

const connection = await amqp.connect(URL, {
  clientProperties: { connection_name: 'restart-lab' },
});
connection.on('error', (err) => console.log('connection error:', err.message));

async function openChannel() {
  const ch = await connection.createChannel();
  ch.on('error', () => {}); // failed calls also reject their promise, reported below
  return ch;
}

async function fill() {
  const ch = await openChannel();
  for (const [name, options] of Object.entries(QUEUES)) {
    await ch.assertQueue(name, options);
    for (let i = 1; i <= 6; i++) {
      const persistent = i <= 3; // p-1..p-3 persistent, p-4..p-6 transient
      const job = { photoId: `p-${i}`, sizes: [320, 1080] };
      ch.sendToQueue(name, Buffer.from(JSON.stringify(job)), { persistent });
    }
    console.log(`${name}: sent p-1..p-3 persistent, p-4..p-6 transient`);
  }

  // A non-durable exclusive queue: allowed, but it belongs to this connection.
  const q = await ch.assertQueue('darkroom.lab.exclusive', { durable: false, exclusive: true });
  ch.sendToQueue(q.queue, Buffer.from('{"photoId":"p-7"}'), { persistent: true });
  console.log(`${q.queue}: declared, exclusive to this connection`);

  await ch.close();
  await connection.close();
  console.log('closed cleanly');
}

async function transient() {
  const ch = await openChannel();
  try {
    await ch.assertQueue('darkroom.lab.transient', { durable: false });
    console.log('darkroom.lab.transient: declared');
    await connection.close();
  } catch (err) {
    console.log('assertQueue failed:', err.message.split('\n')[0]);
  }
}

async function count() {
  for (const name of [...Object.keys(QUEUES), 'darkroom.lab.exclusive']) {
    const ch = await openChannel();
    try {
      const { messageCount } = await ch.checkQueue(name);
      const left = [];
      for (let msg; (msg = await ch.get(name)); ) {
        left.push(JSON.parse(msg.content.toString()).photoId);
      }
      console.log(`${name}: ${messageCount} left [${left.join(', ')}]`);
      await ch.close(); // unacked messages go back to the queue
    } catch (err) {
      console.log(`${name}: gone (${err.message.split(' with message ')[0]})`);
    }
  }
  await connection.close();
}

async function clean() {
  const ch = await openChannel();
  for (const name of Object.keys(QUEUES)) await ch.deleteQueue(name);
  await ch.close();
  await connection.close();
  console.log('lab queues deleted');
}

const commands = { fill, transient, count, clean };
const cmd = commands[process.argv[2]];
if (!cmd) {
  console.error('usage: node restart-lab.js fill|transient|count|clean');
  process.exit(2);
}
await cmd();

Each queue gets six jobs: p-1 to p-3 persistent, p-4 to p-6 transient. count reads the jobs with get and never acks them, so closing the channel puts them back.

Fill the queues and count before the restart:

sh
node restart-lab.js fill
node restart-lab.js count
text
darkroom.lab.classic: sent p-1..p-3 persistent, p-4..p-6 transient
darkroom.lab.quorum: sent p-1..p-3 persistent, p-4..p-6 transient
darkroom.lab.exclusive: declared, exclusive to this connection
closed cleanly
darkroom.lab.classic: 6 left [p-1, p-2, p-3, p-4, p-5, p-6]
darkroom.lab.quorum: 6 left [p-1, p-2, p-3, p-4, p-5, p-6]
darkroom.lab.exclusive: gone (Operation failed: QueueDeclare; 404 (NOT-FOUND))

The exclusive queue is gone already, before any restart. Now restart the broker. The until loop waits for a Server startup complete line logged after the restart (--since a timestamp taken just before it), because the log still holds the line from the first start. A count sent too early fails during the connection handshake:

sh
started=$(date +%s)
docker restart rabbitmq
until docker logs --since "$started" rabbitmq 2>&1 | grep -q 'Server startup complete'; do sleep 1; done
node restart-lab.js count
text
darkroom.lab.classic: 3 left [p-1, p-2, p-3]
darkroom.lab.quorum: 6 left [p-1, p-2, p-3, p-4, p-5, p-6]
darkroom.lab.exclusive: gone (Operation failed: QueueDeclare; 404 (NOT-FOUND))

The classic queue lost its three transient jobs. The quorum queue kept all six.

What survived, and why

Before the restart both queues hold p-1 to p-6; after docker restart the classic queue holds only the persistent p-1 to p-3, the quorum queue still holds all six

QueueMessageAfter docker restartDocs
Classic, durablepersistentKeptrecovered on boot
Classic, durabletransientQueue kept, messages discardeddiscarded during recovery
Quorum (always durable)persistentKeptpersistence "always"
Quorum (always durable)transientKept (lab run)persisted "regardless of the message delivery mode"
Classic, non-durable, exclusiveeitherGone when its connection closedexclusive queues
Classic, non-durable, non-exclusiveeitherCannot be declared by defaultdeprecated, refused since 4.3.0

Two rows surprise people who learned RabbitMQ from older tutorials.

Quorum queues ignore the transient flag for storage. The quorum queues guide says, in its dead-lettering caveats, that messages published to a quorum queue "are persisted on disk regardless of the message delivery mode". It does not say in so many words that transient messages survive a restart. This run is the evidence: p-4 to p-6 came back. The jobs from the first Node.js lesson were sent without persistent, so they were transient. Going by this run, they would still have survived a restart, because darkroom.resize is a quorum queue.

A transient, non-exclusive queue is refused. transient tries to declare one:

sh
node restart-lab.js transient
text
connection error: Connection closed: 541 (INTERNAL-ERROR) with message "INTERNAL_ERROR - Feature `transient_nonexcl_queues` is deprecated.
By default, this feature is not permitted anymore.
The feature will be removed from a future major RabbitMQ version, regardless of the configuration; actual version to be determined.
To..."
assertQueue failed: Channel ended, no reply will be forthcoming

The docs only say such a queue cannot be declared. What this run shows is how: the broker closed the whole connection with 541 INTERNAL_ERROR, and the broker log calls it a connection exception. Every other channel on that connection dies too. A 406 PRECONDITION_FAILED would close only that channel. The full message in the broker log names the way out, the line deprecated_features.permit.transient_nonexcl_queues = true in rabbitmq.conf. The docs call the feature deprecated, and the config comment there says it "will be removed in a later version". I tried that line on a second, fresh test container: the queue was declared, and after a restart the broker log said 1 transient queues from node 'rabbit@darkroom-rabbit' deleted. For a temporary queue, use an exclusive queue, or a durable queue with a queue TTL, which the docs call "a reasonably close equivalent".

Why durable and persistent is still not enough

docker restart is gentle. In the broker log of this run, RabbitMQ stopped its message stores (Stopping message store) before it exited; docker logs --since 2m rabbitmq right after the restart shows those lines. A crash gives it no such chance. Run node restart-lab.js clean first, then save this as blind-publish.js. It publishes up to 200,000 persistent jobs to a durable classic queue, without confirms:

js
// blind-publish.js: publish persistent jobs to a durable queue, without confirms,
// while someone kills the broker. How many did the publisher think it sent?
import amqp from 'amqplib';

const URL = process.env.AMQP_URL ?? 'amqp://127.0.0.1';
const QUEUE = 'darkroom.lab.classic';

const connection = await amqp.connect(URL, {
  clientProperties: { connection_name: 'blind-publish' },
});
const channel = await connection.createChannel();
await channel.assertQueue(QUEUE, { durable: true, arguments: { 'x-queue-type': 'classic' } });

let sent = 0;
const report = (why) => {
  console.log(`${why}: sendToQueue was called for ${sent} persistent jobs`);
  process.exit(0);
};
connection.on('error', (err) => report(`connection error (${err.message})`));
connection.on('close', () => report('connection closed'));

for (let i = 1; i <= 200000; i++) {
  const job = { photoId: `p-${i}`, sizes: [320, 1080] };
  channel.sendToQueue(QUEUE, Buffer.from(JSON.stringify(job)), { persistent: true });
  sent = i;
  if (i % 1000 === 0) await new Promise((r) => setImmediate(r)); // let the socket drain
}
await channel.close();
await connection.close();

Start it and kill the broker 1.5 seconds later. Start the broker again, wait for the fresh startup line, count, and delete the lab queues:

sh
node blind-publish.js & sleep 1.5; docker kill rabbitmq
started=$(date +%s)
docker start rabbitmq
until docker logs --since "$started" rabbitmq 2>&1 | grep -q 'Server startup complete'; do sleep 1; done
docker exec rabbitmq rabbitmqctl -q list_queues name durable messages
node restart-lab.js clean
text
connection error (Unexpected close): sendToQueue was called for 120000 persistent jobs
name	durable	messages
darkroom.lab.classic	true	28672

docker kill sends SIGKILL by default. The publisher called sendToQueue 120,000 times and saw no error until the connection dropped. After the restart, 28,672 jobs were in the queue. The number changes from run to run; the gap does not go away.

A persistent job's path from top to bottom: client buffer, network, broker memory, disk written in batches, then the confirm; a crash before the disk step loses the job

A job can be lost at several points on that path:

The publisher cannot tell which of its 120,000 jobs made it. Durability and persistence decide what the broker keeps once it has a message. They do not tell the publisher that the broker has it.

Settings for Darkroom: the fix and its trade-offs

Does a durable queue make my messages durable?

Not on a classic queue. Durability belongs to the queue: its definition comes back after a restart. Each message decides for itself with its delivery mode, and a transient message in a durable classic queue is dropped during recovery. Quorum queues are the exception: they write every message to disk whatever its delivery mode, and in this lesson's run they kept transient messages across a restart.

Summary

  • A durable queue comes back after a restart. A classic queue brings back only its persistent messages.
  • amqplib publishes transient messages unless you pass persistent: true.
  • Quorum queues are always durable, and in the lab run they kept all six jobs, transient ones included.
  • RabbitMQ 4.4 refuses non-durable, non-exclusive queues by default and in the lab run closed the whole connection with 541. Exclusive queues vanish with their connection.
  • A crash can still lose persistent messages that were in the client buffer, on the wire, or not yet on disk. Publisher confirms tell you which ones made it.

Sources

Looked up on 2026-11-29 against RabbitMQ 4.4 (Docker image rabbitmq:4.4.1-management), amqplib 2.4.2 and Node.js 24.18.0. Every output above comes from a real run on a test container.