0
0
RabbitMQdevops~5 mins

Publisher confirms in RabbitMQ - Commands & Configuration

Choose your learning style9 modes available
Introduction
When sending messages to RabbitMQ, you want to be sure the messages are safely received by the server. Publisher confirms let your app know when messages are successfully stored, so you can avoid losing data.
When you want to make sure your messages are not lost if RabbitMQ crashes.
When your app sends important data that must be confirmed as received before continuing.
When you want to retry sending messages only if the server did not confirm them.
When you want to monitor message delivery success in real time.
When you want to avoid blocking your app while waiting for server acknowledgments.
Commands
Check that RabbitMQ server is running and ready to accept connections.
Terminal
rabbitmqctl status
Expected OutputExpected
Status of node rabbit@localhost ... [{pid,12345}, {running_applications,[ {rabbit,"RabbitMQ","3.11.10"}, {os_mon,"CPO CXC 138 46","2.4"} ]}, {os,{unix,linux}}, {memory,[{total,12345678}]}, {alarms,[]}]
Run a Python script that connects to RabbitMQ, enables publisher confirms, sends a message, and waits for confirmation.
Terminal
python3 publisher_confirms.py
Expected OutputExpected
Message published with confirmation
Key Concept

If you remember nothing else from this pattern, remember: publisher confirms let your app know when RabbitMQ safely received your messages.

Code Example
RabbitMQ
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

channel.confirm_delivery()

message = 'Hello, RabbitMQ with confirms!'

if channel.basic_publish(exchange='', routing_key='test_queue', body=message):
    print('Message published with confirmation')
else:
    print('Message could not be confirmed')

connection.close()
OutputSuccess
Common Mistakes
Not enabling publisher confirms on the channel before publishing messages.
Without enabling confirms, the server does not send acknowledgments, so your app cannot know if messages were received.
Call the method to enable publisher confirms on the channel before sending messages.
Ignoring the confirmation callback or not waiting for confirms after publishing.
If you don't wait or listen for confirms, you lose the benefit of knowing message delivery status.
Implement a callback or wait synchronously for confirms after publishing.
Summary
Check RabbitMQ server status to ensure it is running.
Enable publisher confirms on the channel before sending messages.
Publish messages and wait for confirmation to ensure delivery.