I am working on a Node.js application using Express framework and I use cluster to handle load. In testing I notice that if for some reason one of the processes is taking a long time to load, it affect other subsequent requests too. I made a simple test case for this:
https://gist.github.com/anonymous/7325816
running the example with "node app.js" and trying to load http://localhost:8080/slow
(which has a blocking calculation taking several seconds) in one browser window and http://localhost:8080/fast
immediately in another window, the latter also takes several seconds to load.
If I understand correctly, this is because the same process which is running the calculation is trying to process the new request: but how to avoid this?
Edit:
Here is output of HTTP requests running Siege after running /slow:
HTTP/1.1 200 0.01 secs: 89 bytes ==> GET /fast
HTTP/1.1 200 6.32 secs: 89 bytes ==> GET /fast
HTTP/1.1 200 0.01 secs: 89 bytes ==> GET /fast
HTTP/1.1 200 6.84 secs: 89 bytes ==> GET /fast
HTTP/1.1 200 0.00 secs: 89 bytes ==> GET /fast
HTTP/1.1 200 7.41 secs: 89 bytes ==> GET /fast
HTTP/1.1 200 0.00 secs: 89 bytes ==> GET /fast
HTTP/1.1 200 9.04 secs: 89 bytes ==> GET /fast
Edit 2:
Problem was with the latest Git version (v0.11.9-pre) of Node.js running on OSX: running current release v0.10.21 it works fine without transferring requests to the blocked process. Thanks @goten for the suggestion!
If you have several CPU cores cluster should run several process. It's pretty well explain there http://rowanmanning.com/posts/node-cluster-and-express/
var http = require('http');
var cluster = require('cluster');
var express = require('express');
if(cluster.isMaster){
//count the CPU
var cpuCount = require('os').cpus().length;
// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
cluster.fork();
}
}
else{
var app = express();
var server = http.createServer(app);
app.get('/slow', function(req, res){
someSlowFunction();
});
app.get('/fast', function(req, res){
someFasterFunction();
});
server.listen(3000);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments