Clustering
How nodes cluster, Khepri, network partitions, maintenance mode, and upgrades
A RabbitMQ cluster is a set of nodes that share metadata (users, vhosts, exchanges, bindings, queue definitions, policies) and route messages to each other. Message data is not shared automatically: a queue's messages live on its own members, which is what quorum queues and streams replicate.
Forming the cluster
This stack uses classic config peer discovery: each node has the full
list in rabbitmq.conf and joins the others at first boot. Other backends
discover peers from DNS, Kubernetes, Consul, or AWS. Manual clustering also
works:
# join rabbit-3 to rabbit-1's cluster by hand (wipes rabbit-3's data)
docker exec rabbit-3 rabbitmqctl stop_app
docker exec rabbit-3 rabbitmqctl reset
docker exec rabbit-3 rabbitmqctl join_cluster rabbit@rabbit-1
docker exec rabbit-3 rabbitmqctl start_appRequirements, all of which the compose file handles: every node has the same Erlang cookie, a stable hostname, and can reach the others on the Erlang distribution port (25672) and the inter-node ports.
Khepri: Raft for metadata
RabbitMQ 4 stores cluster metadata in Khepri, a Raft-based store
(the khepri_db feature flag is enabled on this cluster). Before Khepri,
metadata lived in Mnesia, which could disagree with itself after a network
partition and needed a cluster_partition_handling strategy to recover.
With Khepri, metadata changes need a majority of nodes, just like quorum
queues:
- A node cut off in a minority can't change metadata (declare queues, add bindings) until it rejoins.
- The majority side keeps operating.
- When the partition heals, the minority catches up from the Raft log. There's nothing to reconcile by hand.
This is the same reason every system in this repo with replicated state uses a consensus protocol with an odd number of members: Kafka's KRaft controllers, Redis Cluster's primaries, Sentinel's quorum, and now RabbitMQ's metadata and quorum queues. Three nodes tolerate one failure; five tolerate two. Even numbers add cost without adding tolerance.
Where queues live
Clients can connect to any node, but each queue has a leader on one node. A consumer connected to rabbit-1 reading a queue led by rabbit-2 works fine, with an extra network hop inside the cluster.
Maintenance and rolling restarts
Check what a drain actually moved. On this cluster, drain moved every
quorum queue's leader off rabbit-2, but the stream events.stream kept
its leader there (list_queues name type leader still showed
rabbit@rabbit-2). Stopping the node still fails the stream over to a
replica, but stream clients see the leader change at shutdown, not
during the drain. Verify leaders after draining rather than assuming.
A safe rolling upgrade: for each node, check it's not quorum-critical,
drain it, upgrade and restart it, revive it, wait for every quorum queue to
have all members again, then move on. Upgrades between minor versions are
done this way with no downtime; check the release notes for feature flags
to enable afterwards (rabbitmqctl enable_feature_flag all).
Clusters across data centres
Don't stretch one cluster across regions: Raft needs low latency between members, and every write waits for a majority. For multiple sites, run a cluster per site and move messages between them with Shovel (copies messages from a queue to an exchange elsewhere) or Federation (an exchange or queue that pulls from an upstream cluster).
Lab
- Run
rabbitmqctl cluster_statusand find the running nodes, feature flags, and listeners. - Drain
rabbit-2, list queues with their types and leaders, and find what's still led byrabbit-2. Revive it. - Stop
rabbit-3andrabbit-2. Try to declare a queue from a client connected torabbit-1. Start them again. - Run
rabbitmq-upgrade await_online_quorum_plus_onewith all nodes up, and again with one down.