0
0
RabbitMQdevops~5 mins

Mirrored queues for redundancy in RabbitMQ - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes messages in a queue can be lost if the server crashes. Mirrored queues copy messages to multiple servers to keep them safe. This way, if one server fails, another has the same messages ready.
When you want to make sure messages are not lost if one RabbitMQ server goes down
When running RabbitMQ in a cluster and you want queues to be available on multiple nodes
When your application depends on reliable message delivery even during server failures
When you want to balance load and have failover for message processing
When you need high availability for critical messaging systems
Config File - rabbitmq.conf
rabbitmq.conf
cluster_formation.peer_discovery_backend = rabbit_peer_discovery_classic_config

# Define policies for mirrored queues
# This policy mirrors all queues to all nodes in the cluster

policies = [{"ha-all", ".*", {"ha-mode", "all"}, {"priority", 0}}]

This configuration file sets up RabbitMQ to discover cluster nodes using classic config. The policies section defines a policy named ha-all that matches all queues (using .*) and sets ha-mode to all, which means every queue is mirrored to all nodes in the cluster. The priority sets the policy application order.

Commands
This command creates a policy named 'ha-all' that matches all queues and sets them to be mirrored on all nodes. It ensures redundancy for every queue.
Terminal
rabbitmqctl set_policy ha-all ".*" '{"ha-mode":"all"}'
Expected OutputExpected
Setting policy "ha-all" for pattern ".*" to "{"ha-mode":"all"}" ...
This command lists all policies currently set in RabbitMQ so you can verify the 'ha-all' policy is active.
Terminal
rabbitmqctl list_policies
Expected OutputExpected
Name Pattern Definition Priority ha-all .* {"ha-mode":"all"} 0
This command shows all queues and the policies applied to them, confirming that the mirrored queue policy is in effect.
Terminal
rabbitmqctl list_queues name policy
Expected OutputExpected
Name Policy my-queue ha-all another-queue ha-all
Key Concept

If you remember nothing else from this pattern, remember: Mirrored queues copy messages to multiple servers to keep them safe and available during failures.

Common Mistakes
Setting ha-mode to 'exactly' without specifying the number of nodes
RabbitMQ requires a number with 'exactly' mode, otherwise the policy is invalid and queues won't mirror.
Use 'ha-mode' set to 'all' for all nodes or 'exactly' with a number like {"ha-mode":"exactly", "ha-params":2}.
Not applying the policy before creating queues
Queues created before the policy is set will not be mirrored automatically.
Set the policy first, then create queues to ensure they are mirrored.
Using incorrect JSON syntax in the policy command
RabbitMQ rejects policies with invalid JSON, so the command fails silently or errors out.
Use single quotes around the JSON and double quotes inside, like '{"ha-mode":"all"}'.
Summary
Set a policy with rabbitmqctl to mirror queues across all nodes for redundancy.
Verify the policy is active using rabbitmqctl list_policies.
Check queues have the policy applied with rabbitmqctl list_queues.