Bird
Raised Fist0
RabbitMQdevops~5 mins

Message durability and persistence in RabbitMQ - Commands & Configuration

Choose your learning style10 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.

Practice

(1/5)
1. What does setting a queue as durable in RabbitMQ ensure?
easy
A. The queue will only accept persistent messages.
B. Messages in the queue are automatically deleted after consumption.
C. The queue will survive a RabbitMQ server restart.
D. The queue will reject non-persistent messages.

Solution

  1. Step 1: Understand queue durability

    Setting a queue as durable means it will not be deleted when RabbitMQ restarts.
  2. Step 2: Differentiate from message persistence

    Durability affects the queue itself, not the messages inside it.
  3. Final Answer:

    The queue will survive a RabbitMQ server restart. -> Option C
  4. Quick Check:

    Durable queue = Survives restart [OK]
Hint: Durable queue means it stays after server restarts [OK]
Common Mistakes:
  • Confusing durable queue with persistent messages
  • Thinking durable queues delete messages automatically
  • Assuming durability rejects non-persistent messages
2. Which of the following is the correct way to declare a durable queue in RabbitMQ using the command line?
easy
A. rabbitmqadmin declare queue name=myqueue durable=true
B. rabbitmqadmin declare queue name=myqueue durable=1
C. rabbitmqadmin declare queue name=myqueue persistent=true
D. rabbitmqadmin declare queue name=myqueue durable

Solution

  1. Step 1: Check RabbitMQ CLI syntax for durable queues

    The correct syntax uses durable=true to declare a durable queue.
  2. Step 2: Eliminate incorrect options

    rabbitmqadmin declare queue name=myqueue durable=1 uses 1 instead of true, which is invalid. rabbitmqadmin declare queue name=myqueue persistent=true uses persistent which applies to messages, not queues. rabbitmqadmin declare queue name=myqueue durable is incomplete syntax.
  3. Final Answer:

    rabbitmqadmin declare queue name=myqueue durable=true -> Option A
  4. Quick Check:

    durable=true is correct syntax [OK]
Hint: Use durable=true to declare durable queues [OK]
Common Mistakes:
  • Using persistent instead of durable for queues
  • Using numeric values instead of true/false
  • Omitting the durable parameter
3. Given the following Python code snippet using pika library, what will happen if the message is sent with delivery_mode=2 but the queue is not durable?
channel.queue_declare(queue='task_queue', durable=False)
channel.basic_publish(
    exchange='',
    routing_key='task_queue',
    body='Hello World!',
    properties=pika.BasicProperties(delivery_mode=2))
medium
A. The message will be rejected by the queue due to mismatch in durability settings.
B. The message will be persistent but lost if RabbitMQ restarts because the queue is not durable.
C. The message will be persistent and survive RabbitMQ restarts regardless of queue durability.
D. The message will be lost immediately because delivery_mode=2 is invalid.

Solution

  1. Step 1: Understand message persistence with delivery_mode=2

    Setting delivery_mode=2 marks the message as persistent, meaning RabbitMQ will save it to disk.
  2. Step 2: Understand queue durability effect

    If the queue is not durable, it will be deleted on restart, so persistent messages in it will be lost.
  3. Final Answer:

    The message will be persistent but lost if RabbitMQ restarts because the queue is not durable. -> Option B
  4. Quick Check:

    Persistent message + non-durable queue = message lost on restart [OK]
Hint: Persistent messages need durable queues to survive restarts [OK]
Common Mistakes:
  • Assuming persistent messages survive without durable queues
  • Thinking delivery_mode=2 is invalid
  • Believing messages get rejected due to durability mismatch
4. You declared a queue as durable and sent messages with delivery_mode=2. After restarting RabbitMQ, messages are missing. What is the most likely cause?
medium
A. RabbitMQ does not support message persistence.
B. Messages were sent without the delivery_mode property.
C. The queue was deleted manually before restart.
D. The queue was declared as durable after it was first created as non-durable.

Solution

  1. Step 1: Check queue durability declaration timing

    If a queue is first created as non-durable, changing it to durable later has no effect unless the queue is deleted and recreated.
  2. Step 2: Understand message persistence requirements

    Messages with delivery_mode=2 are persistent, but if the queue is non-durable, messages are lost on restart.
  3. Final Answer:

    The queue was declared as durable after it was first created as non-durable. -> Option D
  4. Quick Check:

    Queue durability must be set at creation [OK]
Hint: Queue durability must be set when first created [OK]
Common Mistakes:
  • Assuming changing durability on existing queue works
  • Ignoring message delivery_mode settings
  • Believing RabbitMQ lacks persistence support
5. You want to ensure that messages are never lost even if RabbitMQ crashes or restarts. Which combination of settings should you use when declaring the queue and publishing messages?
hard
A. Declare the queue as durable and publish messages with delivery_mode=2.
B. Declare the queue as exclusive and publish messages with delivery_mode=1.
C. Declare the queue as auto-delete and publish messages with delivery_mode=2.
D. Declare the queue as durable and publish messages without specifying delivery_mode.

Solution

  1. Step 1: Understand durable queue role

    A durable queue survives RabbitMQ restarts, keeping the queue itself intact.
  2. Step 2: Understand persistent message role

    Messages with delivery_mode=2 are saved to disk, so they survive server crashes.
  3. Step 3: Combine both for full durability

    Only durable queues with persistent messages guarantee no message loss on restart.
  4. Final Answer:

    Declare the queue as durable and publish messages with delivery_mode=2. -> Option A
  5. Quick Check:

    Durable queue + persistent messages = no message loss [OK]
Hint: Use durable queues and delivery_mode=2 messages together [OK]
Common Mistakes:
  • Using exclusive or auto-delete queues for persistence
  • Not setting delivery_mode=2 for messages
  • Assuming durable queue alone is enough
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard