CLUSTER


A single instance of Node.js runs in a single thread. To take advantage of multi-core systems the user will sometimes want to launch a cluster of Node.js processes to handle the load.


The cluster module allows easy creation of child processes that all share server ports.


How It Works#

The worker processes are spawned using the child_process.fork() method, so that they can communicate with the parent via IPC and pass server handles back and forth.


The cluster module supports two methods of distributing incoming connections.


The first one (and the default one on all platforms except Windows), is the round-robin approach, where the master process listens on a port, accepts new connections and distributes them across the workers in a round-robin fashion, with some built-in smarts to avoid overloading a worker process.


The second approach is where the master process creates the listen socket and sends it to interested workers. The workers then accept incoming connections directly.


The second approach should, in theory, give the best performance. In practice however, distribution tends to be very unbalanced due to operating system scheduler vagaries. Loads have been observed where over 70% of all connections ended up in just two processes, out of a total of eight.


Because server.listen() hands off most of the work to the master process, there are three cases where the behavior between a normal Node.js process and a cluster worker differs:


server.listen({fd: 7}) Because the message is passed to the master, file descriptor 7 in the parent will be listened on, and the handle passed to the worker, rather than listening to the worker's idea of what the number 7 file descriptor references.

server.listen(handle) Listening on handles explicitly will cause the worker to use the supplied handle, rather than talk to the master process.

server.listen(0) Normally, this will cause servers to listen on a random port. However, in a cluster, each worker will receive the same "random" port each time they do listen(0). In essence, the port is random the first time, but predictable thereafter. To listen on a unique port, generate a port number based on the cluster worker ID.

Note: Node.js does not provide routing logic. It is, therefore important to design an application such that it does not rely too heavily on in-memory data objects for things like sessions and login.


Because workers are all separate processes, they can be killed or re-spawned depending on a program's needs, without affecting other workers. As long as there are some workers still alive, the server will continue to accept connections. If no workers are alive, existing connections will be dropped and new connections will be refused. Node.js does not automatically manage the number of workers, however. It is the application's responsibility to manage the worker pool based on its own needs.


要查看或添加评论,请登录

Amit Rai的更多文章

  • Difference between array and arraylist #java

    Difference between array and arraylist #java

    I. Size: Array in Java is fixed in size.

  • Java Stream API for Bulk Data Operations on Collections

    Java Stream API for Bulk Data Operations on Collections

    A new java.util.

  • Nashorn

    Nashorn

    Nashorn: A Next-Generation JavaScript Engine for the JVM by Julien Ponge Scenarios for using Oracle Nashorn as a…

  • Parallel Operations

    Parallel Operations

    With the addition of Lambda expressions to arrays operations, Java introduced a key concept into the language of…

  • RxJS

    RxJS

    What is RxJS? RxJS or Reactive Extensions for JavaScript is a library for transforming, composing, and querying streams…

  • HTTP/2 vs HTTP 1.x

    HTTP/2 vs HTTP 1.x

    Let’s take a look at the key differences compared to HTTP 1.x and what issue each improvement is addressing: HTTP/2 is…

  • Concurrent Accumulators

    Concurrent Accumulators

    One of the most common scenarios in concurrent programming is updating of numeric counters accessed by multiple…

  • Garbage-First Garbage Collector

    Garbage-First Garbage Collector

    The Garbage-First (G1) garbage collector is a server-style garbage collector, targeted for multiprocessor machines with…

  • Exception Propagation

    Exception Propagation

    After a method throws an exception, the runtime system searches the call stack for a method that contains a block of…

  • Microbenchmarks

    Microbenchmarks

    Microbenchmarks are coming The Java Microbenchmarking Harness (JMH) by Alexey Shipilev is taking the next step in its…

社区洞察

其他会员也浏览了