Deep Dive Into Node.js and MongoDB - Day 8


In case you missed the other days:
Day 1 Deep Dive
Day 2 Deep Dive with MongoDB
Day 3 Deep Dive with MongoDB
Day 4 Deep Dive with MongoDB
Day 5 Deep Dive with MongoDB
Day 6 Deep Dive with Mongoose
Day 7 Deep Dive with Clustering

Keeping with the clustering discussion from yesterday, I started to look into load balancers and other options outside of the cluster module I started utilizing yesterday to solve the scaling issue that exists outside of the box. In doing this research I came across a few solutions:

  1. http-proxy module
  2. nginx
  3. pm2
After some deep diving into other developer's comments and overall plans for my deep dive I chose pm2.


As mentioned previously, this point I am going to assume MongoDB is up and running, if you do not, check my first day post for details on how to get it up and running.


To get started with pm2 you will need to install via:

npm install pm2 -g
Since pm2 has a built in clustering option, the code written last night has no purpose anymore, but thankfully I abstracted out the actual "worker" code to it's own worker.js file. That being said, all you have to do to have pm2 run your node.js code for all of your cpu cores is:
pm2 start worker.js -i 0
From there you should see pm2 kicking off a process for each of your cpu cores available. In my case, my 2014 Razer Blade laptop has 4 cores/8 threads so it kicked off 8 processes. If you wanted to limit the number of processes you can specify a different number instead of 0.

One of the neat things about pm2 is the ability to monitor the processes with a simple:
pm2 monit
Which on my machine produced:

A handy command when you're done is to issue a
pm2 stop all
command to stop all of the processes.

Next up...

Hopefully this was interesting for those following me along my deep dive in to node.js. I am excited to keep deep diving into pm2 tomorrow. An issue I was running into on Windows 10 (need to try it on Linux) is that only one process was being hit at ~65% according to pm2. Whether this is a Windows specific issue, a problem with pm2 or a problem with my code I need to further dive into.

All of the code thus far is committed on GitHub.
none on this post