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?