DEV Community

kinensake
kinensake

Posted on

How can Nodejs handle many requests ?

Hello everyone, I'm a newbie in Node.js.
I read that Node.js has 4 threads in default, but why can it handle many requests (accessing database) at the same time quickly ?
I think it only handles 4 requests at the same time, right ?
Please help me :'((((

Top comments (4)

Collapse
 
bartmr profile image
Bartmr

In most web server technologies, each request takes up a thread. Each thread occupies approximately 1 MB of RAM, and they take long to get started or get recycled. Also, some of these technologies freeze the current thread when looking up stuff in a database till they get an answer. So, if you have 10 000 requests at the same time, you need 10 GB of RAM.

NodeJS, instead, deals with all code running time in a single thread. So if your requests are brief impulses requiring little CPU crunching, it can answer them much more faster than the multi-thread model. That is why you shouldn't use Node for data heavy stuff, but for projects that require reading and writing data, since they won't hang your only thread and won't occupy much CPU time.

Regarding the part where you talk about the 4 threads Node has: you can simulate multi-threading in NodeJS by having 4 instances of the same NodeJS project running, similar to like having 4 browser tabs of the same website running. That's a common trick to have NodeJS working in all available CPU cores.

Hope it was helpful

Collapse
 
arswaw profile image
Arswaw

The Cluster module in NodeJS has what you need. The documentation has a simple example to start. The way it works is that is causes the threads, (which run on as each individual CPU core) to share the same port. It then load balances requests on the various threads.

If you want to have a long running compute operation, you might try the NodeJS Child Process core module.

Collapse
 
solarliner profile image
πŸ‡¨πŸ‡΅οΈ Nathan Graule

Node.js doesn't have 4 threads, as JavaScript is single-threaded. Node.js' "asynchronous"-ness is a well decorated lie because while you can use async/await in the code, you're only doing that to free the single thread Node.js has to do other things it has planned.

In short, every "async" call, or calls through setTimeout or setImmediate adds the function to a stack of different functions to call. Node.js then go through them in order.

The only right answer to "how to make Node.js fast" is to have several Node.js processes running the same code, and have a way to distribute requests to them.

Collapse
 
stereobooster profile image
stereobooster

The answer is event loop

If your backend processes are heavy on IO then you will be able effectively to serve only 4 clients at a time, but otherwise you will be able to serve more.

at the same time quickly

It doesn't do it at the same time (at least not more that 4 items at the same time), it does it in turns.