DEV Community

Discussion on: How can Nodejs handle many requests ?

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.