Node js cluster and pm2 cluster - javascript

So I am running a service in node which has a node js cluster usage meaning I am running the service with node js clusters... now I want to use pm2 and I use the pm2 cluster mode.
I wonder if it's a good thing to use both of them at the same time or should I use only one of them for better performance and stuff like that...
Any help would be appreciated

To take the complexity out of your architecture I would recommend to use PM2. It lets you efficiently manage multiple processes. It has many features including:
Auto restart an app, if there is any change in code with Watch & Reload.
An easy log management for processes.
Monitoring capabilities of the process.
An auto restart if the system reaches max memory limit or it crashes.
Keymetrics monitoring over the web.
As the processes are separated, now can start/stop/restart them with your pm2.config.js, i.e
pm2 start pm2.config.js // start all processes
pm2 stop app // stop app processes
pm2 restart smsWorker // restart smsWorker

Related

How restart nodeJS application with button on javascript from browser

nodeJS file — main.js
Have a website with button which can call function from main.js. I not know how restart nodeJS script correctly. Now I run process.exit() in main.js and then, with nodemon trying restart application but nodemon tell me “[nodemon] clean exit - waiting for changes before restart”. So how correctly restart application?
Clean exit means exit code 0, which means, "Everything is okay! I intended to exit." Usually, programs that exit normally don't specifically intend to be restarted. nodemon is choosing to consider that the end of the program's operation, which isn't a totally insane thing. However, nodemon being a process manager for daemons, probably ought to just restart it anyway. I would suggest using PM2 instead, it is what most people use in production and it will correctly restart the process since its whole job is to keep services running.
Aside from all of that, I want to note that allowing a browser to restart your app is probably not a good idea. If you have carefully designed your app to be stateless and handle random shutdowns and it is clustered, etc. then maybe it is fine. But generally I would not recommend it. At the very least, make sure the request is authenticated and authorized.

How do I deploy Node.js applications as a single executable file using systemd?

I want to deploy my node application as single executable file, is it possible by using systemd, containers. I dont have enough knowledge on systemd and containers. Please help me if anybody knows about it.
Where i work we use pm2 to run NodeJS applications.
It allows you to run multiple instances on one server, monitors them and restarts if needed (on failure or you can provide a memory limit).
If you insist going with systemd, you will have to create a unit - a file that describes the execution path of you application for systemd.
It would usually be in /usr/lib/systemd/system
You would have to create a file ending with ".service"
[Unit]
Description=My NodeJS App
[Service]
ExecStart=/usr/bin/node /path/to/my/app/index.js

Node.js app cluster load balancing using PM2 on Windows

Node.js version: 4.5.0 - PM2 version: 2.0.18
I have an in-house Node.js app under test. I'm using pm2 for clustering and I assumed it will take care of load balancing. BUT when I ran my app on Windows and looking at the processes (using pm2 monit command) I cannot see even distribution of load. Sometimes one process spike to 70% and the other are not doing anything.
So, I think the load balancer of pm2 mechanism is at fault. I read somewhere that Its not using round robin on windows machines?
Could someone share their experience about this issue.
Also, I thought maybe Nginx is another solution to configure it as a load balancer in front of the pm2 Node.js cluster but not quite sure about the configuration

Are there more settings for Nodejs cluster?

Assumed you cluster your node app on a 4 CPU system, in 4 workers(childprocesses=new V8 instance) and each worker starts with about 10mb memory(default).
Is there a way to start them with more? like
--max-old-space-size=...
And how can I pass in more V8-settings to workers?
( + how do strongloop and PM2 handle it? ;) )
You can use cluster.setupMaster() to set the arguments passed to worker processes. Specifically there is an undocumented execArgv setting that defaults to process.execArgv, but you should be able to pass any array of node/v8-specific flags there.
Application arguments are passed via the args setting.
If you are using PM2 then it utilizes full CPU memory as on demand, since it provides a lot configuration for load balancing and performance.
If you want to utilize CPU with it, just increase the number of instances here
pm2 start app.js -i 2
where i is the number of instances you want to start.
While using pm2 following steps are important:
pm2 stop all
pm2 delete all
pm2 start app.js -i 2
Always use pm2 delete all to unregister the CPU, since if you stop it, it still reserve the CPU.

Update file without restart of server in Node.js

Is it possible to update a route,model or controller.js file without restarting the Node.js Server?.
I'm currently dealing with a client who wants constant changes to the application in a very frequent event. And the application deals with user session etc.. Whenever we make any changes to the application it requires a restart for the update to get reflect, which is very expensive in-terms of an high traffic situation.
I have seen some server application providing a feature called Rolling Restart but again I'm not sure whether it is a good way to maintain the user session across the restart event. Or do we have any other solution to deal with this kind of situation.
You can restart a server without downtime yes, I recommend you take a look at PM2 https://github.com/Unitech/pm2
You can have multiple instances of node running and when you set a restart it does it gradually, making that you don't have downtime, it also distributes load to the different instances running so it speeds up your app, hope this helps :-)
Nodemon is what I have used before and I was very happy with it.
Install
npm install -g nodemon
then run your app with
nodemon [your node app]
Done

Categories

Resources