Skip to content

What's your UNS maturity level? Get a custom report: Take the UNS Maturity Assessment

Load Balancing MQTT Clients: A Beginner’s Guide

by Matthew Neal
9 min read

In this beginner’s guide to load balancing MQTT clients, we'll delve into the intricacies of MQTT client load balancing, exploring best practices, techniques, and tools to achieve a scalable and reliable MQTT infrastructure. 

Before diving into load balancing, this article assumes that you have reviewed the content in our MQTT Essentials series or that you have completed our MQTT Professional Certification.

The Importance of Load Balancing MQTT Clients

In MQTT, deployments handling a large number of clients along with an uneven distribution of client connections across brokers can lead to performance bottlenecks, increased latency, and potential service disruptions. Load balancing plays a crucial role in distributing client connections evenly across multiple brokers, ensuring optimal resource utilization, scalability, and fault tolerance.

MQTT Client Load Balancing and Shared Subscriptions

In a previous blog post we introduced the concept of Shared Subscriptions. As quoted in this blog, shared subscriptions are a core feature of MQTT 5. They enable multiple MQTT clients to share a single subscription on the broker. In essence, this feature allows messages on a topic to be distributed among multiple clients, thereby improving load balancing and fault tolerance in an MQTT system.

If Shared Subscriptions are used, all clients that share the same subscription with a subscription group will receive messages in an alternating fashion. This mechanism is sometimes referred to as client load balancing, since the message load of a single topic is distributed amongst all subscribers.

Review of HiveMQ Clusters

As we wrote in our blog post, Creating highly available and ultra-scalable MQTT clusters, an MQTT broker cluster is a distributed system that represents one logical MQTT broker. It consists of many different MQTT broker nodes that are typically installed on different physical machines and are connected over a network. From a MQTT client’s perspective, a cluster of brokers behaves like a single MQTT broker.

A HiveMQ Broker cluster is perfect for cloud environments due to the advantages in scalability, elasticity, and resilience. Deployments on Virtual Machines on cloud infrastructure providers like Amazon Web Services, Microsoft Azure, or Google Cloud Platform are as easy to operate and scale as with classic on-premises data center infrastructure.

MQTT Client and broker communication without load balancingWithout Load Balancing

MQTT client and broker communication with load balancingWith Load Balancing

Four Common Strategies for Load Balancing MQTT Clients

1. Least Connections Load Balancing 

This strategy directs client connections to the MQTT broker with the fewest active connections at any given time. By dynamically routing incoming connections to brokers with lighter loads, this approach helps distribute the workload evenly across the available resources. It's particularly useful in scenarios where client connections vary in duration or intensity, allowing the system to adapt to changing traffic patterns effectively.

2. Round-Robin Load Balancing 

A simple yet effective approach where client connections are evenly distributed across a pool of MQTT brokers in a cyclical manner. This ensures that each broker receives an equal share of client connections, thereby preventing overloading of any single broker.

3. Weighted Load Balancing 

Assigning weights to brokers based on their capacity or performance allows for more fine-grained control over the distribution of client connections. Brokers with higher capacities or better performance can be assigned higher weights, resulting in a more balanced distribution of client traffic.

4. Dynamic Load Balancing 

Utilizing dynamic load balancing algorithms that take into account real-time metrics such as broker CPU utilization, memory usage, and network latency. This allows for adaptive load balancing where client connections are routed to the least loaded brokers, ensuring optimal performance and resource utilization.

Each strategy above has its advantages. Most MQTT implementations achieve scalable results by implementing a “Least Connections” load balancing method. However, each environment is unique. Implementing a combination or selecting the most suitable load balancing strategy based on your environment's specific requirements can lead to an efficiently balanced and highly scalable MQTT infrastructure.

Platforms for MQTT Load Balancing

HiveMQ can work with most load balancer hardware and software vendors. Two of the most popular open source load balancers are:

1. NGINX: A is an open-source web server and reverse proxy that can be used for load balancing MQTT traffic. NGINX's flexible configuration options and support for various load balancing algorithms make it well-suited for distributing MQTT client connections across multiple brokers.

2. HAProxy: An open-source load balancer that supports TCP load balancing and least connections. HAProxy has a robust feature set and high performance makes it a popular choice for high-throughput MQTT deployments. Read our blog, Using HAProxy to Load Balance HiveMQ with the New Health API, to learn how to load balance a MQTT broker from HiveMQ and configure the load balancer for node health check.

Conclusion

Efficient load balancing of MQTT clients is essential for ensuring scalability, reliability, and optimal performance for your MQTT deployment. By employing appropriate load balancing strategies and leveraging tools such as NGINX or HAProxy, along with HiveMQ Broker clustering, organizations can build resilient and high-performance MQTT infrastructures capable of handling the demands of modern IoT applications.

In our upcoming article, Load Balancing MQTT Brokers: An Advanced Guide, we cover load balancing for the HiveMQ Broker, TLS Offloading, health checks, load balancing HiveMQ extensions and other best practices for load balancing the broker to on-prem and cloud systems. Stay tuned!

Matthew Neal

Matthew Neal is a Senior Solutions Engineer at HiveMQ with over twenty years of experience in enterprise infrastructure and technical presales. He has an excellent track record of successfully generating multimillion-dollar revenue streams and expanding business value. An expert in managed services, multi-cloud networking, cybersecurity architecture, IoT, MQTT, and more, Matthew is known for his ability to build exceptional customer relationships, effectively remove technical roadblocks, solve complex customer requirements, and incorporate best practices.

  • Matthew Neal on LinkedIn
  • Contact Matthew Neal via e-mail
HiveMQ logo
Review HiveMQ on G2