Mirrored Queue in RabbitMQ: What It Is and How It Works
mirrored queue in RabbitMQ is a queue that is replicated across multiple nodes in a cluster to ensure high availability. All messages and operations on the queue are copied to mirrors, so if one node fails, another can continue serving the queue without data loss.How It Works
Imagine you have a notebook that you use to write important notes. To make sure you never lose your notes, you keep exact copies of this notebook with your friends. If your notebook gets lost or damaged, you can still get your notes from your friends' copies. This is similar to how a mirrored queue works in RabbitMQ.
In RabbitMQ, a mirrored queue is stored on multiple servers (nodes) in a cluster. One node acts as the main owner of the queue, and the others keep exact copies called mirrors. Every time a message is added or removed, all mirrors update to match the main queue. This way, if the main node crashes, one of the mirrors can take over immediately without losing any messages.
This replication happens automatically and keeps the queue data consistent across nodes, providing fault tolerance and high availability for critical messaging tasks.
Example
This example shows how to declare a mirrored queue in RabbitMQ using the policy feature with the rabbitmqctl command-line tool.
rabbitmqctl set_policy ha-all "^mirrored-queue$" '{"ha-mode":"all"}'
When to Use
Use mirrored queues when you need your messages to be safe even if a server goes down. This is important for systems that cannot afford to lose data, like financial transactions, order processing, or critical notifications.
Mirrored queues help keep your messaging system running smoothly during hardware failures or maintenance by automatically switching to a mirror without losing messages or stopping the service.
However, mirrored queues use more resources because messages are copied multiple times, so use them only for queues that need high availability.
Key Points
- A mirrored queue replicates messages across multiple nodes for reliability.
- It ensures no message loss if a node fails.
- Mirroring is configured using policies in RabbitMQ.
- It increases resource use, so apply it only to critical queues.
- Mirrored queues improve fault tolerance and uptime.