Multiple node.js message receivers for a redis queue

允我心安 提交于 2019-12-12 02:28:03

问题


Trying to implement queue processor(s) in node.js application using rsmq-worker (https://github.com/mpneuried/rsmq-worker). Have to run multiple workers to process messages in parallel.

How do we setup multiple workers?

Exploring on using - https://github.com/Automattic/kue#parallel-processing-with-cluster


回答1:


Simply create multiple instances of the RSMQWorker pointing to the same queue.

Example:

var workers = [];
for (let i = 0; i < NUMBER_OF_WORKERS; i++) {
        workers[i] = createWorker(i);
}

function createWorker(i) {
    const worker = new RSMQWorker(QUEUE_NAME, {...});
    worker.on("message", processMessage)
    return worker;
}

Then you can use the auto-start option of the rsmq-worker or start them manually:

for (let i = 0; i < workers.length; i++) {
    workers[i].start();
}

With this approach you will be processing multiple messages in parallel using one single Node.js instance. It improves the performance if your message processing logic involves some I/O.

For an additional level of parallelism, you can run the above code in multiple instances of Node.js, as mentioned in other answer.




回答2:


You just launch multiple worker processes and they will start to process messages in parallel. RSMQ states that it guarantees that messages will be delivered only once so only one worker will process a single message.

Take a look at RSMQ readme, https://github.com/smrchy/rsmq. Pay attention to:

Guaranteed delivery of a message to exactly one recipient within a messages visibility timeout.

Please have a look at the createQueue and receiveMessage methods described below for optional parameters like visibility timeout and delay.



来源:https://stackoverflow.com/questions/34422094/multiple-node-js-message-receivers-for-a-redis-queue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!