High availability MQTT Cluster

High Availability MQTT Cluster
Ranjith kumar DSM October 24, 2018

High availably MQTT Cluster is very important for any production IoT deployments. Failures can happen due to Software failures, Disk errors, network connectivity, Hardware component failures, etc.,  To ensure that your services are up and running always to your customers, the services need to be functionally and physically redundant. This article helps you to set up a high availability MQTT Cluster with the MQTTRoute, the fastest MQTT Broker. Our IoT Platform  which is a complete SaaS based Framework targeting large scale multi tenancy has High availability built inside it. You need at 5 machines ( or VMs) to set up each and every component as shown in the Image above. We recommend all the machines to be Ubuntu 14 or 16.

Database Cluster High Availability

Data Storage is a crucial component for any production application. It is mandatory to have a Master/ Slave set up for your database storage. You can refer to the Digital Ocean document for setting up the same. Alternatively you can use Amazon RDS or Azure Data Store which by default provides the redundancy of the Data storage and much more features like time based snapshots for recovery.

Dockerize the MQTT Broker

You can either run the MQTT broker using our scripts or dockerize it if needed. A Complete article on how to dockerize the MQTT Broker is available here. Dockerizing will provide you some assistance in service manageability like auto restart and more. You can get your copy of the MQTT Broker from the download page.

Set up High Availability MQTT Cluster

You need to run the dockerized MQTT Broker in two machines in parallel to set up a cluster. Even though both the brokers are fully functional, only the MQTT Broker will be marked as Active Broker which will serve all the requests from the devices and from the Web UI. The second broker will be a hot standby which will start working when the main broker is down.

Enabling High Availability with Load Balancer

Set up the nginx in one of the machines as the load balancer. Following are the steps to configure the same.

Update and Install Nginx.

$ sudo apt-get update

$ sudo apt-get install nginx

Configure the Nginx proxy

$ sudo vim /etc/nginx/nginx.conf

The routing needs to be done for both the Web UI and the MQTT over TCP and MQTT over Web Socket connectivity.

http {

     include mime.types;

     default_type application/octet-stream;

     proxy_connect_timeout 2s;

     proxy_read_timeout 2s;

      sendfile on;

  keepalive_timeout 65;

upstream Brokerui {


        server (Standby MQTTRoute ip):8080 fail_timeout=1s max_fails=1;

         server (Standby MQTTRoute ip):8080 backup;


  server {

  listen 80;

         location / {

             proxy_pass http://Brokerui;


error_page 500502503504 /50x.html;

         location = /50x.html {

         root html;

} } }

stream {

upstream broker {

server (Active MQTTRoute IP ):1883 fail_timeout=1s max_fails=1;

server (Standby MQTTRoute ip);1883 backup;


upstream websocket {

server (Active MQTTRoute IP ):10443 fail_timeout=1s max_fails=1;

server (Standby MQTTRoute ip):10443 backup;


server {

listen 1883;

proxy_pass broker ;

proxy_connect_timeout 1s;


server {

listen 10443;

proxy_pass websocket;

proxy_connect_timeout 2s;

} }

Now your high availability MQTT cluster is ready for production

Validate the High Availability MQTT Cluster

The production ready MQTT Client should have the necessary code to reconnect whenever there is a disconnection issues. Refer to best practices for building  MQTT Client.

  1. Connect a MQTT client that has reconnection support and connect to your Public IP of your Load Balancer machine.
  2. You can see the device on the User Interface of the MQTTRoute and also you will start seeing the MQTT messages.
  3. Once the complete setting up is running, stop the Active MQTT Broker using the sh stop broker.sh or the docker command.
  4. You can see your edge client disconnect, reconnected again and start continue with its messaging and you will start seeing it seamlessly on the MQTT Broker User Interface.
  5. The Standby MQTT Broker has now become active and started serving the requests.

Failure & Update the set up

If the active MQTTRoute fails, the Hot Standby MQTT Broker will become the active broker. This makes it mandatory to set up another MQTT Broker for the hot standby. So set up another instance of Ubuntu and set up the MQTT Broker in it. Go back to the Nginx configuration and change the IP Addresses of the MQTTRoute accordingly and run the following command

Check the correctness of the syntax

$ sudo nginx -t

Reload the configuration.

$sudo nginx -s reload

MQTTRoute is the fastest MQTT Broker that will be helpful if you are planning to run an IoT deployment of tens of thousands of devices for a single business or a few business that falls under a single use case.

Download MQTTRoute now to get started with your High availability MQTT Cluster.

High Availability MQTT Cluster

Feel free to write to support for any queries or suggestions.

For large scale deployments, try the IoT Platform which is hosted at Devicemanager.bevywise.com All our products can be white labelled and used on private cloud or in premise.