ZeroMQ Which socket types for arbitrary communication between exactly 2 peers?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
For exactly two ZeroMQ peers, the "right" socket type depends on what "arbitrary communication" really means. If you want simple bidirectional one-to-one messaging with no strict send-then-receive pattern, PAIR is the direct 1:1 socket type. If you need a more robust, extensible protocol over TCP, many developers prefer DEALER-based designs instead of relying on PAIR everywhere.
PAIR is the literal one-to-one socket
ZeroMQ provides PAIR specifically for exclusive communication between exactly two endpoints. It is symmetric: both sides can send and receive.
Example:
Client side:
For a tightly controlled exactly-two-peer setup, this is the most direct answer.
When PAIR is not the best long-term choice
PAIR is simple, but that simplicity also means it is narrow:
- exactly one peer per side
- no routing features
- no built-in scaling path to more peers
- less flexibility if the protocol later becomes asymmetric
If you know the design will remain a strict one-to-one conversation, PAIR is fine. If the protocol may evolve, or if you want more control over asynchronous request flow, DEALER-style sockets are often a better architectural base.
REQ/REP is only for strict request-response
Some people reach for REQ and REP because there are two peers. That is only correct if your traffic is strictly alternating:
- request
- response
- request
- response
If either side needs to send unsolicited messages, heartbeats, or multiple messages in a row, REQ/REP becomes awkward because it enforces a lockstep pattern.
So for "arbitrary" bidirectional exchange, REQ/REP is usually too rigid.
DEALER is a more flexible async option
If you want asynchronous send and receive behavior without the strict REQ/REP lockstep, DEALER is often the more flexible choice:
A DEALER-based design usually requires you to define your own application protocol more explicitly, but that extra work often pays off in flexibility.
For exactly two peers, DEALER on both sides can work well when you want symmetric asynchronous messaging and may later extend the system.
Choose by protocol shape, not by peer count alone
A good rule is:
- exactly two peers, simple symmetric messaging:
PAIR - strict alternating request-response:
REQ/REP - asynchronous custom protocol with future flexibility:
DEALERand related patterns
ZeroMQ socket type is really a protocol choice. The number of peers matters, but the message flow pattern matters more.
Common Pitfalls
The biggest mistake is choosing REQ/REP just because there are two peers. If the conversation is not strict request-response, that pattern quickly becomes frustrating.
Another mistake is using PAIR and later expecting it to scale or route messages like a more flexible socket type. PAIR is intentionally narrow.
Developers also forget that ZeroMQ socket types encode communication semantics. Picking the wrong type often means the application protocol is fighting the socket's built-in behavior.
Finally, do not describe the requirement as "arbitrary communication" without being more precise. Whether either side may initiate, burst, or pipeline messages changes the correct answer.
Summary
- '
PAIRis the direct ZeroMQ socket type for exactly two peers with symmetric communication.' - '
REQ/REPis only correct when the protocol is strict request-response.' - '
DEALERis a good choice when you want more asynchronous flexibility.' - The right socket type depends more on message flow than on peer count alone.
- In ZeroMQ, socket types are protocol design decisions, not just connection wrappers.
Related reading
- ZeroMQ with NORM - address already in use error was thrown on 2nd .bind() - why?
- ZIP file content type for HTTP request
- zmq hangs in zmq_proxy() during the cleanup
- 401 return from an API Gateway Custom Authorizer is missing 'Access-Control-Allow-Origin' header
- How do I find out which process is listening on a TCP or UDP port on Windows?
- Kafka: Consumer API vs Streams API
- What exactly is RESTful programming?
- A3C in Tensorflow - Should I use threading or the distributed Tensorflow API

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.