0
0
RabbitMQdevops~5 mins

Message durability and persistence in RabbitMQ - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you send messages in RabbitMQ, they can be lost if the server crashes. Message durability and persistence help keep messages safe by saving them to disk so they survive restarts.
When you want to make sure important messages are not lost if RabbitMQ restarts or crashes
When your application sends orders or payments that must be processed even after failures
When you run a queue that handles tasks that cannot be repeated or lost
When you want to keep messages safe during server maintenance or upgrades
When you need reliable communication between services that must not lose data
Config File - rabbitmq_queue_declare.json
rabbitmq_queue_declare.json
{
  "queue": "task_queue",
  "durable": true
}

This JSON file shows how to declare a queue named task_queue with durability enabled. The "durable": true setting means the queue will survive RabbitMQ restarts.

Commands
This command creates a queue named 'task_queue' that will survive RabbitMQ server restarts because it is durable.
Terminal
rabbitmqadmin declare queue name=task_queue durable=true
Expected OutputExpected
Queue declared
durable=true - Makes the queue survive server restarts
This command sends a message 'Hello World' to the 'task_queue' with delivery_mode=2, which makes the message persistent and saved to disk.
Terminal
rabbitmqadmin publish routing_key=task_queue payload='Hello World' delivery_mode=2
Expected OutputExpected
Message published
delivery_mode=2 - Marks the message as persistent so it is saved to disk
This command lists queues with their durability and message counts to verify the queue is durable and messages are present.
Terminal
rabbitmqctl list_queues name durable messages_ready messages_unacknowledged
Expected OutputExpected
task_queue true 1 0
Key Concept

If you remember nothing else from this pattern, remember: to keep messages safe after RabbitMQ restarts, both the queue and the messages must be marked durable and persistent.

Common Mistakes
Declaring the queue without durable=true
The queue will be deleted on server restart, losing all messages
Always declare queues with durable=true if you want them to survive restarts
Publishing messages without delivery_mode=2
Messages will be lost if RabbitMQ crashes because they are not saved to disk
Set delivery_mode=2 when publishing to make messages persistent
Assuming durable queues alone guarantee message safety
Durable queues survive restarts but messages must also be persistent to be saved
Use both durable queues and persistent messages together
Summary
Declare queues with durable=true to keep them after RabbitMQ restarts.
Publish messages with delivery_mode=2 to save them to disk.
Check queue durability and message counts with rabbitmqctl to verify persistence.