Search code examples
node.jsmultithreadingperformanceweb-workersingle-threaded

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!


Solution

  • 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, e.g. using http://senecajs.org/