{"id":9636,"date":"2013-02-25T12:11:18","date_gmt":"2013-02-25T06:41:18","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=9636"},"modified":"2013-02-25T12:18:24","modified_gmt":"2013-02-25T06:48:24","slug":"clustering-in-node-js","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/clustering-in-node-js\/","title":{"rendered":"Clustering in Node.js"},"content":{"rendered":"<p><strong>Node.js<\/strong> is single threaded so it runs one process on a single <strong>CPU<\/strong>, as the power of <strong>Node.js<\/strong> is its performance and speed it doesn&#8217;t seem good to use only one <strong>CPU<\/strong> in a multi-core system. For utilizing all the <strong>CPUs<\/strong>, <strong>Node.js<\/strong> allows us to create a network of processes that all shares the same port.<\/p>\n<p style=\"padding:10px\">\n<p>Create a file named <strong>clusterExample.js<\/strong><\/p>\n<p>[js]<br \/>\nvar cluster = require(&#8216;cluster&#8217;); \/\/use the cluster module<br \/>\nvar http = require(&#8216;http&#8217;)<br \/>\nvar noOfCPUs = require(&#8216;os&#8217;).cpus().length(); \/\/count the no of CPUs available<\/p>\n<p>if (cluster.isMaster) { \/\/Check if the cluster is master or worker process<br \/>\n  for (var i = 0; i &lt; noOfCPUs; i++) {<br \/>\n    cluster.fork() \/\/creates a worker process<br \/>\n  }<\/p>\n<p>  \/\/give message on console when worker process starts listening<br \/>\n  cluster.on(&#8216;listening&#8217;, function (worker, address) {<br \/>\n    console.log(&quot;A new worker process with #&quot; + worker.id + &quot; is now listening to &quot; + &quot;:&quot; + address.port);<br \/>\n  });<\/p>\n<p>  \/\/give message on console and create a new worker porcess when a worker process dies<br \/>\n  cluster.on(&#8216;exit&#8217;, function (worker, code, signal) {<br \/>\n    console.log(&quot;worker process with #&quot; + worker.process.pid + &quot;died&quot;);<br \/>\n    cluster.fork(); \/\/create the new worker process<br \/>\n  });<br \/>\n} else {<br \/>\n  http.createServer(function (req, res) {<br \/>\n    res.writeHead(200);<br \/>\n    res.end(&quot;Cluster Example\\n&quot;);<br \/>\n  }).listen(9090);<br \/>\n}<br \/>\n[\/js]<\/p>\n<p>Now run the clusterExample.js<\/p>\n<p>[js]node clusterExample.js[\/js]<\/p>\n<h3>Output:<\/h3>\n<p>A new worker process with #1 is now listening to :9090<br \/>\nA new worker process\u00a0with #2 is now listening to :9090<br \/>\nA new worker process\u00a0with #3 is now listening to :9090<br \/>\nA new worker process\u00a0with #4 is now listening to :9090<\/p>\n<p style=\"padding:10px\">\n<p>Shreyance Jain<br \/>\n<a href=\"mailto:shreyance@intelligrape.com\">shreyance@intelligrape.com<\/a><\/p>\n<p style=\"padding:10px\">\n","protected":false},"excerpt":{"rendered":"<p>Node.js is single threaded so it runs one process on a single CPU, as the power of Node.js is its performance and speed it doesn&#8217;t seem good to use only one CPU in a multi-core system. For utilizing all the CPUs, Node.js allows us to create a network of processes that all shares the same [&hellip;]<\/p>\n","protected":false},"author":64,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1],"tags":[1137,1138,1124],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9636"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/64"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=9636"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9636\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=9636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=9636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=9636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}