简体   繁体   中英

Would it be really an advantage to achieve multi-threading using web workers in NodeJS?

I think the question is pretty explicit. JavaScript is single threaded and NodeJS still achieves incredible performances. We could think obvious that multi-threading would take NodeJS performances further, but it might be wrong in some cases.

For example, I'm currently building a starter project using NextJS. I wonder if handling each request in a separate thread would be worth it.

Thank you!

As far as I know in production mode nodeJs "usually" used as:

  • nginx server (used as security layer and as HTTPS proxy)
  • number of child NodeJs processes (amount === number of cores)

That means that all cores are used,

request is processed by single core,

server processes several requests at once

=== UPDATE ===

If you want to divide single request processing into several threads - then just remember that cross-process communication is expensive in NodeJS, and you need to delegate huge tasks to other threads/webworkers

If you see the need to split app into several threads - consider using microservices architecture, eg using http://senecajs.org/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM