The Load-Balancing Pattern

Let’s now look at some code. We’ll see how to connect a ROUTER socket to a REQ socket, and then to a DEALER socket. These two examples follow the same logic, which is a load-balancing pattern. This pattern is our first exposure to using the ROUTER socket for deliberate routing, rather than it simply acting as a reply channel.

The load-balancing pattern is very common and we’ll see it several times in this book. It solves the main problem with simple round-robin routing (as PUSH and DEALER offer), which is that round robin becomes inefficient if tasks do not all take roughly the same time.

It’s the post office analogy: if you have one queue per counter, and you have some people buying stamps (a fast, simple transaction), and some people opening new accounts (a very slow transaction), you will find stamp buyers getting unfairly stuck in queues. And just as in a post office, if your messaging architecture is unfair, people will get annoyed.

The solution in the post office is to create a single queue so that even if one or two counters get stuck with slow work, other counters will continue to serve clients on a first-come, first-serve basis.

One reason PUSH and DEALER use this simplistic approach is sheer performance. If you arrive in any major US airport, you’ll find long queues of people waiting at immigration. The border patrol officials will send people in advance to queue up at each counter, rather than using a single queue. Having people walk 50 ...

Get ZeroMQ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.