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
| Criterion | Process | Thread |
|---|---|---|
| Memory (heap) | Isolated, each has its own | Shared between threads |
| Execution | Independent, isolated | Within one process |
| Communication | Via IPC (slow) | Via shared memory (SharedArrayBuffer, Atomics) |
| Safety | High isolation (an error doesn't affect others) | An error can corrupt shared data |
| OS resources | Needs more memory and CPU | Lightweight |
| Startup | Slow (a new Node.js instance) | Fast (within one process) |
| Node.js context | Its own event loop, its own modules | A shared process, but its own isolation |
| Node.js examples | cluster, child_process | worker_threads |
4. Examples in Node.js
Processes (cluster, child_process)
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)
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
| Situation | What it illustrates |
|---|---|
| Processes | Different apartments in a building, each with its own rooms, kitchen, bathroom. Nothing shared. |
| Threads | Rooms in one apartment, shared walls and utilities, but each lived in separately. |
6. When to use which
| Scenario | Better choice |
|---|---|
| You need isolation and stability | Processes (cluster, child_process) |
| You need fast exchange and shared memory | Threads (worker_threads) |
| Many CPU cores, heavy computation | Threads or processes, depends on the task |
| You need resilience (an error in one shouldn't break the rest) | Processes |
| Microservices, APIs, fork-based launches | Processes |
| Algorithms, parsing, encryption | Threads |
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
| Parameter | Process | Thread |
|---|---|---|
| Isolation | Full | Partial |
| Memory | Separate | Shared |
| Communication | Slow (via IPC) | Fast (via shared memory) |
| An error in one affects others | No | Yes, it can |
| Performance | Heavier | Lighter |
| Node.js example | cluster, child_process | worker_threads |
| Good for | Isolated tasks, APIs, microservices | CPU-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 readyA concise answer to help you respond confidently on this topic during an interview.