In this article, we’ll walk through the steps to set up a high-performance RabbitMQ cluster using Docker and Docker Compose. This setup will help you handle high traffic rates efficiently, leveraging the powerful infrastructure of your dedicated server.

Introduction Link to heading

RabbitMQ is a robust messaging broker that facilitates the seamless exchange of messages between different parts of your applications. Setting up a high-performance RabbitMQ cluster ensures that your messaging system can handle high traffic loads and provides high availability.

Step 1: Create a Docker Network Link to heading

First, create a Docker network to allow communication between RabbitMQ nodes:

docker network create rabbitmq_cluster

Step 2: Create Docker Compose Configuration Link to heading

services:
  rabbitmq1:
    image: rabbitmq:3.11-management
    container_name: rabbitmq1
    hostname: rabbitmq1
    environment:
      RABBITMQ_ERLANG_COOKIE: 'mysecretcookie'
      RABBITMQ_DEFAULT_USER: 'admin'
      RABBITMQ_DEFAULT_PASS: 'admin'
      RABBITMQ_DEFAULT_VHOST: '/'
    networks:
      - rabbitmq_cluster
    ports:
      - "15672:15672"
      - "5672:5672"
    volumes:
      - rabbitmq1-data:/var/lib/rabbitmq
      - ./rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf

  rabbitmq2:
    image: rabbitmq:3.11-management
    container_name: rabbitmq2
    hostname: rabbitmq2
    environment:
      RABBITMQ_ERLANG_COOKIE: 'mysecretcookie'
      RABBITMQ_DEFAULT_USER: 'admin'
      RABBITMQ_DEFAULT_PASS: 'admin'
      RABBITMQ_DEFAULT_VHOST: '/'
    networks:
      - rabbitmq_cluster
    volumes:
      - rabbitmq2-data:/var/lib/rabbitmq
      - ./rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf

  rabbitmq3:
    image: rabbitmq:3.11-management
    container_name: rabbitmq3
    hostname: rabbitmq3
    environment:
      RABBITMQ_ERLANG_COOKIE: 'mysecretcookie'
      RABBITMQ_DEFAULT_USER: 'admin'
      RABBITMQ_DEFAULT_PASS: 'admin'
      RABBITMQ_DEFAULT_VHOST: '/'
    networks:
      - rabbitmq_cluster
    volumes:
      - rabbitmq3-data:/var/lib/rabbitmq
      - ./rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf

networks:
  rabbitmq_cluster:
    external: true

volumes:
  rabbitmq1-data:
  rabbitmq2-data:
  rabbitmq3-data:

RabbitMQ Configuration Link to heading

Create a rabbitmq.conf file with the following content, optimized for high performance:

# rabbitmq.conf

# Disable guest user access
loopback_users.guest = false

# Listener configuration
listeners.tcp.default = 5672
management.listener.port = 15672

# Performance tuning
vm_memory_high_watermark.relative = 0.7
disk_free_limit.absolute = 50GB

# Networking tuning
tcp_listen_options.backlog = 4096
tcp_listen_options.nodelay = true
tcp_listen_options.linger.on = false

# Enable HiPE compilation for improved performance
hipe_compile = true
hipe_modules = rabbit_mgmt,rabbit_mgmt_db,rabbitmq_management,rabbitmq_management_agent

# Enable garbage collection tuning
collect_statistics_interval = 5000
channel_max = 2048

# Tuning the number of file descriptors
total_available_cpus = 80

# Queues and messages
default_vhost = /
default_user = admin
default_pass = admin

# High availability
cluster_formation.peer_discovery_backend = rabbit_peer_discovery_classic_config
cluster_formation.classic_config.nodes.1 = rabbit@rabbitmq1
cluster_formation.classic_config.nodes.2 = rabbit@rabbitmq2
cluster_formation.classic_config.nodes.3 = rabbit@rabbitmq3
cluster_partition_handling = autoheal
queue_master_locator = min-masters

# Federation and Shovel plugins (if needed)
# Enable the necessary plugins by adding the following lines
# plugins = rabbitmq_management,rabbitmq_federation,rabbitmq_federation_management,rabbitmq_shovel,rabbitmq_shovel_management

# Advanced performance settings
# Set maximum file descriptors
file_handle_cache_max = 2000000
# Configure Erlang scheduler settings
kernel.threads = 5000
kernel.thread_pool_size = 500
# Erlang garbage collection tuning
vm_memory_high_watermark.paging_ratio = 0.9
vm_memory_high_watermark.absolute = 100GB

Step 3: Launch the RabbitMQ Cluster Link to heading

docker compose up -d

Step 5: Configure RabbitMQ Cluster Link to heading

Connect to each RabbitMQ container and join them to the cluster. For example, for rabbitmq1:

docker exec -it rabbitmq1 bash
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app
exit

Repeat these steps for rabbitmq2 and rabbitmq3, but join them to the cluster:

docker exec -it rabbitmq2 bash
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster rabbit@rabbitmq1
rabbitmqctl start_app
exit
docker exec -it rabbitmq3 bash
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster rabbit@rabbitmq1
rabbitmqctl start_app
exit

Step 6: Set High Availability Policy Link to heading

Set a high availability (HA) policy on any one of the nodes to propagate throughout the cluster:

docker exec -it rabbitmq1 bash
rabbitmqctl set_policy ha-all ".*" '{"ha-mode":"all","ha-sync-mode":"automatic"}'
exit

Conclusion Link to heading

By following these steps, you should have a high-performance RabbitMQ cluster up and running on your dedicated server. This setup is designed to handle high traffic rates efficiently, leveraging the power of Docker and Docker Compose for easy management and scalability.