Skip to main content

What is IPC (inter-process communication)?

What IPC is

IPC (Inter-Process Communication) is a mechanism that lets processes exchange data with each other.

In other words:

if you have two (or more) processes, IPC gives them a "channel" so they can send and receive messages (as if they were "talking" over a wire).

1. Why IPC is needed

Every process:

  • runs in its own address space (its own memory, its own environment),
  • is isolated from others (for security reasons).

So they can't directly share variables or memory. To interact, they need a communication channel.

IPC solves this by letting processes:

  • pass data (text, JSON, buffers),
  • coordinate work (signals, events),
  • synchronize state.

2. Types of IPC (in general, not just in Node.js)

IPC typeDescriptionExample
PipeSequential data transfer (one writes, one reads)stdin/stdout
Message PassingPassing messages (objects) through system sockets or channelsNode.js process.send()
Shared MemoryShared memory for fast exchangeSharedArrayBuffer
SocketsUniversal network exchange between processesTCP/UDP
SignalsSimple OS signals (SIGINT, SIGTERM)process.kill(pid, 'SIGINT')
Files / Named PipesExchange via temp files / named channelsUNIX socket, FIFO

3. IPC in Node.js

In Node.js, IPC is built into:

  • child_process
  • cluster

When you create a process via:

javascript
const { fork } = require('child_process');

Node.js automatically creates an IPC channel between parent and child.

That lets you:

  • send messages (child.send(data));
  • receive them (process.on('message', handler)).

4. An IPC example in Node.js

main.js

javascript
const { fork } = require('child_process'); // Create a child process const child = fork('worker.js'); // Send a message child.send({ task: 'compute', number: 5 }); // Get the reply child.on('message', (msg) => { console.log('Reply from the child process:', msg); });

worker.js

javascript
process.on('message', (msg) => { if (msg.task === 'compute') { const result = msg.number ** 2; process.send({ result }); } });

What happens:

  • fork() creates an IPC channel between parent and child.
  • The parent sends the object { task, number }.
  • The child receives it via process.on('message').
  • After processing, it sends the result back via process.send().

That's Inter-Process Communication in action.

5. IPC in the cluster module

The cluster module also uses IPC under the hood:

  • masterworker talk through the same mechanism.
  • Messages can be passed manually:
javascript
// master worker.send({ msg: 'ping' }); // worker process.on('message', (data) => { console.log('Message from the master:', data); process.send({ reply: 'pong' }); });

So cluster uses IPC to exchange both internal events and user data.

6. How IPC works under the hood in Node.js

Node.js implements IPC through Unix Domain Sockets (on Linux/macOS) or Named Pipes (on Windows).

In other words:

a virtual socket is created between processes, and messages are serialized and sent over it.

These messages:

  • are serialized (for example, as JSON or a binary format),
  • travel through an internal channel (a pipe),
  • are deserialized on the receiving side.

7. IPC and data serialization

Through IPC, you can pass:

  • objects ({name: 'Tim'}),
  • strings, numbers, buffers,
  • even references to sockets or file descriptors.

Node.js automatically serializes objects on transfer:

javascript
child.send({ user: 'Alice', age: 25 });

But keep in mind:

  • functions or context references cannot be passed;
  • data is passed by value, not by reference.

8. Advantages of IPC

AdvantageDescription
Two-way communicationParent and child processes can exchange data
SecurityEach process is isolated, only the channel connects them
PerformanceIPC on one machine is faster than HTTP
UniversalityWorks between any Node.js processes (fork, cluster)
Fault toleranceAn error in one process doesn't break the others

9. Drawbacks of IPC

DrawbackDescription
Serialization/deserializationAdds latency when passing large objects
Memory isolationData can't be shared directly, only via copies
Synchronization complexityWith many processes, queues need managing
Not for large data streamsSockets or streams are a better fit

Summary

CriterionDescription
PurposeCommunication between processes
Implementation in Node.jsVia fork() and cluster
DirectionTwo-way (parent ↔ child)
Data typeJSON, strings, buffers
Under the hoodUnix Socket / Named Pipe
AdvantagesSafe, isolated, efficient
DrawbacksCan't share memory directly

Conclusion:

IPC (Inter-Process Communication) is the mechanism that lets processes in Node.js (or an OS in general) talk and pass data to each other without sharing memory.

In Node.js, IPC is used in the child_process and cluster modules, where processes exchange messages through a built-in communication channel.

Short Answer

Interview ready
Premium

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