SCO
SCO

Reputation: 1932

Improve ZeroMQ REQ/REP performance

I'm using ZeroMQ 3.2.3 and CZmq 1.4.1. I gave the "Hello world" sample a try. That sample (https://github.com/imatix/zguide/tree/master/examples/C), when using 10 concurrent clients, allows me to exchange at most 12500 messages per second on a Intel i7 (8 GB RAM, total 8 cores) on the localhost (Ubuntu 13.04).

I read ZeroMQ could handle much more. Am I doing something wrong, or missing something ?

Here is the sample code :

//  Hello World server

#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>

int main (void)
{
    //  Socket to talk to clients
    void *context = zmq_ctx_new ();
    void *responder = zmq_socket (context, ZMQ_REP);
    int rc = zmq_bind (responder, "tcp://*:5555");
    assert (rc == 0);

    while (1) {
        char buffer [10];
        zmq_recv (responder, buffer, 10, 0);
        //printf ("Received Hello\n");
        zmq_send (responder, "World", 5, 0);
        //usleep (1);          //  Do some 'work'
    }
    return 0;
}



//  Hello World client
#include <zmq.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

int main (void)
{
    printf ("Connecting to hello world server...\n");
    void *context = zmq_ctx_new ();
    void *requester = zmq_socket (context, ZMQ_REQ);
    zmq_connect (requester, "tcp://localhost:5555");

    int request_nbr;
    for (request_nbr = 0; request_nbr != 100000; request_nbr++) {
        char buffer [10];
//        printf ("Sending Hello %d...\n", request_nbr);
        zmq_send (requester, "Hello", 5, 0);
        zmq_recv (requester, buffer, 10, 0);
//        printf ("Received World %d\n", request_nbr);
    }
    zmq_close (requester);
    zmq_ctx_destroy (context);
    return 0;
}

Thank you !

Upvotes: 3

Views: 2997

Answers (2)

Gopalakrishna Palem
Gopalakrishna Palem

Reputation: 1715

For those interested in looking for nanoMsg style of REP-REQ, here is a nice snippet on how to combine zeroMQ ROUTER sockets and NanoMSG REP-REQ:

Combining ZeroMQ and NanoMsg for Serving Web-requests: http://my-classes.com/2014/04/26/combining-zeromq-and-nanomsg-for-serving-web-requests/

Upvotes: 1

Timothy Shields
Timothy Shields

Reputation: 79471

The bottleneck you are hitting is because you aren't asynchronously streamlining your communication.

Try replacing your synchronous REQ <-> REP pattern with an asynchronous ROUTER <-> DEALER pattern.

The reason this can be much faster is that if the client can send consecutive requests without having to wait for each response in between. The cost of processing a single request/reply has two parts:

  1. the cost of sending the messages "over the wire"
  2. the cost of doing processing on request and/or reply on both client and server

An asynchronous pattern helps to greatly mitigate the cost of (2) when you are running a large number of consecutive requests.

Upvotes: 6

Related Questions