ZeroMq

From HerzbubeWiki
Jump to navigation Jump to search

This page is about ZeroMQ, the broker-less asynchronous messaging library. Zero stands for a number of things, foremost possibly for "no message broker", but in general for simplicity and minimalism that is intended to result in a fast and easy messaging solution.

References


Libraries

  • The ZeroMQ low-level library is known as "libzmq".
  • A number of language bindings exist for the low-level library. The ZeroMQ documentation entry point lets you choose the language and then shows you the known bindings that are available for that language. Examples:
    • C#/.NET: clrzmq4 is a C#/.NET wrapper around the native libzmq. NuGet package includes native shared libraries for libzmq.
    • Python: Pyzmq.
    • Node.js: zeromqjs
    • C++: At the time of writing has 5 libraries with varying styles to offer. zmqpp is labeled as "high-level binding", which seems to be the most familiar style to me.
  • In some cases alternative implementations of ZeroMQ exist for a certain platform. Examples:
    • C#/.NET: NetMQ. This is notable because it does not support communication via IPC, i.e. via underlying UNIX Domain Sockets.
    • Java: JeroMQ


Socket API

Fundamental for understanding ZeroMQ is the concept of a ZeroMQ socket. Some general notes:

  • The ZeroMQ API is based on the concept of ZeroMQ sockets.
  • ZeroMQ sockets are abstracting away the underlying transport protocol / communication.
  • Where conventional sockets transfer streams of bytes or discrete datagrams, ZeroMQ sockets transfer discrete messages.
  • ZeroMQ organizes by itself the timings of the physical connection setup and tear down, reconnect and effective delivery.
  • Some ZeroMQ sockets allow many-to-many relationships. Conventional sockets only support one-to-one, many-to-one or one-to-many.
  • ZeroMQ uses different socket types to implement different messaging patterns. For instance, if you decide to implement the publish/subscribe pattern you choose the ZeroMQ socket types PUB and SUB (or XPUB/XSUB), and these have properties and behaviour that intrinsically support the publish/subscribe pattern.


Lifecycle

The lifecycle of a ZeroMQ socket can be broken down into the following phases:

  • Create/destroy the socket.
  • Configure the socket.
  • Create ZeroMQ connections to/from the socket.
  • Write/receive messages to/from the socket.


Bind vs Connect

ZeroMQ peers can use either the "bind" method or the "connect" method to attach to a ZeroMQ socket.

  • Bind: The peer attaches to the socket, but ZeroMQ creates no message queue.
  • Connect: ZeroMQ creates a queue for each peer that attaches to the socket with this method (with some exceptions).

When a peer sends a message to a socket that does not have any queues (because no client attached to the socket with "connect"), the message is lost because there is no queue to store the message.


High-water mark

The so-called "high-water mark" is the limit on the maximum number of outstanding messages that can be queued in memory for a single peer.

When the limit is reached, the ENTIRE SOCKET is entering an exceptional state and ZeroMQ will undertake appropriate action (e.g. blocking, dropping messages) in an attempt to return to the normal state. Which action which depends on the socket type.


Socket types

Request/reply

Multiple clients send request messages to multiple servers.

Socket types:

  • Synchronous: REQ and REP socket types
  • Asynchronous: DEALER and ROUTER socket types


Publish/subscribe

Multiple publishers send messages to multiple subscribers.

General notes:

  • A message being sent has a topic and a payload
  • Subscribers say which topics they are interested in. The subscriber topic is compared to message topics using a prefix check


Socket types:

  • PUB: Can only send, not receive.
  • SUB: Can only receive, not send.
  • XPUB: Publisher can receive subscription messages from subscribers, i.e. a message when a subscriber subscribes and one when it unsubscribes.
  • XSUB: Subscriber sends a subscription message when it subscribers or unsubscribes.


Pipeline

Socket types:

  • PUSH: Can only send, not receive.
  • PULL: Can only receive, not send.


Exclusive pair

Connects two sockets exclusively. This pattern is intended for communication between two threads. It is not clear whether these threads have to live in the same process, or can be in different processes.

Socket types:

  • PAIR: Not clear how this socket type works.


Client/server

At the time of writing, the socket types for this pattern are implemented as draft.

Connects one server to one or more clients.

Socket types:

  • CLIENT: No further info.
  • SERVER: No further info.


Radio/dish

At the time of writing, the socket types for this pattern are implemented as draft.

One publisher sends messages to multiple subscribers.

Socket types:

  • RADIO: No further info.
  • DISH: No further info.