- Maximizing Cluster Perfromance : Using the amount of logical cores available on a system, increasing the performance of web servers so that the response time can be lesser when multiple requests are being made. A module that is responsible for providing cluster related API is called a cluster. It's NodeJS inbuilt module. Cluster uses single master(parent) process and many worker(child) process. See below diagram to know more…
- Load Balancing using PM2 (Process Manager): It is a runtime process management and monitoring tool with a built-in load balancer for NodeJS applications which makes managing them easier on production.
- This could be installed globally on the system, using the following command:
npm i pm2 -g
-
How to use: The best thing about PM2 is easiness. Some of the basic commands for using PM2 are following one:
-
To Start: The following command will start an application.
pm2 start server.js
8 instances shows the max number of logical cores available in a system.
- To Stop an Application/Process:
pm2 stop pid or App-name
- To Restart an Application/Process: Restart will kill and start an application
pm2 restart pid or App-name
- To Reload an Application/Process: As opposed to restart, which kills and restarts the process, reload achieves a 0-second-downtime reload.
pm2 reload pid or App-name
- To Delete an Application/Process:
pm2 delete pid or App-name
- To effect “All” Applications/Processes: 'All' attribute will affect every process and application.
pm2 reload all
- To Monitor:
pm2 monit
- To View Logs:
pm2 logs
Cluster
- Each process has it’s own memory with it’s own Node (v8) instance
- One process is launched on each CPU
Worker Thread
- One process total
- Each worker will have its own instance of V8 and Event Loop
- Shares memory with other threads