ZeroMQ - Transport Protocols



The transport protocol in ZeroMQ is known as the ZeroMQ Message Transport Protocol (ZMTP). It is a transport layer protocol for exchanging messages between two hosts over a connected transport layer such as Transmission Control Protocol (TCP).

TCP is the main protocol in the Internet protocol suite, often referred to as TCP/IP. TCP allows us to use two hosts or devices to connect and exchange data streams.
transport protocol

Here are some of the key features of ZMTP:

  • Security: ZMTP supports extensible security mechanisms. Peers agree on the security mechanism and version by exchanging data and persisting or closing the connection.
  • Backward Interoperability: ZMTP defines rules for backward interoperability.
  • Command and Messaging Framing: ZMTP defines rules for command and message framing.
  • Connection Metadata: ZMTP defines rules for connection metadata.

ZeroMQ supports several transport protocols that manage the exchange of messages between endpoints. Here are the primary transport protocols used in ZeroMQ:

TCP (Transmission Control Protocol)

It is the most reliable connection-oriented protocol that ensures that all messages are delivered in the correct order without duplication. This is typically used when processes are communicating over a network, whether on the same machine or on different machines.

Bind Example (Server):

// Bind to all available network interfaces on port 5555
socket.bind("tcp://*:5555")

Connect Example (Client):

// Connect to a remote server on the specified IP and port
socket.connect("tcp://192.168.0.1:5555")

Advantages

Following are the advantages of using TCP in ZeroMQ −

  • It is widely supported across all platforms and devices.
  • It has reliable message delivery.
  • It ensures that messages are received in order.

Disadvantages

Following are the disadvantages of using TCP in ZeroMQ −

  • It has higher latency compared to other protocols like Inter-Process Communication (IPC).
  • It also has more overhead from error checking, flow control, and acknowledgement packets.

IPC (Inter Process-Communication)

IPC allows communication between different processes on the same machine. It is better than TCP because it avoids network stack overhead by operating directly at the operating system level.

Bind Example (Server):

// Bind to a local file-based endpoint
socket.bind("ipc:///tmp/zeromq-ipc")

Connect Example (Client):

// Connect to a local endpoint on the same machine
socket.connect("ipc:///tmp/zeromq-ipc")

Advantages

Following are the advantages of using IPC in ZeroMQ −

  • It is much faster than network-based protocols like TCP.
  • There is no network latency since it works on the same machine.
  • It is ideal for communication between different processes in the same application or microservices on the same server.

Disadvantages

Following are the disadvantages of using IPC in ZeroMQ −

  • It is limited to communication between processes on the same machine.
  • It cannot be used for communication between different machines

Inproc (In-Process)

Inproc is used for communication between threads within the same process. It is the fastest ZeroMQ transport because it avoids interprocess communication, relying only on memory transfers within the same application.

Bind Example (Thread 1):

// Bind to an in-process endpoint
socket.bind("inproc://some-endpoint")

Connect Example (Thread 2):

// Connect to the in-process endpoint from another thread
socket.connect("inproc://some-endpoint")

Advantages

Following are the advantages of using Inproc in ZeroMQ −

  • It is the fastest transport as it does not involve any networking or process limitations.
  • It supports communication between threads within the same application.

Disadvantages

Following are the disadvantages of using Inproc in ZeroMQ −

  • It is not suitable for communication between different processes or across a network.
  • It is limited to inter-thread communication within a single process.

PGM/EPGM

The PGM/EPGM (Pragmatic General Multicast/Encapsulated Pragmatic General Multicast) is used for applications that require reliable multicast delivery, such as real-time data delivery or live streaming. These protocols enable multicast communications, where a sender sends messages to multiple receivers simultaneously.

  • PGM: This is native multicast and requires network layer support. It is not available on all network infrastructures.

  • EPGM: ZeroMQ wraps PGM over UDP (encapsulation), allowing multicast to work on networks that do not directly support PGM.

Bind Example (Server):

// Bind to a multicast address and port
socket.bind("epgm://eth0;239.192.1.1:5555")

Connect Example (Client):

// Connect to the multi-cast group
socket.connect("epgm://239.192.1.1:5555")

Advantages

Following are the advantages of using PGM/EPGM in ZeroMQ −

  • It is efficient for scenarios where one message needs to reach multiple clients (e.g., stock ticker updates, live sports scores).
  • It minimizes network bandwidth by sending one message to multiple subscribers.

Disadvantages

Following are the disadvantages of using PGM/EPGM in ZeroMQ −

  • It is more complex than unicast-based protocols like TCP.
  • PGM is not widely supported and may require special network configuration.

UDP (User Datagram Protocol)

UDP is a connectionless and unreliable protocol that can be faster than TCP but does not guarantee message delivery or ordering. While, ZeroMQ does not directly support raw UDP as a transport (unlike TCP), it can be used indirectly via PGM or other protocols.

TIPC (transparent Inter Process-Communication)

TIPC is a transport protocol designed for efficient communication between nodes in a cluster. It is present in the Linux operating system and is optimized for high-speed inter-process communication within a cluster of machines.

Bind Example (Server):

// Bind to a TIPC service and instance
socket.bind("tipc://{service,instance}")

Connect Example (Client):

// Connect to a TIPC service on the cluster
socket.connect("tipc://{service,instance}")

Advantages

UDP is efficient for communication and high-speed transport design in cluster environments, especially for Linux clusters.

Disadvantages

Following are the disadvantages of using UDP in ZeroMQ −

  • It is limited to Linux environments with TIPC support.
  • It may require additional network setup to be used effectively.

In summary, each protocol has its strengths and weaknesses, and your choice will depend on the architecture of your system, performance requirements, and the environment in which your applications process.

Advertisements