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.
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:
- Topic: A prefix that subscribers can filter on.
- 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:
Example: Basic ZeroMQ Pub-Sub
Here’s a simple example in Python demonstrating the publisher and subscriber setup.
Publisher Code:
Subscriber Code:
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:
| Feature | Description |
| ZeroMQ Model | Brokerless, direct connections between nodes. |
| Communication | One-to-many (pub to subs). |
| Filtering | Subscribers can filter messages based on topics. |
| Scalability | High scalability, can handle a large number of subscribers. |
| Concurrent Execution | Possible 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
- ZeroMQ PUB/SUB topology on the same machine
- ZeroMQ vs Oracle queuing
- Zookeeper-Kafka and Consistent hashing
- ZooKeeper - clients co-ordination after one or more client lose connection with ZooKeeper
- ZooKeeper and Shared Nothing. Is it Scalable?
- Zookeeper (Curator framework) explicitly giving up the leaderLatch
- ZeroMQ Recommended pattern for inproc clients from multiple threads pushing ordered messages to a server?
- 1 thread vs 5 threads for distributed system communications?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.