简体   繁体   中英

NodeJS app's CPU usage percentage goes to 100% for all requests

I have a NodeJS app built on Express. I use pm2 to keep the app running. Sometimes, the app hangs ie pages just keep loading. If I try to connect to server using SSH, then that is also hanging and does not show any prompt or anything. After some time (around 1-2 minutes), app starts running normal and I can also connect to server using SSH in terminal.

To see if it is because of spike in CPU usage I connected my pm2 process with keymetrics.io . Here I noticed that on every request (even for simple login page), the CPU usage graph hits 100% and then comes back to normal.

It doesn't hang the app, but I am confused if this is normal behaviour and if not then what could be the reason for this? The app is not slow normally from user perspective.

Well, yes that is normal. At the exact moment in time that any Javascript is running, the CPU is 100% occupied. That is normal and expected. At any given moment a CPU is either running 100% or not running at all. There is no in-between. When you see 50%, that is just a moving average over some time period that consists of being at 100% for half the time and at 0% for half the time. So, the operative question is how long was the CPU at 100%. If it was a few ms, that's nothing. If it was a few minutes, that's likely an issue.

Most monitoring tools will show you more of a moving average over time so you don't see instantaneous bursts to 100%, but if your monitoring tool is showing you an instantaneous burst at 100%, then that is perfectly normal. That's the moment that actual Javascript is running.

If you saw your CPU at 100% for a meaningful amount of time (like for minutes), then that would probably indicate an issue to look into because during that time your server is probably not responsive to other requests. But, a momentary and short spike to 100% is normal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM