HiveMQ and Load Balancers
A load balancer is a specialized technology used to evenly distribute incoming traffic across a cluster of servers.
Load balancing is not a core feature of MQTT as defined in the official MQTT specification and the HiveMQ platform does not ship with or require the use of any specific load balancer. However, for production use cases, HiveMQ clusters are usually placed behind a load balancer to further enhance scalability and improve overall system performance.
The use of a load balancer in your MQTT infrastructure can offer several advantages:
Increase scalability: Load balancers enable horizontal scalability that dynamically distributes incoming connection requests across available brokers. This allows the system to add brokers as needed to handle increased traffic and reduces stress on individual broker nodes.
Improve availability: Load balancing ensures that if one broker instance fails or experiences difficulty, new incoming requests are redirected to the remaining healthy nodes. This filters out overloaded servers and minimizes the impact of individual node failures on the availability of the overall system.
Enhance efficiency: Load balancing employs algorithms to determine the distribution of work based on the dynamic state of the system. This adaptability makes it possible to evenly utilize broker capabilities for optimal system performance.
Heighten security: Load balancers provide an entry point into the internal system. This allows the load balancer to inspect and filter incoming requests to limit unwanted traffic. Additionally, the load balancer hides server details such as the number of brokers and the broker IP addresses from the connecting clients.
HiveMQ works seamlessly with a wide range of load balancers such as HAProxy, NGINX, and others.
For more information, see our blog post Using HAProxy to Load Balance HiveMQ with the New Health API.
In a HiveMQ deployment, the load balancer sits between your MQTT clients (publishers and subscribers) and multiple HiveMQ brokers.The primary goal of a load balancer in MQTT is to efficiently manage client connections to maintain an even distribution of incoming MQTT messages to all available brokers.
Load balancers use various algorithms to determine how to distribute incoming traffic among the brokers. Commonly used load balancing algorithms include round-robin, least connections, weighted distribution, and IP hash.
|The least connections algorithm is a popular choice for load balancing MQTT broker clusters due to its simplicity, scalability, fault tolerance, and ability to adapt to changing conditions. However, the selection of a suitable load balancing algorithm depends on your specific deployment requirements, the load balancers capabilities, and your broker cluster configuration.|
The following diagrams illustrate the basic setup of a HiveMQ deployment with and without a load balancer:
|Your HiveMQ broker provides numerous ways to optimize performance and ensure high availability straight out of the box. To see how HiveMQ shared subscriptions automatically distribute messages among a subscription group in a balanced manner, see Shared Subscriptions. To learn how HiveMQ protects your cluster from stressful situations, see Cluster Overload Protection and Connect Overload Protection.|
HiveMQ is optimized to use all available resources on a system to achieve the highest possible throughput of messages.
If a load balancer is in use, we recommend that you offload the SSL/TLS termination to the load balancer. SSL/TLS termination puts a high load on the system CPU and can negatively impact the message throughput of HiveMQ deployment. Offloading the SSL/TLS termination onto the load balancer ensures that a constant level of message throughput can be maintained.
See Proxy Protocol to learn how to use PROXY Protocol with load balancers.
When you configure an idle timeout, it is important to consider that in accordance with the MQTT specification the HiveMQ broker closes the MQTT connection of a client if the MQTT client does not receive any packets for 1.5 times its keep-alive value.
The load balancer closes the underlying TCP connection when no bidirectional traffic has been detected for the duration of the idle timeout.
To avoid unwanted behavior, you must configure the idle timeout of the load balancer to be at least 1.5 times the configured keep-alive value of the MQTT clients. Otherwise, clients lose their connections as soon as they stop sending or receiving MQTT messages with a quality of service level greater than 0 for the time that is configured as idle timeout at the load balancer.
|The idle timeout needs to be at least 1.5 times the configured keep-alive value of the clients.|
It can sometimes be desirable to decrease the size of a cluster in your HiveMQ deployment. For example, due to maintenance work on the machines on which HiveMQ runs or during a rolling upgrade of your HiveMQ deployment. In these cases, all clients connected to the node that is being shut down temporarily lose their MQTT connections and typically reconnect immediately (depending on the MQTT client implementation). This activity results in a high number of connection requests at the load balancer and the HiveMQ cluster. To accommodate this peak in network requests, we recommend that you remove a node that is scheduled to be shut down from the load balancer ahead of time. This method causes the MQTT clients to automatically distribute the workload among the remaining nodes before the node is shut down and results in a smaller peak of connection requests.