ZeroMQ - Handling slow receivers without dropping
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
ZeroMQ is a high-performance asynchronous messaging library, aimed at use in distributed or concurrent applications. It provides sockets (much like conventional sockets) that carry atomic messages across various transports. ZeroMQ sockets provide robust messaging solutions that ensure messages reach their destinations swiftly and reliably. However, one common issue in any message queuing system is handling slow receivers, which can potentially lead to message dropping if not managed correctly. ZeroMQ offers several mechanisms and patterns to effectively handle slow receivers without dropping messages.
Handling Slow Receivers in ZeroMQ
In ZeroMQ, the key to managing slow receivers revolves around high water mark settings, multipart messages, the ZMQ_CONFLATE option, and using different messaging patterns suitable for scenarios involving slow receivers. Below are the sophisticated strategies to handle slow receivers:
High Water Mark Settings
In ZeroMQ, the High Water Mark (HWM) is a setting that controls the maximum number of messages that can be buffered on a socket. When this limit is reached, the socket starts to block or drop messages depending on its type (PUB-SUB, PUSH-PULL, etc.) and configuration. Properly configuring the HWM can prevent a slow receiver from being overwhelmed by pausing the sender or carefully dropping messages when necessary.
Example: Setting HWM on a PUSH socket to limit the number of queued messages.
In the example above, the sender will stop sending more messages once it hits the limit of 10 messages, until the queue size decreases as the receiver processes messages.
Using the ZMQ_CONFLATE Option
The ZMQ_CONFLATE option keeps only the last message sent. This can be particularly useful for data that rapidly becomes obsolete, such as periodic updates of a stock price.
Example:
This setting is ideal for scenarios where only the most recent message is relevant, ensuring that slow subscribers won't have to process a backlog of outdated messages.
Multipart Messages
ZeroMQ provides the ability to send a message in multiple parts, where all parts are sent as a single atomic operation. If a receiver is slow, it won’t receive incomplete messages, since it either receives all parts of the message or none. This ensures data integrity without dropping parts of the messages.
Example:
Message Patterns
Choosing the right ZeroMQ pattern can also help manage slow receivers:
- PUSH/PULL: This pattern is suitable for distributing tasks among multiple workers where each task is handled once. A PUSH socket can drop messages if all PULL receivers are slow or disconnected.
- PUB/SUB: In this case, subscribers can miss messages if they can't keep up since PUB sockets do not wait for subscribers.
- ROUTER/DEALER: These can dynamically route messages to available receivers, ideal for load balancing to manage slow receivers.
Summary Table of Strategies
| Strategy | Description | Usage Scenario |
| High Water Mark (HWM) | Limits the queued messages to prevent overflow. | General use to prevent system overload. |
ZMQ_CONFLATE | Keeps only the last message sent. | When only the latest update is relevant (e.g., stock prices). |
| Multipart Messages | Sends messages atomically in multiple parts. | Ensuring data completeness in slow network conditions. |
| Appropriate Messaging Pattern | Utilizes specific ZeroMQ patterns based on need. | Tailored approach based on system architecture and workflow demands. |
Conclusion
Handling slow receivers in ZeroMQ requires a combination of configuring socket options and choosing the appropriate messaging patterns. By understanding the behavior and capabilities of different socket options like HWM and ZMQ_CONFLATE, and using multipart messages, developers can tailor their applications to efficiently handle slow receivers without losing data integrity and with minimal message dropping.
Related reading
- ZeroMQ / 0mq or nanomsg bindings to Kafka?
- ZeroMQ can subscribe, but how to exclude a specific filter?
- ZeroMQ Publish and Subscribe concurrently
- 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 + Kafka - Unable to create data directory

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.