Node.js Internals8h estimated

Child Processes & Worker Threads

Difficulty
Importance

The problem

Node's single-threaded event loop is excellent for I/O-bound work but has no good answer for CPU-bound work — a heavy computation (image processing, complex parsing) run directly on the main thread blocks the event loop entirely, freezing every other request the server is handling.

Why now

Processes and threads already established the OS-level distinction between full process isolation and lighter-weight shared-memory threads; Node exposes both explicitly, because the event loop's single-threaded model means CPU-bound work has to be deliberately moved off it using one of these two OS primitives, not hidden away automatically.

Mental model

A child process is a fully separate Node instance — its own memory, its own event loop, communicating with the parent only via message passing (the interprocess communication already covered). A worker thread is lighter: it shares the process but gets its own JavaScript engine instance and event loop, communicating via message passing too, since JavaScript's memory model doesn't allow arbitrary shared mutable objects between threads the way some other languages do.

Requires

Unlocks

Used in

Projects

  • Run a CPU-intensive synchronous computation directly on the main thread and observe the server become unresponsive to other requests, then move it to a worker thread and confirm responsiveness is restored
  • Use child_process to run an external command and correctly handle its stdout, stderr, and exit code

Examples

  • A synchronous, CPU-heavy JSON.parse of a huge file blocks every other request on the server until it finishes — moving that parse to a worker thread frees the main thread to keep serving requests
  • child_process.exec('ls -la') runs a shell command and returns its output asynchronously via a callback, without blocking the event loop while the external process runs

Resources

Mastery checklist