In my node.js application I have a rest api in which it has very heavy logic with looping which takes more than 7 secs. The looping count may increase in future and the time going to increase.
In order to reduce its timing I tried to use clusters. It created a separate workers depending on the no of cpus mentioned.
But after implementing the cluster in app.js page, I found that the same time has been taken for the rest api.
My app.js page:
// Include the cluster module
var cluster = require('cluster');
// Code to run if we're in the master process
if (cluster.isMaster) {
// Count the machine's CPUs
var cpuCount = require('os').cpus().length;
// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
cluster.fork();
}
// Listen for dying workers
cluster.on('exit', function (worker) {
// Replace the dead worker, we're not sentimental
console.log('Worker %d died :(', worker.id);
cluster.fork();
});
// Code to run if we're in a worker process
} else {
var express = require('express');
var app = express();
var sampleApi = require('./controllers/restApi.js');
app.post('/api/getResponse', sampleApi.getResponseMtd);
// Bind to a port
app.listen(8080);
console.log('Worker %d running!', cluster.worker.id);
}
The service I have called is a normal service connecting mongo DB with looping the result to perform calculation logic
Did the cluster be used to handle the no of request came in at a particular time using the no of workers or it will be used to split the work with in one api service (service called once at a time).
I want loop with in the service to be splited and give the result finally so that the time taken will be reduced.
Please help to overcome this confusion regarding the cluster and how the cluster or any other method be used to speed up the looping.
Thanks in advance..