0
votes

It's explicitly stated in the ZeroMQ guide that sockets must not be shared between threads. In case of multiple threaded producers who need to PUSH their output via zmq, I see two possible design patterns:

  • 0mq socket per producer thread
  • single 0mq socket in a separate thread

In the first case, each thread handles its own affairs. In the latter, you need a thread-safe queue to which all producers write and from which the 0mq thread reads and then sends.

What are the factors for choosing between these two patterns? What are the pros\cons of each?

2

2 Answers

1
votes

A lot depends on how many producers there are.

If there are only a handful, then having one socket per thread is manageable, and works well.

If there are many, then a producer-consumer queue with a single socket pushing (effectively being a consumer of the queue and singe producer for downstream sockets) is probably going to be faster. Having lots of sockets running is not without cost.

The main pro of the first case is that it is much more easily scaled out to separate processes for each producer, each one single-threaded with its own socket.

1
votes

I've asked a similiar question.

You can use a pool of worker threads, like this, where each worker has a dedicated 0mq socket via ThreadLocal, ensuring sockets are used and destroyed in the threads that created them

You can also use a pool of sockets, perhaps backed with an ArrayBlockingQueue, and just take/replace sockets whenever you need them. This approach is less safe than the dedicated socket approach because it shares socket objects (synchronously) amongst different threads; you should be ok since Java handles locking, but its not the 0mq recommended approach.

Hope it helps...