Home/Learn/RabbitMQ/RabbitMQ Clustering

RabbitMQ Clustering

Advanced
Operations

Cluster nodes share metadata (exchanges, bindings, users) but not queue contents by default; quorum queues replicate content to a majority of nodes for durability.

Overview

A RabbitMQ cluster is a group of nodes sharing exchange definitions, bindings, user accounts, and vhosts — but NOT classic queue contents by default. A message published to a classic queue lives only on the node that owns that queue. If that node fails, the queue and its messages are unavailable until the node recovers. Quorum queues (RabbitMQ 3.8+) use the Raft consensus algorithm to replicate queue contents to a majority of nodes, providing high availability without the deprecated classic mirrored queues. Clients should connect through a load balancer (HAProxy, AWS ALB) or use the AMQP URI list; consumer reconnection logic must handle node failures gracefully. A 3-node cluster tolerates 1 node failure; a 5-node cluster tolerates 2.

Forming a cluster with rabbitmqctl

Join nodes to an existing cluster using rabbitmqctl. All nodes must share the same Erlang cookie (authentication secret).

Shell — manual cluster formation + K8s operator
# Step 1: ensure the same Erlang cookie on all nodes
# /var/lib/rabbitmq/.erlang.cookie  (must be identical on all nodes)

# On node1 (seed node):
rabbitmq-server -detached

# On node2: join node1
rabbitmq-server -detached
rabbitmqctl stop_app
rabbitmqctl reset                     # wipe any existing data
rabbitmqctl join_cluster rabbit@node1 # join with full replication
rabbitmqctl start_app

# On node3: join node1
rabbitmq-server -detached
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster rabbit@node1
rabbitmqctl start_app

# Verify cluster membership
rabbitmqctl cluster_status
# Disk nodes: rabbit@node1, rabbit@node2, rabbit@node3

# RAM nodes: metadata in memory only — never use for quorum queues
# rabbitmqctl join_cluster rabbit@node1 --ram  ← not recommended for HA

# Kubernetes: use the RabbitMQ Cluster Operator (official)
# kubectl apply -f "https://github.com/rabbitmq/cluster-operator/releases/latest/download/cluster-operator.yml"

Quorum queues — Raft replication

Quorum queues replicate to a majority of cluster members. They tolerate (N-1)/2 node failures and do not require all nodes to agree — just a quorum.

Java + Shell — quorum queue setup and fault tolerance
# Declare quorum queue — requires at least 3 nodes
@Bean
public Queue paymentsQueue() {
    return QueueBuilder.durable("payments.quorum")
        .quorum()
        .withArgument("x-quorum-initial-group-size", 3)  // replicate to 3 nodes
        .withArgument("x-delivery-limit", 5)              // max redeliveries
        .build();
}

# Fault tolerance:
# 3-node cluster: tolerates 1 failure (quorum = 2/3)
# 5-node cluster: tolerates 2 failures (quorum = 3/5)
# 2-node cluster: NOT fault-tolerant — needs both nodes up (quorum = 2/2)

# Check queue leader and members
rabbitmq-queues quorum_status payments.quorum
# Shows: leader node, followers, online/offline members

# Force leader election (if leader node is slow)
rabbitmq-queues reclaim_quorum_membership payments.quorum

# application.properties
spring.rabbitmq.addresses=amqp://node1:5672,amqp://node2:5672,amqp://node3:5672
# Spring AMQP connects to first available node; reconnects on failure

Client failover and HAProxy load balancing

Clients must handle broker failure gracefully with reconnection logic. HAProxy in front of the cluster provides a stable endpoint.

HAProxy + Java — load-balanced client failover
# HAProxy configuration for RabbitMQ cluster
frontend rabbitmq
    bind *:5672
    mode tcp
    default_backend rabbitmq_nodes

backend rabbitmq_nodes
    mode tcp
    balance roundrobin
    option tcp-check
    server node1 node1:5672 check inter 3s rise 2 fall 3
    server node2 node2:5672 check inter 3s rise 2 fall 3
    server node3 node3:5672 check inter 3s rise 2 fall 3

# Spring AMQP reconnect configuration
@Bean
public CachingConnectionFactory connectionFactory() {
    CachingConnectionFactory cf = new CachingConnectionFactory();
    cf.setAddresses("haproxy:5672");  // or comma-sep: node1:5672,node2:5672

    // Reconnect settings
    cf.getRabbitConnectionFactory().setAutomaticRecoveryEnabled(true);
    cf.getRabbitConnectionFactory().setNetworkRecoveryInterval(5000);
    cf.getRabbitConnectionFactory().setConnectionTimeout(5000);

    return cf;
}

// Consumer reconnect: Spring AMQP listener containers reconnect automatically
// Producer: catch AlreadyClosedException and retry with CachingConnectionFactory

Key Points to Remember

  • 1Classic queue contents are NOT replicated in a cluster — only quorum queues replicate via Raft.
  • 2Classic mirrored queues are deprecated since 3.9; migrate to quorum queues for HA.
  • 3A 3-node cluster is the minimum for fault tolerance; a 2-node cluster provides no HA (both nodes required).
  • 4Erlang cookie must be identical on all nodes — it is the shared secret for inter-node authentication.
  • 5Quorum queues require (N/2 + 1) nodes online; a network partition that loses quorum makes the queue unavailable.
  • 6Use HAProxy or the official RabbitMQ Cluster Kubernetes Operator for production cluster management.

Interview Questions

Sign in to ask Aria
1

What data does RabbitMQ cluster share across nodes and what is NOT shared by default?

EasyVMware
2

How many nodes can a 3-node quorum queue cluster lose and still function?

EasyPivotal
3

Why are classic mirrored queues deprecated and what replaces them?

MediumZalando
4

What happens to a quorum queue if a network partition splits the cluster into two equal halves?

HardNetflix
5

How does a Spring AMQP client recover from a broker node failure in a 3-node cluster?

MediumAmazon

Ask Aria about RabbitMQ Clustering

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…