Publisher Confirms
It's a mechanism that lets the producer know that the broker has received and persisted with a message. Without it, there is no way to know if a message was lost in transit.
Publisher confirms provide a reliability layer to prevent message loss on the producer side. This mechanism ensures that messages are successfully handed over even if a broker restart or connection failure occurs during the publishing process.
Using Publisher Confirms
Publisher confirms are not enabled by default. It can be enabled by turning on delivery confirms on the channel.
channel.confirm_delivery()Each subsequent basic_publish will block until the broker acknowledges the message with a basic.ack. If the broker cannot handle the message, it replies with a basic.nack. The delivery-tag field contains the sequence number of the confirmed message.
connection = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
channel = connection.channel()
# Enable publisher confirms on this channel
channel.confirm_delivery()
try:
channel.basic_publish(
exchange="",
routing_key="tasks",
body=b"do some work",
properties=pika.BasicProperties(delivery_mode=2), # persistent
)
print("Message confirmed by broker — safe on disk")
except pika.exceptions.UnroutableError:
print("Message returned — no queue could receive it")To increase throughput, handle confirms asynchronously: continue publishing while tracking sequence numbers, then resolve any pending messages if the connection closes.
Note: Confirm mode and transaction mode cannot be used on the same channel.
Ready to take the next steps?
Managed LavinMQ instance via CloudAMQP
LavinMQ has been built with performance and ease of use in mind - we've benchmarked a throughput of about 1,000,000 messages/sec . You can try LavinMQ without any installation hassle by creating a free instance on CloudAMQP. Signing up is a breeze.
Get started with CloudAMQP ->Help and feedback
We welcome your feedback and are eager to address any questions you may have about this piece or using LavinMQ. Join our Slack channel to connect with us directly. You can also find LavinMQ on GitHub.