Does heroku restart NodeJS server if application crashes - node.js

We are running NodeJS server on Heroku. we want to know whether heroku will restart the application if application crashes. Also will there any different behavior between free version and paid version?

It will. For several times, and then "cool off" for ten minutes and try again. From the docs:
Heroku’s dyno restart policy is to try to restart crashed dynos by spawning new dynos once every ten minutes. This means that if you push bad code that prevents your app from booting, your app dynos will be started once, then restarted, then get a cool-off of ten minutes. In the normal case of a long-running web or worker process getting an occasional crash, the dyno will be restarted instantly without any intervention on your part. If your dyno crashes twice in a row, it will stay down for ten minutes before the system retries.
The docs: https://devcenter.heroku.com/articles/dynos#automatic-dyno-restarts
EDIT Regarding free dynos: the restart behavior is the same. However, there is something called "Dyno Idling" which happens only in free dynos. Basically it means that if your dyno does not receive any request for 1 hour it will "go to sleep", and the next request will "wake it up", which will cause that next request to be slightly delayed. This happens only when you have 1 free web dyno for your app.
To circumvent that, either have 2 dynos (and then none of them will idle, but you will be paying for one), or have "something" poll your web dyno every (say) 30 minutes. Like pingdom, say.
The docs: https://devcenter.heroku.com/articles/dynos#automatic-dyno-restarts

Fast-forward to 2022, Go to your app on the Heroku portal
Click on "More" dropdown
Select "Restart all dynos"

Related

Heroku: Is my discord bot making web requests?

So i found this on Heroku's help page: dyno sleeping and I am concerned about my bot being online 24/7 so I tested to see if that was true, and the good thing is that it wasn't! But thing is, is my bot making web requests? Because if you pay attention to the highlighted text, you can notice that it says that: "Apps that only utilise a free worker dyno do not sleep, because they do not respond to web requests. Be mindful of this as they may run 24/7 and consume from your pool of hours." I still changed the web dyno to worker dyno But i can't tell if it is working 24/7 even if I shutdown my PC So if someone know that answer for this question, please tell me.
Thank you.
When running a worker the process (in this case Discord Bot) runs in the background and it does not go to sleep (unlike free Web Dynos after 30 min inactivity.)
You can see the logs in your Heroku Dashboard (https://dashboard.heroku.com/apps/{app_name}/logs, you should see some activity and definitely the process not terminating.

Node.js Active handles rise suddenly

I have a Parse Server which is a Node.js + express wrapper for a mobile app (about 100 simultaneous users every day), hosted on DigitalOcean. The app server communicates with MongoDB, which is hosted on another droplet of DigitalOcean. I'm using pm2 as a process manager and its monitoring tool, which is web-based. On the same process, we operate LiveQuery, a WebSocket server made by the Parse community as well.
The thing is, I've been having some performance issues with the server. Everything works smoothly, until the Active handles rise up uncontrollably! (see the image below) It's like after one point the server says "I'm done! Now I rest!"
Usually the active handles stay between 30 to 70. The moment I restart the process with pm2 restart everything goes back to normal!
I've been having this issue for quite some time now and I haven’t been able to figure out what’s causing it! Any help will be greatly appreciated!
EDIT: I did a stress test where I created 200 LiveQuery sockets for 1 user, instead of 2 that a user normally has and there was a spike of 300 active handles, for like 5 seconds! The moment the sockets were all created, everything went back to normal!
I usually use restart based on memory usage
pm2 start filename.js --max-memory-restart 160 --exp-backoff-restart-delay=100
pm2 has also built-in cron job or autostart script setup in case the server ever restarts, see https://pm2.keymetrics.io/docs/usage/restart-strategies/
it would be could if pm2 would provide restart options based on active connections or heap memory

Is it possible to have workers running "virtually" 24h/7 in Heroku?

I have real time data incoming and I need to process it 24h/7, but:
a) Heroku will restart Dynos once a day.
b) Heroku will restart Dynos when code is updated.
Point a, can be more or less handled by having multiple dynos, if one restarts, the other is still there.
But for point b, I don't see how I can handle it. If all dynos restart for an update, I'll lose data until they are up again.
Is there any solution?
Yes, you can enable preboot on your dynos.
Preboot changes the standard dyno start behavior for web dynos. Instead of stopping the existing set of web dynos before starting the new ones, preboot ensures that the new web dynos are started (and receive traffic) before the existing ones are terminated. This can contribute to zero downtime deployments.
heroku features:enable preboot -a <myapp>
Do read the entire docs - there are some important considerations / caveats to be mindful of, like the fact that deployments will now take several minutes to switch to the newer set of dynos.

Heroku Deployment and traffic

Since only one free dyno is available. How many users can simultaneously run my application?
And how does it calculate how many free dyno hours have been used
As Dominic mentions in the comments, this will depend heavily on how your application is architected -- what web server it's using, how quick responses can be returned, etc.
From the "Free dyno hours" perspective, the clock is ticking whenever the dyno is up and running. After 30 minutes of no activity, the dyno will idle and the clock will stop ticking. You can read more about this here: Free Dyno Hours

How to warm up a Heroku Node.js server?

Heroku reboots servers everyday. After reboot, my node server takes around 20 seconds to load a page for the first time. Is there a way to prevent this?
EDIT: You guys seem to be misunderstanding the situation. In Heroku, even production servers must be restarted daily. This is not the same as a free server sleeping. This question is aimed more at preventing lazy-loading and pre-establishing connection pools to databases.
Old question, but in case others stumble upon it like I did
Use can use Heroku's Preboot feature:
Preboot changes the standard dyno start behavior for web dynos. Instead of stopping the existing set of web dynos before starting the new ones, preboot ensures that the new web dynos are started (and receive traffic) before the existing ones are terminated. This can contribute to zero downtime deployments
You could also combine it with a warmup script like the one described in this Heroku post

Resources