How to Build a Fault-Tolerant RabbitMQ Cluster with Quorum Queues Using Docker Compose

Docker tutorial - IT technology blog
Docker tutorial - IT technology blog

An Unforgettable Outage: When the Lone Broker Crashed at Midnight

Early in my career, the first backend cluster I managed ran on just a single, standalone RabbitMQ container. Everything went smoothly until the Singles’ Day (11/11) mega sale. A sudden spike in order volume completely exhausted server RAM. Without hesitation, Docker’s OOM killer wiped out the RabbitMQ container right in the middle of peak traffic.

The aftermath was catastrophic: over 30,000 checkout and inventory deduction messages were deadlocked. Microservices lost connection across the board, and the system remained frozen for 40 minutes before being rescued. It was an expensive lesson in Single Point of Failure (SPOF).

For a production system to survive under pressure, its message broker must meet High Availability (HA) standards. When one node drops dead, the surviving nodes must shoulder the workload immediately, with zero data loss.

How Do RabbitMQ Clusters and Quorum Queues Work?

RabbitMQ allows you to pool multiple servers into a unified cluster. Nodes recognize each other and exchange data securely using a shared secret called the Erlang Cookie.

Why Quorum Queues Completely Replaced Classic Mirroring

In older RabbitMQ versions, we commonly relied on Classic Mirrored Queues to replicate messages across servers. However, this mechanism easily caused severe bottlenecks during network partitions. In fact, RabbitMQ has completely deprecated and removed Classic Mirroring starting with version 4.0.

The current gold standard is Quorum Queues, which operate on the Raft consensus algorithm (similar to etcd and Consul):

  • Strict Data Durability: A message is only considered successfully written once a majority of nodes (formula: N/2 + 1) commit the data to disk. For a 3-node cluster, at least 2 nodes must acknowledge. Even if 1 node drops, the cluster operates seamlessly.
  • Automatic Leader Election: Each queue has a single Leader node handling reads and writes, while the rest serve as Followers. If the Leader fails, Followers automatically elect a new Leader in under a second.
  • Smooth Reconnection: A restarted node can rejoin the cluster and quickly catch up on missing state without freezing the entire queue as older mechanisms did.

Hands-On: Setting Up a 3-Node RabbitMQ Cluster with Docker Compose

Our lab setup consists of 3 containers: rmq1, rmq2, and rmq3. All three connect to a shared Docker bridge network and automatically discover one another upon startup.

Step 1: Create the Project Directory

Open your terminal and create the directory structure:

mkdir rabbitmq-cluster && cd rabbitmq-cluster
mkdir -p config

Step 2: Configure Auto-Clustering

You don’t need to exec into each container to manually run cluster join commands. RabbitMQ supports automatic node discovery via Docker’s internal DNS. Create the file config/rabbitmq.conf:

loopback_users.guest = false
listeners.tcp.default = 5672
management.tcp.port = 15672

# Automatic node discovery mechanism based on Docker DNS
cluster_formation.peer_discovery_backend = rabbit_peer_discovery_classic_config
cluster_formation.classic_config.nodes.1 = rabbit@rmq1
cluster_formation.classic_config.nodes.2 = rabbit@rmq2
cluster_formation.classic_config.nodes.3 = rabbit@rmq3

Step 3: Write the docker-compose.yml File

Create docker-compose.yml in your root project directory. Important note: The RABBITMQ_ERLANG_COOKIE string must match across all 3 containers character for character; otherwise, nodes will reject incoming connections.

version: '3.8'

networks:
  rmq-cluster-net:
    driver: bridge

services:
  rmq1:
    image: rabbitmq:3.13-management-alpine
    container_name: rmq1
    hostname: rmq1
    environment:
      - RABBITMQ_ERLANG_COOKIE=MY_SECRET_ERLANG_COOKIE_2026
    volumes:
      - ./config/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf:ro
    ports:
      - "15672:15672" # Web UI node 1
      - "5672:5672"   # AMQP port node 1
    networks:
      - rmq-cluster-net

  rmq2:
    image: rabbitmq:3.13-management-alpine
    container_name: rmq2
    hostname: rmq2
    environment:
      - RABBITMQ_ERLANG_COOKIE=MY_SECRET_ERLANG_COOKIE_2026
    volumes:
      - ./config/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf:ro
    ports:
      - "15673:15672" # Web UI node 2
      - "5673:5672"   # AMQP port node 2
    networks:
      - rmq-cluster-net

  rmq3:
    image: rabbitmq:3.13-management-alpine
    container_name: rmq3
    hostname: rmq3
    environment:
      - RABBITMQ_ERLANG_COOKIE=MY_SECRET_ERLANG_COOKIE_2026
    volumes:
      - ./config/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf:ro
    ports:
      - "15674:15672" # Web UI node 3
      - "5674:5672"   # AMQP port node 3
    networks:
      - rmq-cluster-net

Step 4: Launch the Cluster and Verify Node Health

Run the following command to pull images and spin up the containers:

docker compose up -d

Wait about 15 seconds for the nodes to complete their initial network handshakes. Next, verify the cluster health with:

docker exec -it rmq1 rabbitmqctl cluster_status

The Running Nodes section should display all three members: rabbit@rmq1, rabbit@rmq2, and rabbit@rmq3.

Beyond the CLI, you can query RabbitMQ’s HTTP API directly:

curl -s -u guest:guest http://localhost:15672/api/overview

The JSON response from this API is fairly extensive. To inspect RAM usage, free disk space, and connection counts per node without the clutter, I usually drop the raw JSON into toolcraft.app/en/tools/developer/json-formatter to inspect and format it directly in the browser without installing bulky extensions.

Step 5: Create a Quorum Queue and Simulate a Real Outage

Access the management dashboard at http://localhost:15672 (default credentials: guest / guest):

  1. Navigate to the Queues tab and click Add a new queue.
  2. Under Type, switch from Classic to Quorum.
  3. Name the queue orders.process and click Add queue.

The queue list will now display your new queue with type quorum, clearly identifying which node currently holds the Leader role.

Now, let’s simulate pulling the plug on the Leader node:

docker stop rmq1

Switch to Node 2’s dashboard at http://localhost:15673. The cluster remains green. The orders.process queue immediately promotes one of the two surviving nodes as the new Leader. Client message publishing and consumption continue uninterrupted without losing a single record.

Production Takeaways

A 3-node cluster paired with Quorum Queues effectively eliminates single-point-of-failure risks. When rolling this out to real production servers, keep two non-negotiable rules in mind: always maintain an odd number of nodes (3 or 5 nodes to allow the Raft algorithm to form a clear majority), and ensure /var/lib/rabbitmq is mounted to persistent external disk storage to preserve message state across server updates and restarts.

Share: