1.

How are worker threads different from clusters?

Answer»

Cluster:

  • There is one process on each CPU with an IPC to communicate.
  • In case we WANT to have multiple servers accepting HTTP requests via a single PORT, clusters can be helpful.
  • The processes are spawned in each CPU THUS will have separate memory and node instance which further will lead to memory issues.

Worker threads:

  • There is only one process in total with multiple threads.
  • Each thread has one Node instance (one EVENT loop, one JS engine) with most of the APIs accessible.
  • Shares memory with other threads (e.g. SharedArrayBuffer)
  • This can be used for CPU-intensive tasks like processing data or ACCESSING the file system since NodeJS is single-threaded, synchronous tasks can be made more efficient leveraging the worker's threads.


Discussion

No Comment Found