CPU-intensive application blocks competing requests

Asked

Viewed 221 times

0

In a web application it is common for each request to create a new thread that handles all processing and if for some reason the thread is blocked by executing a heavy task, the resource is allocated to the request.

I understand that this is inefficient because the allocated resources are misused and cause excessive consumption of server resources.

However when studying Node, I tested the following code that calculates a prime number and consequently makes an intense use of CPU;

Calc-primo.js

var http = require('http');
var isPrime = require('./number-util').isPrime;

var count = 1;
var server = http.createServer(function (req, res) {
    console.log('Requisição #' + count++);
    console.time('Tempo');

    var number = 0;
    var numberOfPrimes = 0;
    while(true) {
        if(isPrime(++number)) numberOfPrimes++;
        if(numberOfPrimes === 1000000) break;
    }
    res.end("Número: " + number);
    console.timeEnd('Tempo');
});

server.listen(3000);

I executed the command node calc-primo.js and in the browser open 2 tabs and in each one I made a request to localhost:3000. On the console I got the following result:

Request #1
Time: 12458ms
Request #2
Time: 12358ms

Due to the nature of the Node, the request #2 only received a reply after the request #1, but the second request waited about 12 seconds to be processed and another 12 to get a response.

Although optimizing the server resources, there is a greater slowness to get a response. I saw that this can be solved using the module cluster and have multiple instances of loop Event.

1) When adding multiple instances of Event loop (each instance being a new thread), it does not violate the Node principle of optimizing server resources?

2) The fact that the Node has a single main thread does not make the system slower for requests that require heavier processing, since there will be competition?

1 answer

0

Answering the question 1: Node.js uses an approach that seeks to optimize hardware resources (processor and memory) because it rules out the use of threads in Sleep state, meaning you don’t have idle threads waiting for some I/O event to respond. However if by chance the server has more than one processor you would have a loop Event processing in only one processor and would leave the others idle, in this case the "cluster" is a good option. Imagine that you have 3 processors and configure the Node cluster with 3, in which case there would be 3 loops to meet your requests.

Answering the question 2: Yes, Node is not a good approach for systems that require high cpu usage in Banck-end, because like Event-loop and single thread it will bottle the requests. It is ideal for systems that delegate work to some device other than the processor, such as memory (primary or secondary), database or network card.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.