Node.js/Express and parallel queues

后端 未结 4 1927
刺人心
刺人心 2020-12-30 04:16

We are building an infrastructure which features a Node.js server and Express.

In the server, what is happening is as follow:

  1. The server accepts an inc
4条回答
  •  时光说笑
    2020-12-30 04:38

    tldr; You can use the native Node.js cluster module to handle a lot of concurrent requests.

    Some preamble: Node.js per se is single threaded. Its Event Loop is what makes it excellent for handling multiple requests simultaneosly even in its single thread model is, which is one of its best features IMO.

    The real deal: So, how can we scale this to even handle more concurrent conections and use all CPUs available? With the cluster module.

    This module will work exactly as pointed by @Qualcuno, which will allows you to create multiple workers (aka process) behind the master to share the load and use more efficiently the CPUs availables.

    According with Node.js official documentation:

    Because workers are all separate processes, they can be killed or re-spawned depending on your program's needs, without affecting other workers. As long as there are some workers still alive, the server will continue to accept connections.

    The required example:

    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('exit', function(worker, code, signal) {
        console.log('worker ' + worker.process.pid + ' died');
      });
    } else {
      // Workers can share any TCP connection
      // In this case its a HTTP server
      http.createServer(function(req, res) {
        res.writeHead(200);
        res.end("hello world\n");
      }).listen(8000);
    }
    

    Hope this is what you need.

    Comment if you have any further questions.

提交回复
热议问题