ZeroMQ
Publish-Subscribe Pattern
Concurrency
Message Queuing
Distributed Systems

ZeroMQ Publish and Subscribe concurrently

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

ZeroMQ, also known as 0MQ, ZMQ, or Zero Message Queue, is a high-performance asynchronous messaging library aimed at use in distributed or concurrent applications. It provides a messaging queue, but without the need for a dedicated message broker. One of the common patterns used in ZeroMQ for message distribution is the Publish-Subscribe (Pub-Sub) pattern.

Understanding ZeroMQ's Pub-Sub Pattern

The Pub-Sub pattern in ZeroMQ is designed for one-to-many communication where a publisher sends messages to multiple subscribers. Each subscriber may receive all messages or only a subset, typically filtered by topics.

  • Publisher: Sends messages to multiple subscribers. Each message can have a topic, and the publisher can send many different topics.
  • Subscriber: Receives messages from the publisher. A subscriber subscribes to specific topics.

In ZeroMQ, each message in a Pub-Sub connection has two parts:

  1. Topic: A prefix that subscribers can filter on.
  2. Content: The actual message data.

Setting Up ZeroMQ

To implement a Pub-Sub message system using ZeroMQ, first, install the necessary ZeroMQ library in your environment. For Python, it is typically installed using pip:

bash
pip install pyzmq

Example: Basic ZeroMQ Pub-Sub

Here’s a simple example in Python demonstrating the publisher and subscriber setup.

Publisher Code:

python
1import zmq
2import time
3
4context = zmq.Context()
5socket = context.socket(zmq.PUB)
6socket.bind("tcp://*:5556")
7
8while True:
9    # Publishing messages with different topics
10    socket.send_string("Topic1 Message from Publisher")
11    socket.send_string("Topic2 Another Message from Publisher")
12    time.sleep(1)

Subscriber Code:

python
1import zmq
2
3context = zmq.Context()
4socket = context.socket(zmq.SUB)
5socket.connect("tcp://localhost:5556")
6
7# Subscribe to 'Topic1'
8socket.setsockopt_string(zmq.SUBSCRIBE, 'Topic1')
9
10while True:
11    message = socket.recv_string()
12    print(f"Received: {message}")

In this example, the publisher sends out messages under Topic1 and Topic2. However, the subscriber only receives messages that begin with Topic1.

Running Publisher and Subscriber Concurrently

To realistically implement and run the Pub-Sub model, you often need the publisher and subscribers to operate concurrently. This can be achieved in several ways:

  • Using Threads: Run each component in separate threads.
  • Using Processes: Use multiprocessing to run each on different cores.
  • Distributed Systems: Run publishers and subscribers on different machines.

In Python, the threading or multiprocessing module can be used to handle concurrency.

Key Considerations

Here’s a summary of key points when implementing ZeroMQ Pub-Sub:

FeatureDescription
ZeroMQ ModelBrokerless, direct connections between nodes.
CommunicationOne-to-many (pub to subs).
FilteringSubscribers can filter messages based on topics.
ScalabilityHigh scalability, can handle a large number of subscribers.
Concurrent ExecutionPossible through threads, processes, or distributed systems.

Advanced Topics

  • High Water Mark (HWM): Controls how messages are queued in ZeroMQ to handle slow subscribers.
  • Message Reliability: Message delivery is not guaranteed by default. Use patterns like conflation or acknowledgments to enhance reliability.
  • Security: ZeroMQ supports encryption via CURVE (based on the ZMQ_CURVE mechanism), enabling secure connections between clients and servers.

Conclusion

ZeroMQ's Pub-Sub is a powerful pattern for building distributed and scalable applications. By understanding and utilizing this pattern effectively, developers can leverage ZeroMQ's performance benefits without the complexity and overhead of traditional message queuing or enterprise messaging systems.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.