Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zeromq DEALER client to multiple servers (ROUTER)

Tags:

zeromq

I am using ZEROMQ for distributed messaging application. Need to connect client (DEALER socket) to multiple servers (ROUTER socket on server side). What are my options on CLIENT side ?

  1. Create DEALER socket on client side for each server endpoint (ROUTER socket).
  2. Create only ONE DEALER socket on client side and add multiple endpoints.

I tried option 2 - connecting to multiple endpoints but message always goes to the first connected endpoint. followed following steps:

  • create DEALER socket
  • connect to first endpoint
  • then at run time, add another endpoint to the socket by using socket.connect(endpoint). Do I need to reconnect?

In DEALER socket, there is no option to send message on a particular endpoint in case it is connected to multiple endpoints.

Any idea?

like image 257
ashish nagar Avatar asked Sep 28 '13 18:09

ashish nagar


People also ask

Is ZeroMQ asynchronous?

ZeroMQ is an asynchronous network messaging library known for its high performance.

Does ZeroMQ use sockets?

ZeroMQ patterns are implemented by pairs of sockets with matching types. The built-in core ZeroMQ patterns are: Request-reply, which connects a set of clients to a set of services. This is a remote procedure call and task distribution pattern.

What is Zmq context?

Contexts help manage any sockets that are created as well as the number of threads ZeroMQ uses behind the scenes. Create one when you initialize a process and destroy it as the process is terminated. Contexts can be shared between threads and, in fact, are the only ZeroMQ objects that can safely do this.


1 Answers

ZeroMQ encodes certain behaviors into socket types. These mainly deal with:

  1. handling multiple peers
  2. handling undeliverable messages
  3. handling excessive throughput (HWM)

A DEALER socket is one that can connect to multiple peers, and uses LRU (least recently used, aka round-robin) to decide which peer gets each message. If you do not want this behavior, then you do not want a DEALER socket with multiple peers.

If you want to decide which peer gets a message, there are two options for this:

  1. create a DEALER per peer, and send on the appropriate socket
  2. create a single ROUTER connected to all peers, and use IDENTITY prefixes to route messages. You may need to pass IDENTITIES via a side channel, in order to use ROUTER-ROUTER connections.

at run time, add another endpoint to the socket by using socket.connect(endpoint). Do I need to reconnect?

No, you do not need to reconnect. You can add (and remove) peers at any time during the program.

like image 165
minrk Avatar answered Oct 12 '22 22:10

minrk