A Load-Balancing Message Broker
The previous example is half-complete. It can manage a set of workers with dummy requests and replies, but it has no way to talk to clients.
If we add a second frontend ROUTER socket that accepts client requests, and turn our example into a proxy that can switch messages from frontend to backend, we get a useful and reusable tiny load-balancing message broker (Figure 3-7).

Figure 3-7. Load-balancing broker
This broker does the following:
Accepts connections from a set of clients
Accepts connections from a set of workers
Accepts requests from clients and holds these in a single queue
Sends these requests to workers using the load-balancing pattern
Receives replies back from workers
Sends these replies back to the original requesting client
The broker code (listed in Example 3-5) is fairly long, but worth understanding.
Example 3-5. Load-balancing broker (lbbroker.c)
//// Load-balancing broker// Clients and workers are shown here in-process//#include "zhelpers.h"#include <pthread.h>#define NBR_CLIENTS 10#define NBR_WORKERS 3// Dequeue operation for queue implemented as array of anything#define DEQUEUE(q) memmove (&(q)[0], &(q)[1], sizeof (q) - sizeof (q [0]))// Basic request-reply client using REQ socket.// Since s_send and s_recv can't handle 0MQ binary identities we// set a printable text identity to allow routing.//staticvoid*client_task(void ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access