1

In the excellent book Node.js the right way the author shows this example:

const
    fs = require('fs'),
    zmq = require('zmq'),
    // socket to reply to client requests
    responder = zmq.socket('rep');
    // handle incoming requests
responder.on('message', function(data) {
    // parse incoming message
    let request = JSON.parse(data);
    console.log('Received request to get: ' + request.path);
    // read file and reply with content
    fs.readFile(request.path, function(err, content) {
        console.log('Sending response content');
        responder.send(JSON.stringify({
            content: content.toString(),
            timestamp: Date.now(),
            pid: process.pid
        }));
    });
});

and then says:

There is a catch to using ØMQ REP/REQ socket pairs with Node. Each endpoint of the application operates on only one request or one response at a time. There is no parallelism.

He then uses the nodejs cluster module to fix that scaling issue.

I was surprised by that the, whole concept of nodejs and the book is nio and asyncrhonisity so I was more like expecting this solution:

  1. Request arrives and handled by zmq.
  2. fs handles it asynchronously.
  3. More requests arrive and handled by zmq.
  4. All are directed to fs asynchronous readFile nio.
  5. Once there is a callback from fs.readFile zmq responder is initiated for each nio which completes.

in this way with a single event loop and single thread for the requests to zmq we can serve multiple requests. Why isn't that the case? Must we open more threads to increase scale? Why can't nio and asynchronousity help with our single thread?

1 Answer 1

2

This has not so much to do with Node.js but more with how zmq works.

If you read the paragraph about ZMQ_REQ there is this part:

This socket type allows only an alternating sequence of zmq_send(request) and subsequent zmq_recv(reply) calls.

So a worker that receives a request can only receive an other request once the first request has gotten a reply. This is part of the zmq spec.

If you want to handle multiple requests at the same time you can create multiple responders just call zmq.socket('rep'); multiple times.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.