Skip to main content

How are threads different from processes?

1. What a process is

A process is a separate instance of a program, launched by the operating system.

Every process has:

  • its own memory (heap) and execution context,
  • its own global variables, environment, file descriptors,
  • its own address space, inaccessible to other processes directly.

Example: If you run three node app.js commands in a terminal, the OS creates three independent Node.js processes, unconnected to each other.

2. What a thread is

A thread is a unit of execution inside a process.

All threads within one process:

  • share memory (the heap, resources, modules),
  • run in parallel on different cores,
  • but each has its own call stack.

Example: When you create new Worker() via the worker_threads module, you're not starting a new process, you're creating a new thread inside the same Node.js process.

3. The key difference between processes and threads

CriterionProcessThread
Memory (heap)Isolated, each has its ownShared between threads
ExecutionIndependent, isolatedWithin one process
CommunicationVia IPC (slow)Via shared memory (SharedArrayBuffer, Atomics)
SafetyHigh isolation (an error doesn't affect others)An error can corrupt shared data
OS resourcesNeeds more memory and CPULightweight
StartupSlow (a new Node.js instance)Fast (within one process)
Node.js contextIts own event loop, its own modulesA shared process, but its own isolation
Node.js examplescluster, child_processworker_threads

4. Examples in Node.js

Processes (cluster, child_process)

javascript
const { fork } = require('child_process'); const child = fork('child.js'); child.on('message', msg => console.log('Response from process:', msg)); child.send('Hello');

Here:

  • a new Node.js process is created,
  • it has its own memory and event loop,
  • data exchange happens through IPC (inter-process communication),
  • an error in the child process won't take down the parent.

Threads (worker_threads)

javascript
const { Worker } = require('worker_threads'); const worker = new Worker('./task.js', { workerData: 42 }); worker.on('message', result => console.log('Result:', result));

Here:

  • a new thread is created inside the same process,
  • memory is shared (through SharedArrayBuffer),
  • modules and the environment are shared,
  • it runs faster, but an error can affect the whole process.

5. An analogy

SituationWhat it illustrates
ProcessesDifferent apartments in a building, each with its own rooms, kitchen, bathroom. Nothing shared.
ThreadsRooms in one apartment, shared walls and utilities, but each lived in separately.

6. When to use which

ScenarioBetter choice
You need isolation and stabilityProcesses (cluster, child_process)
You need fast exchange and shared memoryThreads (worker_threads)
Many CPU cores, heavy computationThreads or processes, depends on the task
You need resilience (an error in one shouldn't break the rest)Processes
Microservices, APIs, fork-based launchesProcesses
Algorithms, parsing, encryptionThreads

7. How Node.js uses them under the hood

  • The main JavaScript code in Node.js runs on one thread (the main thread).
  • All I/O operations (reading files, network, etc.) run in the thread pool (libuv's thread pool).
  • When needed, you can manually create new threads (worker_threads) or new processes (cluster) to scale.

Summary

ParameterProcessThread
IsolationFullPartial
MemorySeparateShared
CommunicationSlow (via IPC)Fast (via shared memory)
An error in one affects othersNoYes, it can
PerformanceHeavierLighter
Node.js examplecluster, child_processworker_threads
Good forIsolated tasks, APIs, microservicesCPU-heavy tasks inside a single app

Conclusion:

Threads are "light" units of execution inside one process. Processes are "heavy", fully isolated program instances.

In Node.js, threads (worker_threads) are used for parallel computation, while processes (cluster, child_process) are used for scaling and isolation.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.