
- ZeroMQ - Home
- ZeroMQ - Overview
- ZeroMQ - Installation
- ZeroMQ - Features
- ZeroMQ Messaging
- ZeroMQ - Socket Types
- ZeroMQ - Communication Patterns
- ZeroMQ - Transport Protocols
- ZeroMQ - Message Framing
- Scaling & Performance
- ZeroMQ - Load Balancing
- ZeroMQ - SMP
- ZeroMQ - Multithreading
- ZeroMQ - Performance Considerations
- ZeroMQ Useful Resources
- ZeroMQ - Quick Guide
- ZeroMQ - Useful Resources
- ZeroMQ - Discussion
ZeroMQ - Load Balancing
Load Balancing is one of the powerful messing patterns. It helps distribute tasks across multiple workers, enabling scalable and fault-tolerance applications. ZeroMQ has several patterns for load balancing, but the most common ones are the Request-Reply (REQ-REP) and Push-Pull (PUSH-PULL) patterns.

Following are the ZeroMQ pattern for load balancing −
REQ-REP Pattern: In this pattern, a client sends a request to a load balancer, which then forwards the request to one of the available networks. The worker processes the request and sends the response back to the load balancer which then forwards it to the client.
Dealer-Router Pattern: In this pattern, a dealer socket is used to distribute incoming messages across multiple workers. Each worker connects to the dealer socket using a router socket.
Router-Dealer Pattern: In this pattern a router socket is used to distribute incoming message across multiple workers. Each worker connect to the router socket using a dealer socket.
Achieving Load Balancing with ZeroMQ?
Here are some ways to achieve load balancing with ZeroMQ −
- Round-Robin (RR) Load Balancing: In this approach, each incoming message is forwarded to the next available worker in the circle list. This is the simplest load balance and is appropriate for most cases.
- Least Recently Used (LRU) Load Balancing: In this approach, each incoming message is forwarded to the long-term worker. This approach is useful when workers have different abilities or when some workers are slower than others.
- IPC (Inter-Process Communication) Load Balancing: In this approach, ZeroMQ uses IPC to communicate between operators, enabling more efficient and faster messaging.
- Device Load Balancing: In this approach, ZeroMQ uses a device (such as a network interface card) to distribute incoming messages to multiple workers.
- Queue Device Load Balancing: In this approach, ZeroMQ uses a queuing mechanism to distribute incoming messages to multiple workers.
Load Balancing in ZeroMQ
The following are the options to configure load balancing with zeroMQ −
- ZeroMQ_LB: It enables load balancing on a socket.
- ZeroMQ_LB_INTERVAL: It sets the interval in milliseconds between load balancing decisions.
- ZeroMQ_LB_THRESHOLD: It sets the threshold in millisecond for the worker to be considered a idle.
Example
The following is an example that demonstrates how to implement load balancing using the REQ-REP pattern in ZeroMQ −
LoadBalancer class
import org.zeromq.ZMQ; import org.zeromq.ZContext; public class LoadBalancer { public static void main(String[] args) { try (ZContext context = new ZContext()) { // Create the load balancer (ROUTER socket) ZMQ.Socket lbSocket = context.createSocket(ZMQ.ROUTER); lbSocket.bind("tcp://*:3300"); // Buffer to hold client and worker messages ZMQ.Socket worker1Socket = context.createSocket(ZMQ.REP); worker1Socket.connect("tcp://localhost:3300"); ZMQ.Socket worker2Socket = context.createSocket(ZMQ.REP); worker2Socket.connect("tcp://localhost:3300"); while (true) { // Receive the identity of the client byte[] clientID = lbSocket.recv(0); // Receive the actual message from the client byte[] message = lbSocket.recv(0); System.out.println("Received message from client: " + new String(message)); // Forward the message to one of the workers (Worker 1 in this case) worker1Socket.send(message, 0); // Receive the reply from the worker byte[] reply = worker1Socket.recv(0); System.out.println("Worker 1 processed the message: " + new String(reply)); // Send the reply back to the client using the client identity lbSocket.send(clientID, ZMQ.SNDMORE); lbSocket.send(reply, 0); } } } }
Worker class
package com.zeromq.zeromq3; import org.zeromq.ZMQ; import org.zeromq.ZContext; public class Worker { public static void main(String[] args) { try (ZContext context = new ZContext()) { ZMQ.Socket workerSocket = context.createSocket(ZMQ.REP); workerSocket.connect("tcp://localhost:3300"); while (true) { // Receive the message from the load balancer byte[] message = workerSocket.recv(0); System.out.println("Worker received message: " + new String(message)); // Simulate processing and send a reply back String reply = "Processed: " + new String(message); workerSocket.send(reply.getBytes(), 0); } } } }
Client class
package com.zeromq.zeromq3; import org.zeromq.ZMQ; import org.zeromq.ZContext; public class Client { public static void main(String[] args) { try (ZContext context = new ZContext()) { ZMQ.Socket clientSocket = context.createSocket(ZMQ.REQ); clientSocket.connect("tcp://localhost:3300"); // Send a message to the load balancer String request = "Hello"; System.out.println("Client sending message: " + request); clientSocket.send(request.getBytes(), 0); // Receive the reply from the load balancer byte[] reply = clientSocket.recv(0); System.out.println("Client received reply: " + new String(reply)); } } }
Following is the output of the above code −
Client sending message: Hello
Explanation
Let us understand the working of the above program. Here we have three classes Load Balancing, Worker and, Client.
Load Balancing (ROUTER):
- The load balancer accepts requests from the client through the ROUTER socket.
- It forward the request to an worker.
- Once the worker processes the message and sends a reply, the load balancer forward that reply back to the client, storing the client's identity.
Worker (REP):
- Workers use the REP socket to listen to operations.
- Once the task is received, it is processed (in this case just appending "Processed:") and the response is sent back to the load balancer.
Client (REQ):
- The client sends the message using the REQ socket.
- It then waits for a response from the load balancer.
ZeroMQ Balancing Patterns
Pattern | Type | Load Balancing | Use Cases |
---|---|---|---|
REQ-REP | Synchronous | Round-Robin, tightly-Coupled | Simple client server system |
PUSH-PULL | Asynchronous | Worker based, as worker pull task as available | Parallel task distribution and processing |
ROUTER-DEALER | Asynchronous | Custom load balancing, more complex control | Complex distributed system need dynamic routing |