ZeroMQ - Message Framing



Understanding Messages in ZeroMQ

A Message in ZeroMQ is a discrete data unit that is passed between applications or components of the same application. From ZeroMQ's point of view, messages are considered opaque binary data.

They are blocks of data sent and received over the network using various message patterns supported by ZeroMQ. ZeroMQ provides several message patterns as follows −

  • PUB/SUB (Publish/Subscribe)
  • REQ/REP (Request/Reply)
  • PUSH/PULL
  • Router/Dealer

Message Framing

The ZeroMQ messaging library refers to the structure and organization of messages exchanged between applications or services. The messaging framework provides a way to construct, send, and receive messages, and ensuring that messages are properly formatted, routed, and processed by the receiving application.

Here is a simple diagram to demonstrates how ZeroMQ frames messages −

Message framing

Here,

  • Socket: The Socket handles communication over the network. It sends and receives messages and manages the framing internally.
  • Queue: It represents the internal mechanism that ZeroMQ uses to store and manage messages and their frames. It ensures that messages are processed correctly, even if split into multiple frames.
  • Message: It is a logical unit of data composed of one or more frames.
  • Frame: The individual data chunks that make up a message. Frames are sent and received in order, and reassembled into complete messages by ZeroMQ.

How Messages are Managed and Structured?

In ZeroMQ, the concepts of Envelope, Body, and Frames are important for understanding how messages are structured and managed:

  • Envelope
  • Body
  • Frames

Envelope

In ZeroMQ, the term envelope is often used in the context of the ROUTER socket. An envelope generally includes metadata such as routing information, which routes messages to the correct receiver. It is more commonly associated with ROUTER sockets, where the envelope consists of the message routing identity and other metadata.

Body

The body of the message in ZeroMQ refers to the actual data being sent. This is the payload of the message that the application processes. The body is the main content being transmitted between sockets.

Frames

The concept of frames is relevant when dealing with more complex socket types like STREAM and ROUTER. Messages can be split into multiple frames, where each frame is a chunk of data that together forms the complete message. This is useful for handling large messages or messages with multiple parts.

ZeroMQ itself does not define or implement specific types of message framing as some protocols do. Instead, ZeroMQ provides a flexible messaging system where the framing and serialization of messages are handled by the application. However, it provides several messaging patterns and socket types that implicitly handle message framing in different ways. They are −

Publish/Subscribe (PUB/SUB)

  • Publisher (PUB) - Sends messages to multiple subscribers.
  • Subscriber (SUB - Receives messages based on subscription filters.
  • Framing - ZeroMQ handles the framing internally, but messages are published as they are.

Request/Reply (REQ/REP)

  • Request (REQ) - Sends a request to a reply socket and waits for a response.
  • Reply (REP) - Receives requests and sends responses.
  • Framing - ZeroMQ frames messages as discrete entities, maintaining the request and response protocol.

DEALER and ROUTER

  • Dealer (DEALER) - Acts as a non-blocking requester, and able to handle multiple requests simultaneously.
  • Router (ROUTER) - Handles messages in a more complex way, routing them based on identity or other criteria.
  • Framing - Messages include routing metadata or identifiers, and ZeroMQ frames messages with this metadata for routing.

PUSH and PULL

  • Push (PUSH) - Sends messages to a pull socket, generally used for distributing tasks.
  • Pull (PULL) - Receives messages from a push socket.
  • Framing - Messages are distributed in a round-robin technique, with ZeroMQ handling the framing internally.

Delivery Guarantees

ZeroMQ does not provide a built-in guarantee of message delivery, and there is no automatic retry mechanism in ZeroMQ. If a message is sent and the receiver is not available or there is a network failure, the message may be lost.

Advertisements