mksmanjit
mksmanjit

Reputation: 244

How many thread really have in Node

As Node is using java-script engine internally it means Node has only one thread but i was checking online,some people saying two thread some saying one thread per core and some are saying something else,Can anyone clarify?

And if it is single threaded then how many request it can handle concurrently because if 1000 request are coming at the same time and say each request taking 100ms then the one at the 1001th will take 100 sec (100×1000=100,000 ms=100 sec).

So it means Node is not good for large number of user?

Upvotes: 3

Views: 1033

Answers (2)

shiva
shiva

Reputation: 27

Node has 1 main thread(that's why called single threaded) which receives all request and it then give that request to internal threads(these are not main thread these are smaller threads) which are present in thread pool.
Node js put all its request in event queue. And node server continuously runs internal event loop which checks any request is placed in Event Queue.
If no, then wait for incoming requests for indefinitely else it picks one request from event queue.
And checks Threads availability from Internal Thread Pool if it's available then it picks up one Thread and assign this request to that thread.
That Thread is responsible for taking that request, process it, perform Blocking IO operations, prepare response and send it back to the Event Loop Event Loop in turn, sends that Response to the respective Client.

Click here for Node js architecture image

Upvotes: 2

Mestre San
Mestre San

Reputation: 1915

Based on your writing, I can tell you know something about programming, some logic and, some basic math, but you are getting into javascript (not java-script) now.

That background makes you an excellent fit for this new javascript on the server-side with nodejs, and I foresee you becoming great at it.

What is clear to me is that you are confusing parallelism with concurrency and this post here is going to be very useful for you.

But a TLDR could be something like this:

  • Parallelism: 2 or more Process/Threads running at the same time
  • Concurrency: The main single Process/Thread will not be waiting for an IO operation to end, it will keep doing other things and get back to it whenever the IO operation ends

IO (Input/Output) operations involve interactions with the OS (Operating System) by using the DISK or the NETWORK, for example.

In nodejs, those tasks are asynchronous, and that's why they ask for a callback, or they are Promise based.

const fs = require('fs')

function myCallbackFunc (err, data) {
  if (err) {
    return console.error(err)
  }

  console.log(data)
}

fs.readFile('./some-large-file', myCallbackFunc)
fs.readFile('./some-tiny-file', myCallbackFunc)

A simple way you could theoretically say that in the example above you'll have a main thread taking care of your code and another one (which you don't control at all) observing the asynchronous requests, and the second one will call the myCallBackFunc whenever the IO process, which will happen concurrently, ends.

So YES, nodejs is GREAT for a large number of requests.

Of course, that single process will still share the same computational power with all the concurrent requests it is taking care.

Meaning that if within the callback above you are doing some heavy computational tasks that take a while to execute, the second call would have to wait for the first one to end.

In that case, if you are running it on your own Server/Container, you can make use of real parallelism by forking the process using the Cluster native module that already comes with nodejs :D

Upvotes: 3

Related Questions