0

I started learning nodejs some time back, and the cool thing about it is that whatever the developer writes, runs on a single thread.
But when I do a non-blocking file I/O or network I/O, some thread has to wait for the response, and that is being done by the underlying V8 architecture.
We say that nodejs process runs on a single core because there is only 1 thread allowed, and we'll need multiple threads only then CPU can schedule them to different cores.
But when we say that nodejs process runs on a single core, does it mean that the underlying V8 javascript engine is also running on a single core? All node processes use the same instance of V8 js engine or is it like a separate support for each of the processes?

These questions are coming to mind because I want to create clusters for a nodejs process, and I wish to know if I can create n clusters on an n core machine or should I leave a significant number of cores for the V8 engine?

Edit: Found a link which gives some answer http://blog.carbonfive.com/2014/02/28/taking-advantage-of-multi-processor-environments-in-node-js/
"These child Nodes are still whole new instances of V8. Assume at least 30ms startup and 10mb memory for each new Node. That is, you cannot create many thousands of them."

neeraj
  • 1,191
  • 4
  • 19
  • 47

2 Answers2

0

Move your question to discussion forum as this is not right place to 'discuss/share' ideas.

I could not find answer to - if single instance of V8 is used or not. [nice question by you]

Following comments are according to my understandings:

Thumb rule is, if you have multiple cores then use cluster and allow node to spawn multiple processes[one per core]. Inter process communication will be taken care by event machine itself. So I think V8 instance will be shared.

Going ahead, use node-redis-cluster which will help you to share data via redis between node processes.

Clusters are warhorses for node. cheers.

Pranav
  • 2,054
  • 4
  • 27
  • 34
  • 'allow node to spawn multiple processes[one per core]' How do we know each process will go to a separate core? Isn't that the task of the thread scheduler of the OS over which nobody has any control? – neeraj Jul 24 '14 at 11:15
  • yes, you are right, the theory is - it is 'Fork'. All worker processes are bound to a master(creator) so that they can exchange the server handle. Try using graphical tool 'HTOP' so that you will come to know how cores are used. Awesome Node. – Pranav Jul 24 '14 at 11:28
0

But when we say that nodejs process runs on a single core, does it mean that the underlying V8 javascript engine is also running on a single core? All node processes use the same instance of V8 js engine or is it like a separate support for each of the processes?

Nodejs uses V8 engine to process. And V8 engine is single-threaded by design.

if I can create n clusters on an n core machine or should I leave a significant number of cores for the V8 engine?

I think you should use the whole available machine cores. However, it's all your choice. If you don't want node to use the whole machine cores, then set a number of cores that are going to be used.

Lewis
  • 14,132
  • 12
  • 66
  • 87
  • V8 is single threaded? How can it be single threaded? who will create threads who will wait on the asynchronous operations started by the node processes? – neeraj Jul 24 '14 at 11:42
  • What do you mean by `who`? It's just about Javascript. And Javascript is single-threaded by design. – Lewis Jul 24 '14 at 11:46
  • V8 is written in C++. And asynchronous functions are asynchronous because something else is synchronously waiting for the response. That's how I understand. – neeraj Jul 24 '14 at 12:02
  • Eventhough V8 is written in C++, it's a `Javascript Engine` that follows `ECMAScript standard`. Also, `multithreading` and `asynchronous` are not the same. You can take a look at this question to figure it out. http://stackoverflow.com/questions/600795/asynchronous-vs-multithreading-is-there-a-difference – Lewis Jul 24 '14 at 13:31
  • I read the discussions and they seem to suggest I am right. Some thread or process WILL wait for the task to complete. It might not be utilizing CPU, but it will wait. If V8 is single threaded, everything else will HAVE to stop. But since this is not true, I expect that V8 is not single threaded at all. – neeraj Jul 24 '14 at 13:44