[zeromq-dev] How to implement lossless pub-sub with ZeroMQ?

Antti Karanta Antti.Karanta at neromsoft.com
Mon Nov 30 15:34:54 CET 2015

3.11.2015, 13:10, Doron Somech kirjoitti:
> You can use malamute. Also take a look at the clone example from 
> ZeroMQ guide.
   Thanks for your answer.

   I had a look at Malamute and am not yet sure whether it fits my 
needs. I wrote some further questions about it to Pieter's response.

   I take it that by the clone example you mean this: 

> Anyway what I did in event driven system is to save the event to db 
> prior publishing. All events are part of specific stream and numbered. 
> When subscriber recognize a gap it goes directly to database to get 
> missing events. If in your case subscriber doesn't have access to 
> database you can have a service for that.

   If I understood correctly this is pretty much the same idea as in the 
clone pattern described in the guide.

   I could go this way, but it seems to be a bit complex to use pub-sub 
and then separately request for the missed messages via dealer-router. 
In my case the messages are derived from certain events and not at the 
moment stored anywhere. There is no need to persist them beyond them 
being received by the subscribed clients, so I would need to implement 
some in-memory / database storage for them that discards them after a 
timeout. They would need to be kept until the timeout (afaik there is no 
way to know which messages all the subscribers have received unless they 
send separate acknowledgements for them), increasing memory consumption 
and code complexity.

   Wouldn't it be more straightforward to implement "custom pub-sub" on 
top of dealer-router something like this:

   * a client subscribes to a given topic by sending a message using 
dealer socket to the server's router socket. The message is essentially 
the topic id. Client identity is sent automatically as the first message 
frame by the dealer socket.
   * the router does not respond to this request directly but saves the 
client id to a list of client ids subscribed to the topic
   * when the server wants to broadcast a message, it loops through all 
the client ids registered to the given topic and sends the message to 
them using the router socket
   * if a client crashes / otherwise disappears, the messages going to 
it are kept in zmq's buffers till their timeout is reached


   * need only use one socket type pair (dealer-router) instead of two 
(pub-sub + dealer-router)
   * no need to save / manage outgoing messages (so the client can 
request the ones it missed) anywhere as they would be kept in the 
router's buffers until their timeout expired. In my specific case the 
message streams do not need to survive a server crash. Also, it is 
acceptable to restart a client that is for some reason unable to receive 
messages for a lengthy period of time.


   * this might be much less performant than using pub-sub sockets?
   * greater memory consumption than pub-sub as the messages are copied 
separately to every subscribed client's zmq outbound buffers (as from 
the router socket's point of view they are separate messages - this is 
because router identifies the client to send the reply to by data in the 
first message frame, i.e. the client identity)
   * related to the previous: if there are a lot of clients that crash / 
are suddenly unreachable the same message is kept in memory in zeromq's 
buffers for each of these until it expires

   Does this sound like a sensible approach? Any caveats I missed?


More information about the zeromq-dev mailing list