1

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..

2 Answers 2

1

According to my knowledge, clusters are used for load balancing.Suppose 4 users access your api at same instant, if there are no clusters, a single thread will be responsible for serving their request.However, if there are 4 workers, each thread will be responsible for serving a particular user's request.

But clusters cannot reduce the time taken by your logic to generate a response.Try to optimize your code

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply.. Is there any other module like cluster to handle the loop process within api??
javascript is single threaded. You can't create threads in it like in java to execute different parts of your code by different processes
what you can do is , integrate your service with other technology such as python,java , so that node.js only handles serving the request part but other tech handles the extensive computations part
0

There is a lot of reading material at the introductory level about node. However there is much less material about the node event loop.

The node event loop is the one responsible for scheduling and executing all asynchronous operations in node. It is effectively node.js's only valid threading model and you need to work within its limitations.

Among the limitations node.js imposes, you cannot have long lived synchronous code. e.g: serialization, encryption, compression... any operation that takes a large input can be potentially blocking unless implemented very carefully.

What goes in the event loop?

Some of the operations in that share the same event loop with your code include: reading/writing from a socket, which is what happens when you serve requests (e.g: using express).

Most application protocols are TCP based, and TCP is time sensitive. If a TCP transmission is not completed in a timely manner, there are retries (TCP retransmissions), and after sufficient retries, a connection timeout.

If you add a long lived task to the event loop, other tasks can timeout.

How to prevent blocking the event loop?

You can surely distribute the work over multiple workers to balance the load. That's fine. However you need to also distribute the work over multiple event loop ticks, to ensure other tasks don't suffer from starvation.

This means that at some moment your logic will need to be divided into chunks, using setImmediate to execute each chunk to allow node.js to process other tasks in the event loop before resuming your computation.

How to distribute work?

It largely depends on what the tasks are. You can create a queue from which each worker pulls work from. e.g: with a queue such as zmq, beanstalk, etc.

10 Comments

user is "looping" the result array from database to perform his calculation. setImmediate doesn't solve the issue to increase computation performance.All it will do is prevent event loop blocking
Yes, that's a good start. Even if he distributed his work across multiple workers, the blocking problem would have remained. Unfortunately most people underestimate the severity of blocking the event loop. A slow application is better than a crashing application. Once it doesn't crash, make it faster.
How to perform operations in a non-blocking way is indeed important.But his issue is regarding loop's performance, not event blocking.
It is about event loop blocking if his code is taking 7 seconds to execute. Code blocking the event loop for 50 ms can be severe enough to cause service degradation.
What you are suggesting will allow server to handle multiple request, but it doesn't reduces computation time.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.