1

We have a node service on which we detected CPU Blocking . Culprit is the usual (JSON.Parse())(Which is a pure computational operation)

In the context I have a pretty dumb question, Is CPU blocking possible(assume single core) without peaking the CPU?.

randomness
  • 1,377
  • 1
  • 14
  • 21
  • What do you mean by CPU blocking? That's a broad term. And why is `JSON.parse` the culprit, and why is that "the usual"? – Josh Beam Sep 15 '15 at 02:02
  • By CPU Blocking, I mean that the processors is not able to perform any other activity due to the current ongoing activity. This is a case specific to node.js where it is typical that when you parse very large json objects in range of 40+MBs (In a system which spupports 100 TPS), the CPU gets blocked/unresponsive (Due to node's single threaded nature). – randomness Sep 15 '15 at 02:08
  • Have you tried wrapping the call to `JSON.parse` in a timeout? – Josh Beam Sep 15 '15 at 02:11
  • We are thinking of solving it by various means including 1. web workers 2. Clusters etc. But probing on this issue made me think what does CPU Blocking even means and is it different from 100% CPU utilization on any scenarios – randomness Sep 15 '15 at 02:29
  • Have you seen this question? http://stackoverflow.com/questions/10773564/which-would-be-better-for-concurrent-tasks-on-node-js-fibers-web-workers-or-t – Josh Beam Sep 15 '15 at 02:49

2 Answers2

1

In your context, "CPU blocking" just means that the single core that node.js is running Javascript on is busy. And, while a given core is busy, it is running full time. You can't have a 50% utilized core that is running a large JSON.parse() operation. It's either running an operation or it's not. A 50% utilization just means that it's only running half the time, but when it's running, it is fully "peaked".

A typical CPU these days has multiple cores. One large JSON.parse() operation would only be running on one single core. So, while that core would be running max during the duration of the JSON.parse() operation, the other cores would not and thus the whole CPU would not necessarily be maxed as the other cores are available to do other work.

Since node.js is single threaded for Javascript execution, the main way to use those other cores is to either spawn a child process to run certain operations or to use node.js clustering so you have multiple node.js processes all serving requests that arrive for your server.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thank You for the explanation. So can I conclude (elaborating for the sake of clarity) 1. when a JSON.parse() is running on the single core CPU, for the time period it is running it is 100% utilized. 2. And not 50% utilized but not accessible for other operations as node is single threaded? – randomness Sep 15 '15 at 02:24
0

I don't think the whole CPU would be blocked, as you likely have multiple cores. However, JSON.parse is a synchronous command and will block any remaining synchronous code execution on whichever core your process is running. So I think the answer to that is to make JSON.parse asynchronous.

There are a number of ways to do this, and I think it might depend on your specific implementation which one you decide to go with:

  • Spawn a child process to handle the parsing
  • Use a web worker (this is more "JavaScript-esque")
  • Wrap the call in a setTimeout
  • Delegate the parsing to a different server instance (this one is interesting)
Josh Beam
  • 19,292
  • 3
  • 45
  • 68