Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS beanstalk nodejs multicore

I have a question which I can't find in the documentation. If I create a nodejs application using beanstalk and use multicore machines, will beanstalk utilize all of these cores? Since nodejs is a single threaded application, will beanstalk then create a new instance of nodejs for each cpu? How does this exactly work?

Cheers

like image 411
user2924127 Avatar asked Oct 19 '22 11:10

user2924127


1 Answers

No, Beanstalk fires only one nodejs process, using only one core. To take advantage of multi-core machines, the best practice is to use the cluster module. It allows to fork your process as many times as you want. From the doc:

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

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

  cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} died`);
  });
} else {
  // Workers can share any TCP connection
  // In this case it is an HTTP server
  http.createServer((req, res) => {
    res.writeHead(200);
    res.end('hello world\n');
  }).listen(8000);
}
like image 138
Antoine Avatar answered Oct 22 '22 01:10

Antoine