Heavy-duty CPU application blocks concurrent requests

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 gets blocked by performing 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.

While studying Node, I tested the following code that calculates a prime number and therefore makes CPU intensive use;

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 opened 2 tabs and in each one I made a request to localhost:3000 . In the console I get the following result:

  

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

Due to the nature of the Node, the #2 request only received a response after the #1 request, but the second request waited for about 12 seconds until it was processed and 12 more for a response.

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

1) When adding multiple instances of the event loop (each instance being a new thread), does it not violate the principle of Node that is to optimize server resources?

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

    
asked by anonymous 22.09.2017 / 18:31

1 answer

0

Answering question 1: Node.js uses an approach that seeks to optimize the hardware resources (processor and memory) because it discards the use of threads in sleep state, ie, you do not 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 an event loop processing on only one processor and leave the others idle, in which case the cluster is a good option. Imagine that you have 3 processors and configure the cluster node with 3, in which case there would be 3 event loops to fulfill your requests.

Answering question 2: Yes, the node is not a good approach for systems that demand high usage of the cpu in banck-end, because as the 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 the network card.

    
16.02.2018 / 18:16