Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What ZeroMQ socket type to use for inter process communication?

Tags:

c

zeromq

ipc

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?

like image 537
Marko Kevac Avatar asked Jun 09 '14 14:06

Marko Kevac


2 Answers

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.

like image 192
Jason Avatar answered Sep 21 '22 00:09

Jason


@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
>>> 
like image 22
user3666197 Avatar answered Sep 18 '22 00:09

user3666197