8

When I had two threads, I used PAIR socket type. But now I am using two processes that can be either on one machine or on different machines. I don't need requests and responses, I don't need sending to multiple nodes, etc. I need same thing that I had with PAIR (async, bidirectional), but with processes and with network. What socket types should I use?

Marko Kevac
  • 2,902
  • 30
  • 47

2 Answers2

3

Unfortunately, your world has gotten a bit more complicated. There's no direct analog to the PAIR/PAIR socket pairing in more widely distributed systems.

That said, if you keep roughly the same shape of the topology (two nodes connecting exclusively to each other and no other nodes) then you can pretty much achieve what you want using ROUTER/DEALER or even DEALER/DEALER (as you suggested in comments). Those sockets are sort of like REQ/REP, but they don't enforce a strict request/response communication pattern, they are entirely unrestricted, so in effect you get the same thing. The only problem comes if you intend to add more nodes, at which point you have to start managing things a little differently, in particular the DEALER socket doesn't allow you to choose which node you send to, it's strictly round robin.

But, doing that should get you what you're looking for (async, bidirectional).

The ROUTER socket type can require a little additional complexity since you need to keep track of the "identifier" of the other node to be able to send back to it (you can get this almost for free, especially in your case with just one peer, by using it directly out of the sent message). Because this is an exclusive pair, you can ignore the round-robin uncertainty introduced by the DEALER socket, and just go straight to DEALER/DEALER, which gives you an unrestricted message pattern and doesn't require any management of identities.

Jason
  • 13,606
  • 2
  • 29
  • 40
  • 1
    I was able to use DEALER/DEALER. Thank you. – Marko Kevac Jun 11 '14 at 08:35
  • I hadn't considered that pairing, but in an exclusive pair it would work great and is probably the best choice for minimal complexity. I'll edit my answer accordingly. – Jason Jun 11 '14 at 14:14
1

@Marko let me notice,

there is a principal separation between a ZMQ.SOCKET's (formal-communication-pattern) "type" and whatever a transport, one opts to .bind() / .connect() over

Once your architecture was happy (as you have written ) to work with PAIR/PAIR "session"

you may just without a single additional SLOC change the transport that is to be used

it works

Python 2.7.3 ...
>>> import zmq
>>> zmq.zmq_version()
'2.1.11'
>>> aZmqCONTEXT =  zmq.Context()                       # --<BoCTX>-- [SideA] Node
>>> aZmqSOCKET  = aZmqCONTEXT.socket( zmq.PAIR )       # here one decides about a type
>>> aZmqSOCKET.bind( "tcp://192.168.0.62:2027" )       # here is the transport // used to be ( "ipc://...")
>>> aZmqSOCKET.recv()                                  # here the PAIR waits for 1st MSG
'aMSG from the opposite PAIR/PAIR zmq-session Node arrived via TCP-transport ... QED'
>>> aZmqSOCKET.setsockopt( zmq.LINGER, 0 )             # pre-termination tidy-up
>>> aZmqSOCKET.close()
>>> aZmqCONTEXT.term()                                 # --<EoCTX>-- safe to clean-exit
>>> 
user3666197
  • 1
  • 6
  • 50
  • 92
  • It is clearly stated in ZeroMQ documentation that PAIR works for inproc only. I don't know why it's working in your case. https://www.dropbox.com/s/he8rkzds98am4p1/Screenshot%202014-06-14%2011.14.03.png – Marko Kevac Jun 14 '14 at 07:12
  • @MarkoKevac Thanks for feedback -- nevertheless, the text does say: "...designed for ... ://inproc" ( as the PAIR/PAIR abstract pattern yields it´s best on stack-less inter-process communication fields,) but **it does not** say any word about not being able, nor being that forbidden, to `.bind()` / `.connect()` over any other supported transport-class. Sure, spending [usec]-s on feeding TCP/IP-stack to just pass a buffered block of bytes to another <_localhost_> process is possible, however it wastes both time and resources, thus `://inproc` serves best of all here. – user3666197 Jun 14 '14 at 09:41
  • 1
    As a bit more general note, this experience makes it better visible, that the concept of Abstraction-rich Formal Communication Patterns is the most exciting and powerfull "Dark Mass" surprise "behind" ZeroMQ / nanomsg that is not always seen, understood & harnessed for achieving the best for the use of these grand tools. Worth read the book, taking just pictures of ideas from Figs. and only after that going down to design sketches and at very end, code the source. **Transport agnostic, fault resilient parallel load balancing simply does not get understood from separate lines of source code.** – user3666197 Jun 14 '14 at 09:46
  • PAIR sockets do not automatically reconnect, this can be problematic. "While ZMQ_PAIR sockets can be used over transports other than zmq_inproc(7), their inability to auto-reconnect coupled with the fact new incoming connections will be terminated while any previous connections (including ones in a closing state) exist makes them unsuitable for TCP in most cases.". – YouJiacheng Aug 10 '23 at 08:34