Saturday, November 5, 2011

How Node.js Multiprocess Load Balancing Works

As of version 0.6.0 of node, load multiple process load balancing is available for node. The concept of forking and child processes isn't new to me. Yet, it wasn't obvious to me how this was implemented at first. It's quite easy to use however:

var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}

cluster.on('death', function(worker) {
console.log('worker ' + worker.pid + ' died');
});
} else {
// Worker processes have a http server.
http.Server(function(req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
}

This is quite a beautiful api, but it hides some clever details. For instance, how is possible for all child processes to each open up port 8000 and start listening?

The answer is that the cluster module is just a wrapper around child_process.fork and net.Server.listen is aware of the cluster module. Specifically, cluster.fork uses child_process.fork to create child processes with special variable set in their environments to indicate cluster was used. Specifically process.env.NODE_WORKER_ID is non-zero for such children.

envCopy['NODE_WORKER_ID'] = id;
var worker = fork(workerFilename, workerArgs, { env: envCopy });

Then net.Server.listen checks to see if process.env.NODE_WORKER_ID is set. If so, then the current process is a child created cluster. Instead of trying to start accepting connections on this port, a file handle is requested from the parent process:



//inside net.js
require('cluster')._getServer(address, port, addressType, function(handle) {
self._handle = handle;
self._listen2(address, port, addressType);
});

//inside cluster.js
cluster
._getServer = function(address, port, addressType, cb) {
assert(cluster.isWorker);

queryMaster({
cmd: "queryServer",
address: address,
port: port,
addressType: addressType
}, function(msg, handle) {
cb(handle);
});
};

Finally, this handle is listened on instead of creating a new handle. At which point you have another process that is listening on the same port. Quite clever, though I think the beauty of the api comes at the cost of some not so hideous hacks inside the api.

9 Comments:

At August 15, 2012 at 1:25 PM , Blogger DZONEMVB said...

Very cool post on Node.js. Would you be interested in having some of your writing promoted on DZone.com? We've got an audience of advanced developers who, I think, would like your writing.

 
At August 2, 2019 at 1:20 AM , Blogger Sathya said...

Nice post. It is really interesting. Thanks for sharing the post!
AC Online Shopping | Smart LED TV
Laptops for Sale | Best Inverter AC

 
At March 27, 2020 at 5:54 AM , Blogger hrroman said...

If you are stuck with your marketing assignment then in this case you can opt for our Marketing Assignments. we provide the bestMarketing assignment help.We also provide Integrated Marketing Communication for students across the globe. for more information contact us +16692714848.

 
At May 28, 2020 at 10:27 PM , Blogger Sathya said...

Nice post. It is really interesting. Thanks for sharing the post!
Fridge Online | Buy Fridge Online
Buy Refrigerator Online | Washing Machine Online
Washing Machine Offers | Buy Washing Machine Online
Buy Smartphone Online | Buy Smartphone Online in India
Sathya Online Shopping

 
At September 25, 2020 at 4:12 AM , Blogger Twinkle said...

Nice post. It is really interesting. Thanks for sharing the post!
Web Design Company in Tuticorin | Domain Name Registration In India
Web Design Company In India | Digital Marketing Company in India
PPC Services in India | SEO Company India
Bulk SMS Service in India | Social Media Marketing Company in India

 
At September 30, 2020 at 2:37 AM , Blogger post said...

Excellent post! Your post is very useful and I felt quite interesting reading it. Expecting more post like this. Thanks for posting such a good post.To service your laptop with offer prices.
Please visit : Laptop Service Center | Laptop Service Center in Kelambakkam

 
At April 21, 2021 at 3:23 AM , Blogger Twinkle said...

Nice post. It is really interesting. Thanks for sharing the post!
Domain Registration Company In India | Web Design and Development In India
Digital Marketing Services in India | Best PPC Company
Best SEO Company in India | Bulk SMS Service Provider in India
Social Media Marketing Services in India | Web Design Company in Tuticorin

 
At June 8, 2021 at 9:04 AM , Blogger JamesF Jones said...

Nice post with great explanation, it will help as we are currently, working with our blog. Thanks for sharing. online embroidery

 
At August 30, 2021 at 3:07 AM , Blogger Dim4ksan said...

Hello. Another thing that helped me out was pay someone to take my online exam for me. I've been using it without any issues for the past few months. A decent work isn't cheap, let's face it. I've shown it to a lot of others, and now they're all using it as well. You should at the very least give it a go. My warmest wishes to you in your endeavors!

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home