ZeroMQ patterns

In this chapter, you will learn about the basic communication patterns that are supported by ZeroMQ. Do not worry if all that sound a bit theoretical; you will implement all of these patterns yourself throughout the chapter.

Request/reply pattern

The ZeroMQ library supports a variety of different communication patterns. For each of these, you will need different ZeroMQ socket types. The easiest communication pattern is the Request/reply pattern, in which a client opens an REQ socket and connects to a server listening on an REP socket. The client sends a request that is then replied to by the server.

Request/reply pattern

ZeroMQ Request/Reply sockets

It's important to know that REQ and REP sockets are always synchronous. Each REQ socket can be sending requests to a single REP socket at a time, and more importantly, each REP socket can also only be connected to a single REQ socket at a time. The ZeroMQ library even enforces this on the protocol level and triggers errors when an REQ socket tries to receive new requests before replying to the current one. There are advanced communication patterns to work around this limitation that we'll work with later.

Publish/subscribe pattern

The publish/subscribe pattern consists of a PUB socket on which messages can be published. To this socket, any number of SUB sockets can be connected. When a new message is published on a PUB socket, it will be forwarded to all connected SUB sockets.

Publish/subscribe pattern

Publish/subscribe sockets

Each subscriber in a PUB/SUB architecture needs to specify at least one subscription - a string that works as a filter for each message. Messages will be filtered by the publisher so that each subscriber only receives messages that they have subscribed to.

Publish/Subscribe works strictly in one direction. Publishers cannot receive messages from the subscribers, and subscribers cannot send messages back to the publishers. However, just as multiple SUB sockets can be connected to a single PUB socket, a single SUB socket can also be connected to multiple PUB sockets.

Push/pull pattern

The push/pull pattern works similar to the publish/subscribe pattern. A PUSH socket is used to publish messages to any number of PULL sockets (just like with PUB/SUB, a single PULL sockets can also be connected to any number of PUSH sockets). In contrast to publish/subscribe patterns, however, each message that is sent on a PUSH socket is dispatched to only one of the connected PULL sockets. This behavior makes the PUSH/PULL patterns are ideal to implement worker pools that you can, for example, use to distribute tasks to any number of workers to process in parallel. Similarly, a PULL socket may also be used to collect results from any number of PUSH sockets (which may in turn be results that are sent back from a worker pool).

Using a PUSH/PULL socket to distribute tasks to a worker pool and then using a second PUSH/PULL layer to collect results from that pool in a single socket is also called fan-out/fan-in.

Push/pull pattern

Using PUSH and PULL sockets to implement a fan-out/fan-in architecture

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.86.18