1

Cause threads are new to Node.js, it would be beneficial if someone could compare threading in node.js with other programming languages like C++ & Java. Please answer the following questions especially?

What are the similarities and differences of threading implementation in node.js compared to other programming languages like C++ & Java?

What is the maximum number of threads for a node.js application?

2
  • @jfriend00 i wish i knew a better place to ask my question. Commented Oct 28, 2019 at 22:10
  • I don't know. You could probably explore less generic and more specific questions about one specific language's threading implementations here (such as your part #2). Commented Oct 28, 2019 at 22:16

1 Answer 1

4

This is a pretty broad topic that one could write several chapters of a book about. I'll try to stay at a pretty high level and give you a few things to consider.

What are the similarities and differences of threading implementation in node.js compared to other programming languages like C++ & Java?

Not very many similarities at all. Node.js creates a separate programming context for each thread. That programming context does not share access to the regular variables of your main thread or other threads like C++ or Java do. Instead you communicate between threads with some sort of messaging which forces synchronization of access and requires serialization/copying of data that is passed between them (except for a few specialty, managed situations).

In C++, to access data that other threads can access you must use tools like a mutex to prevent race conditions. This type of programming is very doable by strong, disciplined developers with good design and a good attention to detail, but it is very easy to make mistakes with threaded code in C++ that can cause very hard to find bugs. Because node.js forces restricted access and forces synchronization via messaging, one isn't generally exposed to the same kinds of potential problems.

What is the maximum number of threads for a node.js application?

As far as I know, there is no coded limit to the number of threads you can have in a node.js application. As with any language, each thread consumes resources and a given system configuration and usage profile will have some practical limit. But, that can only be determined with testing and trial and error for a given situation. If you're thinking about a node.js app with tons of threads, then you may be pursuing the wrong design for the node.js architecture since that is generally not necessary or optimal with the asynchronous I/O model.

If you are just going to run all your threads with very CPU intensive operations, there will be little performance or throughput advantage to running more threads than you have CPU cores (or threads by hyperthreading) and, in fact, there may be a disadvantage to running more because of increased overhead in tasks switching. This is much of the same discussion when using node.js clustering and deciding how many clustered instances to run on the same host.

And, with any multi-threaded design, you have to fully understand where your bottlenecks are. If everybody is accessing and depending upon the same database (for example), you may find that only a couple threads is more than enough to keep the database fully busy.

Sign up to request clarification or add additional context in comments.

1 Comment

I have read articles which suggest to limit the number of threads to the number of CPU cores. Could you edit your question to cover this part also?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.