40
  • Did I understand correctly: If I use cluster package, does it mean that a new node instance is created for each created worker?

  • What is the difference between cluster and worker_threads packages?

1 Answer 1

71

Effectively what you are differing is process based vs thread based. Threads share memory (e.g. SharedArrayBuffer) whereas processes don't. Essentially they are the same thing categorically.

cluster

  • One process is launched on each CPU and can communicate via IPC.
  • Each process has it's own memory with it's own Node (v8) instance. Creating tons of them may create memory issues.
  • Great for spawning many HTTP servers that share the same port b/c the master main process will multiplex the requests to the child processes.

worker threads

  • One process total
  • Creates multiple threads with each thread having one Node instance (one event loop, one JS engine). Most Node API's are available to each thread except a few. So essentially Node is embedding itself and creating a new thread.
  • Shares memory with other threads (e.g. SharedArrayBuffer)
  • Great for CPU intensive tasks like processing data or accessing the file system. Because NodeJS is single threaded, synchronous tasks can be made more efficient with workers
5
  • For worker_threads: One process total. Does that mean spread across multiple V8 instances ?
    – joedotnot
    Jan 4, 2021 at 1:22
  • 8
    Each worker thread will have it's own V8 instance and Event Loop. This is how you gain parallelism. They call it a V8 isolate. Effectively, this is an independent instance of the V8 runtime that has its own JS heap and a microtask queue. Each Node.js worker thread can thus run it's code in isolation without sharing their heaps. Communication; however, occurs through a message channel between child and parent.
    – snewcomer
    Jan 5, 2021 at 14:21
  • 1
    @snewcomer Wouldn't worker threads involve in deadlock situations as they share memory? If yes, then wouldn't it slow down the CPU performance? May 2, 2022 at 0:47
  • Why do you strikethrough master ?
    – CDT
    Jul 16, 2022 at 11:34
  • 1
    @SarimJavaidKhan They do not share memory. data passed over channels is copied, but this is still way more efficient than JSON stringify/parse for IPC. Nov 3, 2022 at 5:15

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.