High availability MQTT Cluster is very important for any IoT deployments in production environment. 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 without a single point of failure, the  services need to be functionally and physically redundant. By default MQTT protocol does not support high availability.  But the resilient high availability set up can be done with simple steps with the right MQTT Broker. This article helps you to set up a high availability MQTT Cluster with the Bevywise MQTT Broker, the fastest MQTT Broker in the market. Our IoT Platform  which is a complete SaaS based Framework targeting large scale multi tenancy has high availability built inside it. You need at least 5 machines ( or Virtual machines) 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 FREE 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 one MQTT Broker in the cluster node will be marked as Active Broker which will serve all the requests (publishes and subscribes) from the devices and from the Web UI. The second broker in other cluster node will be a hot standby which will start working when the main broker is down. The major advantage of this set up is that when one of the MQTT servers / brokers fails, the other one will start working to prevent failure.

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 127.0.0.1:8080;

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

         server (Standby MQTT Broker 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 MQTT Broker IP ):1883 fail_timeout=1s max_fails=1;

server (Standby MQTT Broker ip);1883 backup;

}

upstream websocket {

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

server (Standby MQTT Broker 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 MQTT Broker 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 MQTT Broker 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 theMQTT Broker accordingly and run the following command

Check the correctness of the syntax

$ sudo nginx -t

Reload the configuration.

$sudo nginx -s reload

Your MQTT high availability cluster set up is ready now. Hope this blog post provides a clear understanding on High availability cluster. Try yourself by setting it up.

Bevywise MQTT Broker is the fastest & best 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 MQTT Broker now for FREE to get started with your High availability MQTT Cluster. The new version of MQTT Broker, MQTTRoute 3.1 is available now with custom authentication & more.

download now

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

For large scale deployments, try Bevywise IoT Platform by signing up for free which is hosted at mqttserver.com All our products can be white labelled and used on any private cloud infrastructure or in premise.