Operations 8 min read

How to Achieve RabbitMQ High Availability with HAProxy: A Step‑by‑Step Guide

This tutorial explains why HAProxy is essential for RabbitMQ clusters, walks through installing HAProxy on Ubuntu, configuring load‑balancing and health‑check parameters, integrating with Java applications, and validating automatic failover to ensure high availability and efficient resource utilization.

Xiao Liu Lab
Xiao Liu Lab
Xiao Liu Lab
How to Achieve RabbitMQ High Availability with HAProxy: A Step‑by‑Step Guide

Why Use HAProxy with RabbitMQ?

Even with quorum queues, a RabbitMQ cluster faces two critical issues: a single‑point dependency that throws java.net.ConnectException when a node fails, and resource waste because all requests target one node while others stay idle. HAProxy acts as a unified gateway, distributing requests across nodes and automatically detecting failures to switch traffic without code changes.

Installing HAProxy on Ubuntu

Update package sources: apt-get update Install HAProxy: apt-get install haproxy Verify installation: systemctl status haproxy (status should be active)

Enable auto‑start on boot: systemctl enable haproxy Check version:

haproxy -v

Key HAProxy Configuration

Edit /etc/haproxy/haproxy.cfg (use vim or any editor) and add the following sections at the end:

# HAProxy web stats interface
listen stats
    bind *:8100
    mode http
    stats enable
    stats realm Haproxy Statistics
    stats uri /
    stats auth admin:admin

# RabbitMQ load‑balancing configuration
listen rabbitmq
    bind *:5670   # unified access port (different from default 5672)
    mode tcp
    balance roundrobin
    server rabbitmq1 127.0.0.1:5672 check inter 5000 rise 2 fall 3
    server rabbitmq2 127.0.0.1:5673 check inter 5000 rise 2 fall 3
    server rabbitmq3 127.0.0.1:5674 check inter 5000 rise 2 fall 3

Important parameters:

Load‑balancing algorithm : roundrobin (default), weighted roundrobin, or leastconn for different traffic patterns.

Health‑check settings : check inter 5000 (check every 5 seconds), rise 2 (node considered up after 2 consecutive successes), fall 3 (node marked down after 3 failures).

Applying the Configuration

After editing, restart HAProxy to apply changes:

systemctl restart haproxy

Integrating with Java Applications

Modify the Spring Boot RabbitMQ connection settings to point to HAProxy instead of a specific node:

spring:
  rabbitmq:
    addresses: amqp://admin:password@haproxy-ip:5670/test
    publisher-confirm-type: correlated
    listener:
      simple:
        acknowledge-mode: auto

Now the application only needs the HAProxy IP and port (5670); HAProxy handles node selection.

Verification

Normal operation : Start the RabbitMQ cluster and the Java app, then call http://127.0.0.1:8080/producer/test. A successful response indicates messages are being delivered.

Failover test : Stop one RabbitMQ node with rabbitmqctl -n rabbit stop_app. Subsequent requests still succeed because HAProxy routes traffic to healthy nodes.

Conclusion

Using HAProxy with RabbitMQ provides three major benefits: automatic high‑availability through failover, improved concurrency by distributing load, and easier maintenance since adding or replacing nodes requires no code changes. This solution fits micro‑services, distributed systems, and any scenario demanding reliable message communication.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javahigh availabilityload balancingLinuxRabbitMQHAProxy
Xiao Liu Lab
Written by

Xiao Liu Lab

An operations lab passionate about server tinkering 🔬 Sharing automation scripts, high-availability architecture, alert optimization, and incident reviews. Using technology to reduce overtime and experience to avoid major pitfalls. Follow me for easier, more reliable operations!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.