[Node.js] MultiProcess

1) Introduction

Before the introduction of worker threads, from an implementation perspective, Node.js is single-threaded (for the most part). Under the hood, there are multiple threads managed by libuv to perform asynchronous I/O operations. However, because NodeJS is based on V8, which has a hard memory limit of about 1.5GB. Therefore, it cannot automatically take advantage of additional memory above that limit.

This means that you can't take full advantage of multi-core machines. Thankfully for us, you can use the cluster module or a process manager (such as PM2) to spawn  child/worker processes to better utilize the multi-cores that you might have.

2) Cluster module

2.1) Pros

  • Scales according to # of CPU cores available on your machine.
  • Easy to manage as there is no dependency on any other module/service.
  • Easy to implement process communication.

2.2) Cons

  • You take a hit on performance of the app if there are too many messages.
  • Implementation doesn't appear to be the best for managing communication of dedicated workers.
  • Need to manage process state by yourself (no automation)
  • Can't start/stop/restarts workers without affecting app because they are coupled.

3) PM2 (Process Manager)

  • Lets you manage multiple processes, and have many features including:
  • Auto restart an app; if there is any change in code with Watch & Reload
  • Easy log management for process.
  • Monitoring capabilities of process, with dashboard.
  • Auto-restart system if it reaches max memory limit or crashes.

3.1) Pros

  • PM2 manages all processes.
  • Easy to start/stop/restart worker.
  • Workers remain independent of each other.

3.2) Cons

  • Adds dependency of PM2
  • Need to manage PM2

4) Extra Tips

  • Consider using messaging queues, like RabbitMQ, to distribute/queue up events to different cores.
  • As a general, you should be looking to spawn N-1 application processes, where N is the # of available CPU cores. That way, each process is guaranteed a good slice of one core and there's a spare for kernel scheduler to run other server tasks.

Resource

Comments

Popular posts from this blog

[Redis] Redis Cluster vs Redis Sentinel

[Unit Testing] Test Doubles (Stubs, Mocks....etc)

[Node.js] Pending HTTP requests lead to unresponsive nodeJS