ZeroMQ and TCP Retransmits on Linux
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
ZeroMQ (ZMQ) is a high-performance asynchronous messaging library, aimed to be used in distributed or concurrent applications. It provides a messaging queue, but unlike message-oriented middleware tools which require a message broker, ZeroMQ operates brokerlessly. It is designed to connect your applications across various platforms and languages with high throughput and low latency. TCP (Transmission Control Protocol) is one of the transport protocols supported by ZeroMQ for reliable byte stream communication, making it a favorite for many distributed applications that require guaranteed message delivery.
Understanding ZeroMQ
ZeroMQ handles messaging by establishing "sockets" (similar to the concept but not identical to TCP sockets) which can represent various patterns such as pub/sub, request/reply, and push/pull. These patterns abstract the complexities of managing the underlying networking protocols. It's like having a conversation where ZeroMQ is handling the nuances of the language (the protocol) while you just speak (send messages).
One of ZeroMQ's powerful features is its ability to use TCP effectively for message transport. TCP, being a connection-oriented protocol, ensures the reliable delivery of streams of bytes across the network. However, the reliability comes with complexity, particularly concerning message management in the face of network issues, latency, or unresponsive peers.
TCP Retransmits in Linux
TCP ensures the reliable delivery of data using acknowledgments (ACKs) and retransmissions. When a segment of data is sent, the sender starts a timer waiting for an acknowledgment from the receiver. If the ACK is not received within a timeout period, the sender retransmits the segment.
In Linux, TCP retransmissions are handled by the kernel. The TCP stack of the Linux kernel monitors all TCP connections, and the retransmit timer checks regularly if any segments need to be resent. This process involves several system parameters and algorithms like RTO (Retransmission Timeout), which can dynamically change based on network conditions.
ZeroMQ and TCP Retransmits: A Deep Dive
When using ZeroMQ over TCP, several scenarios might necessitate TCP retransmits:
- Network Congestion: Packets may be dropped due to buffer overflows in network equipment or high traffic, leading to retransmissions.
- Unstable Network Connections: In mobile networks or satellite communications, frequent disruptions can cause packets to be lost, triggering retransmits.
- Remote Server Issues: If the receiving server is overwhelmed or slow to process messages, it might delay ACKs, resulting in retransmissions.
ZeroMQ applications must be designed considering these scenarios, especially in a distributed setup where message delivery is critical. Applying best practices such as handling message acknowledgments at the application level or implementing heartbeats can enhance reliability and performance.
Example: Handling TCP Retransmits in ZeroMQ
Consider an application where ZeroMQ is used to send critical updates between servers. Here's a simplistic code snippet using Python's pyzmq library:
This example attempts to send a JSON message. Using zmq.DONTWAIT, it instructs the socket not to block if the message cannot be sent immediately. Transmission errors can be caught and handled appropriately, potentially logging or queuing the message for a retry.
Key Points Summary
| Feature | Description | Relevance to ZeroMQ |
| TCP Connection | reliable stream of bytes | Ensures message delivery |
| TCP Retransmits | automatic re-sending of lost data | Increases reliability under network issues |
| ZeroMQ Patterns | pre-defined topologies for message flows | Simplifies complex network interactions |
| Socket (ZeroMQ) | communication endpoint | Abstraction over raw TCP, easing development |
Enhancing Reliability with TCP and ZeroMQ
The combination of ZeroMQ's messaging patterns and TCP's reliability (with mechanisms like retransmits) provides a robust foundation for developing distributed apps. However, understanding and planning for network behavior and potential issues like latency, packet loss, and TCP retransmits are crucial.
By thoroughly monitoring and tuning both the ZeroMQ configurations and the Linux TCP settings, applications can achieve optimal performance and reliability, ensuring that every message not only reaches its destination but does so efficiently.
Related reading
- ZeroMQ DEALER doesnt receive response from ROUTER in DEALER/ROUTER configuration
- ZeroMQ How to handle non-message-related, asynchronous events in a ZeroMQ node?
- ZeroMQ permanent PULL socket
- ZeroMQ PUB/SUB topology on the same machine
- ZeroMQ round-robin fail-over on disconnected peers
- ZeroMQ Which socket types for arbitrary communication between exactly 2 peers?
- ZeroMQ with NORM - address already in use error was thrown on 2nd .bind() - why?
- ZIP file content type for HTTP request

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.