AMQP 0-9-1 Overview

Publisher Confirms

A mechanism that gives the producer a receipt — the broker acknowledges each message only after the leader node has received and persisted it.

Whenever message loss is not acceptable. Without confirms, a network drop between publish and persist means the message is simply gone.

Using Publisher Confirms

To use Publisher Confirms, enable delivery confirms on the channel.

channel.confirm_delivery()

Each subsequent basic_publish waits for a basic.ack from the broker before returning. 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")

For higher throughput, confirms can be handled asynchronously. Register callbacks for acks and nacks and track each message by sequence number to retry any unconfirmed messages if the connection closes.

def on_delivery_confirmation(method_frame):
    tag = method_frame.method.delivery_tag
    if method_frame.method.NAME == "Basic.Ack":
        print(f"Message {tag} confirmed")
    else:
        print(f"Message {tag} not confirmed — retry")

channel.confirm_delivery()
channel.add_on_ack_callback(on_delivery_confirmation)
channel.add_on_nack_callback(on_delivery_confirmation)

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.


Can’t find what you’re looking for? Let us know
Was this helpful?

Search