Communication Costs: Shared Memory vs IPC
Shared Memory Communication
Threads communicate by reading and writing shared heap memory. One thread writes a value; another reads it. The cost is essentially memory access: ~100 nanoseconds for a cache miss, or 1-10 nanoseconds if data is in CPU cache.
However, shared memory requires synchronization. Without locks or atomic operations, two threads writing the same location create a race condition. An uncontended mutex costs 25-50 nanoseconds; a contended mutex can cost microseconds as threads wait.
Inter Process Communication
Processes cannot access each others memory directly. They use IPC (Inter Process Communication) mechanisms involving the kernel. Every IPC operation requires at least two context switches: one to enter the kernel, one to return.
Pipes and sockets copy data from sender to kernel buffer, then to receiver. Sending 1KB through a local socket takes 2-10 microseconds, roughly 100x slower than shared memory.
Shared memory regions let processes map the same physical memory into their address spaces. After setup, access is as fast as regular memory, but cross process synchronization primitives are needed.
Serialization Overhead
IPC often requires serialization: converting in memory structures to bytes. A 1KB message takes 1-5 microseconds with binary formats, or 10-50 microseconds with JSON.
The Performance Gap
For one million messages: threads with a lock free queue complete in ~100ms. Processes with sockets take ~10 seconds, a 100x slowdown.