Search code examples
node.jsexpressprofilingcpupm2

node.js CPU usage spikes


I have an express.js app running in cluster mode (nodejs cluster module) on a linux production server. I'm using PM2 to manage this app. It usually uses less than 3% CPU. However, sometimes the CPU usage spikes up to 100% for a short period of time (less than a minute). I'm not able to reproduce this issue. This only happens once a day or two. Is there any way to find out which function or route is causing the sudden CPU spikes, using PM2? Thanks.


Solution

  • i think have some slow synchronous execution on some request in your application.

    • add log every request income on middleware and store to elastic search and find what request have long response time or use newrelic (easy way but spend more money).
    • use blocked-at to find slow synchronous execution if detect try to use worker threads or use lib workerpool