Publisher confirms and returned messages
🎯 Introduction
The payment system publishes a message for the order program to process later. During publication, the network might disconnect or a queue's receiving rules might be wrong. How can the sender tell whether the message reached a queue, beyond its own “sent” log? This article introduces publisher confirms and returned messages, then tests both successful routing and the absence of a receiving queue.
🧱 The sender needs to check two results
A publisher confirm is RabbitMQ's response to a producer's publication. Whether the consumer completed order processing is covered by the previous article's consumer acknowledgements; neither replaces the other.
A returned message informs the sender that a message could not be routed to a queue. Here, mandatory: true requests a return when no queue matches. Without it, such messages are discarded by default.
| Signal | Direction | Meaning |
|---|---|---|
| Publisher confirm | RabbitMQ → producer | The server finished handling the publication |
| Returned message | RabbitMQ → producer | This mandatory message had no routable queue |
| Consumer acknowledgement | Consumer → RabbitMQ | The consumer reports completion of the delivery |
A publisher confirm and a return can both occur. Determining that no queue matches still counts as handling the publication, so receiving a confirm does not by itself mean the message entered the business queue.
🛠 Prepare an exchange with only a success binding
Reuse the directory, dependencies, and config.mjs from installation and connection. Save all files and run commands there.
import amqp from 'amqplib';
import { rabbitmqUrl } from './config.mjs';
const connection = await amqp.connect(rabbitmqUrl);
const channel = await connection.createChannel();
await channel.assertExchange('payment.events', 'direct', { durable: false });
await channel.assertQueue('order.payment-success', { durable: false });
await channel.bindQueue('order.payment-success', 'payment.events', 'success');
console.log('Publisher confirm experiment ready');
await channel.close();
await connection.close();
The direct exchange payment.events represents payment events. The order.payment-success queue receives payment success messages for the order system with binding key success. Resources are non-durable here: we observe publication outcomes before discussing restart survival in the next article.
node setup-confirms.mjs
No consumer needs to be online for this experiment. Once a message reaches the queue, it can wait for consumption. Publisher confirms do not wait for business processing.
📤 Wait for confirms and check returns
import amqp from 'amqplib';
import { rabbitmqUrl } from './config.mjs';
const routingKey = process.argv[2] ?? 'success';
const connection = await amqp.connect(rabbitmqUrl);
try {
const channel = await connection.createConfirmChannel();
let returned = false;
channel.on('return', (message) => {
returned = true;
console.log('Message returned', message.fields.routingKey, message.fields.replyText);
});
channel.publish('payment.events', routingKey, Buffer.from('P1001'), {
mandatory: true,
});
await channel.waitForConfirms();
console.log('Publisher confirm received');
if (returned) {
console.log('Publication result: no matching queue');
process.exitCode = 1;
} else {
console.log('Publication result: routed to a queue');
}
} finally {
await connection.close();
}
createConfirmChannel() creates a channel supporting publisher confirms. waitForConfirms() waits for previously submitted messages to be confirmed, rejecting on failures such as a server negative acknowledgement. finally closes the connection at the end.
Register the return listener before publishing. For the unroutable mandatory message here, the server sends the return before the confirm, so returned can be checked after waiting. Each run publishes one message, making a boolean sufficient; batches require correlating returns with individual messages.
The true or false returned by publish() indicates whether the client write buffer allows more writes, not server confirmation. False does not mean the message was rejected and is not a reason to resend it immediately. See the amqplib channel API.
🔎 Verify two publication outcomes
First publish using the existing binding:
node publish-check.mjs success
Expected output:
Publisher confirm received
Publication result: routed to a queue
Then use a routing key without a binding:
node publish-check.mjs unknown
Assuming no extra bindings exist, expect:
Message returned unknown NO_ROUTE
Publisher confirm received
Publication result: no matching queue
The second run exits with a nonzero status because the intended enqueueing did not occur. This demonstrates an existing exchange with no matching queue. Publishing to a nonexistent exchange instead causes a channel error.
The first message remains in order.payment-success. Repeating the experiment increases the waiting message count; there is no need to empty the queue to verify these publication signals.
❓ Questions and answers
Q1: Does a missing confirm mean the message was not accepted?
No. The message may have been accepted while the response failed to arrive. Reuse the business ID when resending and have consumers prevent duplicate operations.
Q2: Does mandatory confirm receipt by every intended queue?
No. It detects the absence of any receiving queue; it does not validate your full list of intended subscriptions.
Q3: Can publisher confirms help if the database write succeeded but the program exited before publishing?
No. They cover submitted publications, not consistency between database operations and message publication. See the official RabbitMQ explanation for their scope.
🧪 Exercise
Publish once with Success, then once with success. Expect the first to be returned and the second to be queued, with publisher confirms in both cases. Explain why checking only confirm logs is insufficient to determine routing.
🎉 You can now check publisher confirmation and unroutable message returns separately!
Message processing and consumer acknowledgements
Interrupt a consumer to observe redelivery of unacknowledged messages and understand why business operations still need deduplication.
Queue durability and message persistence
Configure queue durability and message persistence so unprocessed payment messages can be read after RabbitMQ restarts.
