Sub topic 11
Durability and persistence
This chapter assumes you know what a broker, queue, producer, and acknowledgement are, and the difference between at-most-once and at-least-once delivery. It covers what must be configured so a message survives a broker crash or restart.
What durability means
Durability means that an accepted message is not lost when the broker process stops, the machine reboots, or a node fails. A broker keeps messages in memory for speed. Without explicit settings, a restart wipes them.
In RabbitMQ, three separate things must all be true:
- The queue is durable, so its definition survives a restart.
- The message is persistent, so its body is written to disk.
- The publisher knows the broker has taken responsibility for the message.
If any one is missing, messages can vanish.
Durable queue plus persistent message
A durable queue with non-persistent messages comes back after a restart, but empty. A persistent message sent to a non-durable queue is lost with the queue. You need both.
import pika
conn = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
ch = conn.channel()
ch.queue_declare(queue="tasks", durable=True)
ch.basic_publish(
exchange="",
routing_key="tasks",
body=b"resize image 42",
properties=pika.BasicProperties(delivery_mode=pika.DeliveryMode.Persistent),
)
conn.close()durable=True makes the queue survive. DeliveryMode.Persistent marks the message to be written to disk.
The gap before the disk write
Marking a message persistent does not mean it is on disk the instant basic_publish returns. The broker may buffer the write for a short time. A crash in that window loses the message, and the publisher never learns about it.
Publisher confirms close this gap. The broker sends a confirmation after it has taken responsibility for the message. For a persistent message on a durable queue, that means it has been written to disk. In pika, enable confirms on the channel.
import pika
conn = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
ch = conn.channel()
ch.confirm_delivery()
ch.queue_declare(queue="tasks", durable=True)
try:
ch.basic_publish(
exchange="",
routing_key="tasks",
body=b"resize image 43",
properties=pika.BasicProperties(delivery_mode=pika.DeliveryMode.Persistent),
mandatory=True,
)
print("broker confirmed")
except pika.exceptions.UnroutableError:
print("message was not routed to any queue")
except pika.exceptions.NackError:
print("broker refused the message")
finally:
conn.close()With confirm_delivery() on a BlockingConnection, basic_publish waits for the confirmation and raises an error if the broker rejects it. If you get no confirmation, treat the publish as failed and retry. A retry can produce a duplicate, which is why consumers should be idempotent.
The write path
Costs and limits
Durability trades speed for safety. Each disk write and confirmation adds latency, so throughput drops compared with in-memory messages. Brokers reduce the cost by batching writes and confirms, so you can publish many messages and wait for confirmations together.
Also remember that a single disk is a single point of failure. Replicating the queue across nodes protects against losing the machine. RabbitMQ quorum queues do this, and replication has its own cost in latency.
| Setting | Protects against | Does not protect against |
|---|---|---|
| Durable queue | Queue definition lost on restart | Message bodies lost |
| Persistent message | Message lost on restart | Crash before the disk write completes |
| Publisher confirms | Unnoticed publish failures | Loss of the disk itself |
| Replicated queue | Loss of one node | Loss of all replicas |
Exercise
A team declares a queue with durable=True and publishes messages without setting delivery_mode. The broker restarts. What do they find, and what two changes make messages survive and let the producer know they were safely stored?
Show answer
The queue still exists but is empty, because the messages were not persistent and were held only in memory. They should publish with delivery_mode set to Persistent and enable publisher confirms, so the producer learns when the broker has accepted responsibility for each message.