Dealing with Node.js high CPU in production

Alex Vasilev
Geek Culture
Published in
5 min readAug 16, 2021

--

Let’s assume you have a Node.js service deployed in production, every line of code is reviewed and covered with tests. But with 10 requests per second, the Node.js process begins to consume 100% CPU, or there are some random spikes on a CPU graph, as a result, response time grows and affects all consumers. For sure you can just increase the amount of the running instances, but it’s not a resolution of the problem, the service will behave the same.

High CPU root causes

  • Loops and iterations. Any .map, .reduce, .forEach and other iteration methods call can cause a problem if you don’t limit the size of the iterable collection. The same potential issue with for and while loops. If you have to deal with big collections, use streams or split collections into chunks and process them asynchronously. It will distribute the load between different EventLoop iterations, and the blocking effect will be reduced.
  • Recursive functions. The same principle here, you need to take about recursion depth, especially when the function is synchronous. Example from my experience: one of my teammates added a function for solving the Trading Salesman Problem, it had worked fine until it was called with 28 points. As a result, each call blocked the whole Node.js process for 2 seconds with 100% CPU.

--

--