In order to learn the library, I've been translating the examples from the ZeroMQ guide to NetMQ. I've gone through a third of the book, and the main sticking point that I have with the library is the way it deviates from ZeroMQ in how it does polling.
At first glance, NetMQ seems better as you can just add callbacks to the sockets and call the poller.Run
, but the reality is that the NetMQ approach is significantly less expressive ZeroMQ one and I outright cannot translate the load balancing pattern to NetMQ directly.
Yes, there are alternatives: here it alternates between polling the frontend and the backend 0.5s each. Here is a straightforward implementation which does what the book does without the intermediate queue.
In addition to the above, I tried several schemes such as using two queues (the additional one for the requests), or adding and removing frontend sockets, and all of them have issues. Having two queues fails in the simple pirate example as it leads to stale requests being in the queues and I can't just remove them if the other side times out. Adding and removing sockets fails because callbacks for a socket can be called even after it is removed from the poller leading to dequeue from empty queue exceptions.
To my mind none of this is good enough, and as a matter of principle I should be doing it how the book does it. The approach closest to the one in the book which does not use any queues would be useless if I wanted to use a load balancing approach which is not LRU or needed to poll from multiple backends in the router.
I see all this poller hassle as a red mark against the NetMQ library. Are there any better approaches to the load balancing pattern that I am not aware of?