0
0
RabbitmqHow-ToIntermediate · 4 min read

How to Configure High Availability in RabbitMQ: Step-by-Step Guide

To configure high availability in RabbitMQ, set up a cluster of nodes and use mirrored queues (classic queues with replication) or quorum queues for data replication across nodes. This ensures messages are replicated and available even if one node fails.
📐

Syntax

High availability in RabbitMQ is mainly configured by defining policies that control queue mirroring or quorum queue behavior across cluster nodes.

Key parts:

  • rabbitmqctl set_policy <name> <pattern> <definition> --apply-to queues: Command to set HA policies.
  • ha-mode: Defines mirroring mode, e.g., all to mirror to all nodes.
  • ha-params: Specifies nodes to mirror to (used with exactly mode).
  • ha-sync-mode: Controls synchronization behavior, e.g., automatic.
  • quorum queues: Use x-queue-type set to quorum for built-in replication.
bash
rabbitmqctl set_policy ha-all ".*" '{"ha-mode":"all","ha-sync-mode":"automatic"}' --apply-to queues
Output
Setting policy "ha-all" for pattern ".*" to queues Policy set.
💻

Example

This example shows how to create a RabbitMQ cluster with three nodes and configure a policy to mirror all queues across all nodes for high availability.

bash
# On each node, start RabbitMQ with a unique node name
rabbitmq-server -detached --node rabbit@node1
rabbitmq-server -detached --node rabbit@node2
rabbitmq-server -detached --node rabbit@node3

# On node2 and node3, join the cluster with node1
rabbitmqctl stop_app
rabbitmqctl join_cluster rabbit@node1
rabbitmqctl start_app

# On node1, set policy to mirror all queues
rabbitmqctl set_policy ha-all ".*" '{"ha-mode":"all","ha-sync-mode":"automatic"}' --apply-to queues

# Create a test queue (on any node)
rabbitmqadmin declare queue name=test_queue durable=true

# Publish a message
rabbitmqadmin publish routing_key=test_queue payload="Hello HA"
Output
Setting policy "ha-all" for pattern ".*" to queues Policy set. Queue declared. Message published.
⚠️

Common Pitfalls

Common mistakes when configuring RabbitMQ high availability include:

  • Not forming a proper cluster before applying HA policies, so queues are not mirrored.
  • Using ha-mode incorrectly, such as setting exactly without specifying ha-params.
  • Ignoring synchronization mode, which can cause queues to be out of sync after node failures.
  • Using classic mirrored queues instead of quorum queues for new deployments, which are more reliable and easier to manage.

Always verify cluster status with rabbitmqctl cluster_status and queue mirroring with rabbitmqctl list_queues name slave_pids synchronised_slave_pids.

bash
## Wrong way: setting ha-mode exactly without ha-params
rabbitmqctl set_policy ha-exact ".*" '{"ha-mode":"exactly"}' --apply-to queues

## Right way: specify ha-params with nodes
rabbitmqctl set_policy ha-exact ".*" '{"ha-mode":"exactly","ha-params":2,"ha-sync-mode":"automatic"}' --apply-to queues
Output
Error: ha-params must be specified when ha-mode is exactly Setting policy "ha-exact" for pattern ".*" to queues Policy set.
📊

Quick Reference

CommandDescription
rabbitmqctl set_policy ha-all ".*" '{"ha-mode":"all","ha-sync-mode":"automatic"}' --apply-to queuesMirror all queues to all nodes with automatic sync
rabbitmqctl cluster_statusCheck cluster node status
rabbitmqctl list_queues name slave_pids synchronised_slave_pidsCheck queue mirroring and sync status
rabbitmqadmin declare queue name=myqueue durable=trueCreate a durable queue
rabbitmqadmin publish routing_key=myqueue payload="message"Publish message to queue

Key Takeaways

Set up a RabbitMQ cluster before configuring high availability policies.
Use policies with ha-mode 'all' or quorum queues for reliable queue replication.
Always verify cluster and queue synchronization status after configuration.
Prefer quorum queues for new deployments as they offer better fault tolerance.
Specify ha-params when using ha-mode 'exactly' to avoid configuration errors.