A
A
Al2016-07-12 00:32:35
Node.js
Al, 2016-07-12 00:32:35

How to properly run a nodejs application in a cluster on a multiprocessor machine?

We take an example from documentation:

if (cluster.isMaster) {
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }
  cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} died`);
  });
} else {
  // start my application
}

As a result, the application started 8 times (I have a core i7-4770K), everything seems to be correct, but! The application has cronjobs and sockets are used everywhere in addition to the http server. Because of this, I constantly see several attempts to connect sockets, at startup, the same config file is read and parsed several times (used to initialize application settings), sometimes this leads to errors when trying to read simultaneously. And this, I'm sure, is not all the rake that I stepped on when trying to run the application on each cpu core.
What should I do now? Need to pick the whole application and somehow crutch everything so that it is not duplicated in cluster mode? Or is there still an option to do everything painlessly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xfg, 2016-07-12
@Sanu0074

Think of each worker as if it were running on a remote machine that doesn't know anything about another worker running on another remote machine. Then it will become clear that the tasks should be added to the queue server, and he will already scatter the tasks among the workers so that they do not try to perform the same task at the same time.
For queues, you can use kue . Works on top of redis, which many people already use to scale the application across several physically remote servers, caching and everything else.
The config can be included in the index file and given to each copy of the application.

const Application = require('path/to/Application');
const config = require('path/to/config');

if (cluster.isMaster) {
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }
  cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} died`);
  });
} else {
  new Application(config).run();
}

I don't quite understand about sockets. Is the application trying to connect somewhere and acting as a client or is the application trying to raise its socket server?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question