How to Build an High Availability MQTT Cluster for the IoT
Introduction
MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol widely used in Internet of Things (IoT) applications due to its efficiency and reliability. However, for critical IoT applications, a high availability MQTT cluster is essential to prevent data loss, ensure scalability, and provide fault tolerance.
This guide explains how to build a high availability MQTT cluster using Eclipse Mosquitto and EMQX, incorporating load balancing, redundancy, and failover mechanisms.
Why Do You Need a High Availability MQTT Cluster?
A single MQTT broker setup has the following limitations:
- Single point of failure (SPOF) – If the broker crashes, all clients lose connectivity.
- Scalability issues – One broker may not handle thousands of connections efficiently.
- Performance bottlenecks – A single broker can experience delays under high loads.
To overcome these issues, an MQTT cluster distributes the load across multiple brokers, ensuring redundancy and seamless failover.
Key Components of an MQTT Cluster
- Multiple MQTT Brokers – Several MQTT broker instances ensure redundancy.
- Load Balancer – Distributes client connections across brokers.
- Shared Backend (Database/Message Queue) – Ensures consistency across brokers.
- Monitoring & Logging – Helps detect failures and performance bottlenecks.
Step 1: Setting Up Multiple MQTT Brokers
1.1 Choosing an MQTT Broker
Popular MQTT brokers that support clustering:
- Eclipse Mosquitto (Lightweight, but requires external clustering solutions)
- EMQX (Built-in clustering support)
- VerneMQ (Scalable, built-in clustering support)
For this guide, we will use EMQX, as it provides native clustering and high availability.
1.2 Installing EMQX Brokers on Multiple Nodes
Install EMQX on Ubuntu servers:
wget https://www.emqx.io/downloads/latest/emqx-ubuntu20.04-amd64.deb
sudo dpkg -i emqx-ubuntu20.04-amd64.deb
sudo systemctl start emqx
Repeat this on at least two or more nodes.
Verify EMQX status:
sudo systemctl status emqx
Step 2: Configuring the MQTT Cluster
2.1 Enabling Clustering in EMQX
On each EMQX broker, edit the configuration file:
sudo nano /etc/emqx/emqx.conf
Locate the cluster settings section and add:
cluster.name = emqx-cluster
node.name = emqx@<server_ip>
Save and restart EMQX:
sudo systemctl restart emqx
2.2 Connecting Brokers to Form a Cluster
On one node, run:
emqx_ctl cluster join emqx@<other_node_ip>
Verify the cluster:
emqx_ctl cluster status
If successful, the output should show multiple nodes in the cluster.
Step 3: Setting Up Load Balancing
A Load Balancer is required to distribute MQTT client connections.
3.1 Using HAProxy as an MQTT Load Balancer
Install HAProxy on a separate server:
sudo apt update && sudo apt install haproxy -y
Edit the HAProxy configuration:
sudo nano /etc/haproxy/haproxy.cfg
Add the following configuration:
frontend mqtt_frontend
bind *:1883
default_backend mqtt_backend
backend mqtt_backend
balance roundrobin
server broker1 <emqx_node1_ip>:1883 check
server broker2 <emqx_node2_ip>:1883 check
Restart HAProxy:
sudo systemctl restart haproxy
Now, clients can connect to HAProxy’s IP (port 1883) instead of directly connecting to a broker.
Step 4: Ensuring Data Consistency with a Shared Backend
To maintain data consistency, use a shared backend such as:
- Redis (For session persistence)
- PostgreSQL/MySQL (For storing messages)
Example: Configure EMQX to use Redis for session persistence:
emqx_ctl plugins load emqx_redis
Then edit emqx.conf
to include Redis connection details.
Step 5: Setting Up Monitoring & Logging
5.1 Enable EMQX Dashboard
EMQX provides a built-in dashboard at:
http://<emqx_node_ip>:18083/
Default login:
Username: admin
Password: public
5.2 Integrate with Prometheus & Grafana
To monitor broker performance, install Prometheus:
sudo apt install prometheus -y
Edit prometheus.yml
to scrape EMQX metrics:
scrape_configs:
- job_name: 'emqx'
static_configs:
- targets: ['<emqx_node1_ip>:18083']
Then visualize metrics in Grafana.
Step 6: Testing the MQTT Cluster
6.1 Publish & Subscribe Messages
Install MQTT CLI:
sudo apt install mosquitto-clients -y
Publish a test message:
mosquitto_pub -h <haproxy_ip> -t "test/topic" -m "Hello MQTT Cluster"
Subscribe to the topic:
mosquitto_sub -h <haproxy_ip> -t "test/topic"
If the message is received, your MQTT cluster is working correctly!
6.2 Simulate Broker Failure
Stop one EMQX node:
sudo systemctl stop emqx
Publish a message again and confirm that the cluster is still operational.
Conclusion
In this guide, we covered how to build a high availability MQTT cluster for IoT using EMQX brokers, HAProxy for load balancing, and Redis for session persistence. By implementing these techniques, you can ensure your MQTT infrastructure is fault-tolerant, scalable, and reliable for large-scale IoT deployments.
Next Steps:
- Implement TLS encryption for secure communication.
- Use MQTT authentication plugins (JWT, OAuth, etc.).
- Deploy in Docker/Kubernetes for better scalability.
With this setup, your IoT applications can handle millions of connections with minimal downtime! 🚀