PUB/SUB pattern in ZeroMQ not working
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
The PUB/SUB (Publish-Subscribe) pattern in ZeroMQ is an essential messaging paradigm used for building distributed systems where publishers send messages without regard to whether any subscribers are actually listening. Subscribers, on the other hand, listen for messages that match their subscriptions without regard to who is publishing them. Although powerful, this pattern sometimes encounters difficulties due to ZeroMQ's inherent complexity and environmental factors.
Understanding PUB/SUB In ZeroMQ
ZeroMQ is a high-performance asynchronous messaging library, aimed at use in scalable distributed or concurrent applications. It provides several message patterns, among which PUB/SUB is particularly useful for scenarios requiring high scalability and topic-based filtering.
In the ZeroMQ PUB/SUB model:
- Publishers send messages tagged with a topic to a socket of type
ZMQ_PUB. - Subscribers set up filters on a socket of type
ZMQ_SUBto receive only the messages that contain topics they are interested in.
Common Issues with PUB/SUB Pattern
- Loss of Messages at Connection Time
- When a subscriber connects to a publisher, there can be a short time window during which messages published are lost because the subscription is not yet fully established. This is especially problematic in systems where message delivery guarantees are crucial.
- Topic Filtering Done at Subscriber Side
- While ZeroMQ handles subscriber's topic filtering, it is done at the subscriber side. If the network bandwidth is low and the publications are high-volume, this can lead to unnecessary data transfer over the network, impacting performance.
- Lack of Built-In Durability or Persistence
- ZeroMQ inherently does not store messages, and if a subscriber is offline, messages are lost. Systems requiring durability must implement custom solutions such as message queuing or logging on the publisher side.
- High-Water Mark Limitations
- ZeroMQ sockets use a high-water mark (HWM) setting that governs the maximum number of messages that can be held in the socket’s queue. Once this limit is reached, further messages are dropped. Publishers might continue sending messages that are never processed, leading to message loss.
Example Scenario: Subscriber Misses Messages
Let’s consider an example where a subscriber misses the initial messages from a publisher due to the time it takes to set up the subscription:
In this code, if the sleep times are not properly managed, the subscriber might miss the first "TOPIC1 Message" due to the delay in starting the message receipt loop after establishing the subscription.
Solutions and Best Practices
- Use Confluent or Reliable Patterns on Top of PUB/SUB: Implement custom patterns that buffer messages at the publisher end or acknowledge receipt at the subscriber end.
- Adjust High-Water Marks and Buffer Sizes: Configure these settings thoughtfully to prevent message dropping while considering system memory constraints.
- Grace Period on Setup: Introduce a delay after establishing a connection but before starting the message transmission to ensure that subscriptions are fully active.
Summary Table
| Issue | Description | Potential Solutions |
| Lost Messages at Startup | Messages sent before the subscriber is fully ready are lost. | Implement a handshake or delay message sending. |
| Inefficient Topic Filtering | Filtering on the subscriber can lead to unnecessary data transfer. | Optimize network usage and redesign message distribution. |
| Lack of Durability/Persistence | Messages are lost when a subscriber is offline. | Implement message queuing or storage mechanisms. |
| High-Water Mark Issues | Messages are dropped when the queue reaches its size limit. | Adjust the high-water mark or buffer sizes appropriately. |
By understanding and addressing these challenges, developers can optimize the use of the PUB/SUB pattern in ZeroMQ to build more reliable and efficient distributed systems.

