[zeromq-dev] Incoming data on multiple sockets and fairness

gonzalo diethelm gdiethelm at dcv.cl
Tue Mar 9 15:19:19 CET 2010

This is yet another request for your insights...

I will have a process that may be receiving incoming data on multiple
sockets (probably a couple of Upstream sockets, at least one Sub
socket). I see two designs to handle this, each with different
implications on fairness; by "fairness" I am thinking about processing
incoming messages from all sockets in a way that none of them waits "too
long" to be processed.

1. Have many threads; each thread waits on a zmq_recv() call and, upon
receiving a message, processes it right away and goes back to calling
zmq_recv(). Of course this means I would have to use mutual exclusion
for processing the messages (supposing this processing will need to
touch shared structures). In this scenario, I would be trusting the
thread scheduling mechanism (however goo or bad) to ensure fairness.

2. Have a single thread doing a zmq_poll() on all sockets; upon being
signaled, process the messages on all signaled sockets. But in which
order? What if socket 1 has 100 pending messages and socket 2 has 3
pending messages? Which ones do I process first, and how do I know the
pending queue length for each socket? Related question: if a socket
receives 10 messages, causing zmq_poll() to wake up, and I only read 2
of those messages, will zmq_poll() wake up right away if I call it
again, or do I have to process all 10 messages before counting on this?
(I believe this is called "edge-triggering" vs. "level-triggering").

3. I could also have a hybrid: many threads, as in #1, but they simply
put all incoming messages on a single queue. Then have one thread (or
more) fetching elements off this queue and processing them. It adds more
latency and it forces me to use mutual exclusion for pushing elements on
the queue tail and reading them off the queue head, but looks "saner" to
me and I might even use the queue to handle retries.

Perhaps I am barking up the wrong tree here; would appreciate hearing
any advise anyone might have. Thanks in advance.

Gonzalo Diethelm 

Declaración de confidencialidad: Este Mensaje esta destinado para
el uso de la o las personas o entidades a quien ha sido dirigido y
puede contener información reservada y confidencial que no puede
ser divulgada, difundida, ni aprovechada en forma alguna. El uso no
autorizado de la información contenida en este correo podrá ser
sancionado de conformidad con la ley chilena. 
Si usted ha recibido este correo electrónico por error, le pedimos
eliminarlo junto con los archivos adjuntos y avisar inmediatamente
al remitente, respondiendo este mensaje. 

"Before printing this e-mail think if is really necesary".
Disclosure: This Message is to be used by the individual,
individuals or entities that it is addressed to and may include
private and confidential information that may not be disclosed,
made public nor used in any way at all. Unauthorized use of the
information in this electronic mail message may be subject to the
penalties set forth by Chilean law. 
If you have received this electronic mail message in error, we ask
you to destroy the message and its attached file(s) and to
immediately notify the sender by answering this message. 

More information about the zeromq-dev mailing list