DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Cluster Computing with Node.js

Cluster Computing with Node.js

Hemanth HM user avatar by
Hemanth HM
·
Oct. 09, 12 · Interview
Like (0)
Save
Tweet
Share
13.44K Views

Join the DZone community and get the full member experience.

Join For Free

A single instance of Node runs in a single thread. To take advantage of multi-core systems we may want to launch a cluster of Node processes to handle the load!

That is to say if a system has 8 cores in it a single instance of node would use only one core, but to make the most of it, we can make use of all the cores with the wonderful concept of workers and more interestingly they can share the same port!

This module is still in Stability: 1 - Experimental phase, check out the code below to enjoy the awesomeness of the cluster!

var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
 
if (cluster.isMaster) {
  // Fork workers.
  require('os').cpus().forEach(function(){
    cluster.fork();
  });
  // In case the worker dies!
  cluster.on('exit', function(worker, code, signal) {
    console.log('worker ' + worker.process.pid + ' died');
  });
  // As workers come up.
  cluster.on('listening', function(worker, address) {
    console.log("A worker with #"+worker.id+" is now connected to " +\
     address.address +\
    ":" + address.port);
  });
  // When the master gets a msg from the worker increment the request count.
  var reqCount = 0;
  Object.keys(cluster.workers).forEach(function(id) {
    cluster.workers[id].on('message',function(msg){
      if(msg.info && msg.info == 'ReqServMaster'){
        reqCount += 1;
      }
    });
  });
  // Track the number of request served.
  setInterval(function() {
    console.log("Number of request served = ",reqCount);
  }, 1000);
 
} else {
  // Workers can share the same port!
  require('http').Server(function(req, res) {
    res.writeHead(200);
    res.end("Hello from Cluster!");
    // Notify the master about the request.
    process.send({ info : 'ReqServMaster' });
  }).listen(8000);
}

On a quad core machine, the output would be as below, for each hit two works are responding.

Number of request served =  0
A worker with #2 is now connected to 0.0.0.0:8000
A worker with #4 is now connected to 0.0.0.0:8000
A worker with #1 is now connected to 0.0.0.0:8000
A worker with #3 is now connected to 0.0.0.0:8000
Number of request served =  0
...
Number of request served =  2
..
Number of request served =  4
...
Number of request served =  6

One can also try : ab -n 1000 -c 5 http://127.0.0.1:8000 to benchmark this!

Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
 
 
Server Software:        
Server Hostname:        127.0.0.1
Server Port:            8000
 
Document Path:          /
Document Length:        19 bytes
 
Concurrency Level:      5
Time taken for tests:   0.171 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      94000 bytes
HTML transferred:       19000 bytes
Requests per second:    5841.57 [#/sec] (mean)
Time per request:       0.856 [ms] (mean)
Time per request:       0.171 [ms] (mean, across all concurrent requests)
Transfer rate:          536.24 [Kbytes/sec] received
 
 
 
Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      1
  95%      2
  98%      4
  99%      5
 100%      8 (longest request)

 

 

cluster Node.js Computing

Published at DZone with permission of Hemanth HM, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Data Leakage Nightmare in AI
  • Java Development Trends 2023
  • PostgreSQL: Bulk Loading Data With Node.js and Sequelize
  • The 31 Flavors of Data Lineage and Why Vanilla Doesn’t Cut It

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: