Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path →
Process vs thread: isolated, or shared memory?
Both let a program do more than one thing at a time, but they differ on the single most consequential question in systems: do they share memory? A process gets its own private world — safe, but expensive to talk across. Threads share one world — fast to coordinate, until two of them touch the same thing at the same time.
The one distinction that decides everything
A process is an independent running program with its own isolated memory space — one process cannot read or corrupt another’s memory, and they communicate only through explicit channels (pipes, sockets, shared-memory segments). A thread is a unit of execution inside a process, and all threads in a process share the same memory — the same heap, the same globals, the same open files. That one difference — isolated versus shared memory — cascades into everything: how expensive they are to create, how they communicate, and what happens when one of them crashes.
Want isolation, fault tolerance, or true multi-core parallelism (especially where a runtime lock limits threads)? Reach for processes. Want cheap, fast, shared-memory concurrency where the tasks cooperate closely? Reach for threads. Isolation vs shared state is the whole trade.
Head to head
| Dimension | Process | Thread |
|---|---|---|
| Memory | Own private, isolated space | Shared with sibling threads |
| Isolation | Strong — can’t touch others’ memory | None — shares everything |
| Creation cost | Heavy (new address space) | Light (within an existing one) |
| Context-switch cost | Higher (swap address space, TLB) | Lower (same address space) |
| Communication | IPC — pipes, sockets, shared mem (serialize) | Direct — just read/write shared memory |
| If one crashes | Only that process dies | Can take down the whole process |
| Synchronization | Rarely needed (isolated) | Required — locks, races, deadlocks |
| Multi-core parallelism | Yes, always | Yes — unless a runtime lock (e.g. Python GIL) blocks it |
When to use each
Reach for processes
- You need isolation — a crash or bug must not spread
- Security boundaries (sandbox untrusted work in its own process)
- CPU-bound parallelism, especially under a GIL (Python)
- Independently deployable/restartable workers
- When simplicity of "no shared state" beats communication cost
Reach for threads
- Tasks that share a lot of data and coordinate closely
- IO-bound work (waiting on network/disk) needing concurrency
- Low creation/switching overhead matters (many short tasks)
- A responsive UI thread plus background workers
- When cheap, direct shared-memory communication is a win
The answer is usually a mix (plus a third option)
Real systems combine them. A common shape: run several worker processes for isolation and true multi-core parallelism (one crashing worker doesn’t kill the rest), and within each worker use threads (or async) to handle many concurrent IO-bound connections cheaply. This is exactly how many production web servers are structured — a pool of processes, each running an event loop or thread pool. There’s also a third concurrency model that sidesteps both: a single-threaded event loop (async/await) that interleaves many IO-bound tasks on one thread with no locks and no context-switch cost — great for IO-bound work, useless for CPU-bound work (which still wants processes).
IO-bound and cooperative? Threads or async — cheap and shared. CPU-bound, needs isolation, or fighting a runtime lock? Processes. Big systems layer them: processes for isolation and cores, threads/async for concurrency within.
The shared-memory double edge (and the GIL)
Threads’ shared memory is their superpower and their curse. Communication is nearly free — one thread writes a variable, another reads it, no serialization, no copying. But that same sharing is why threads need synchronization: two threads touching the same data at once cause race conditions, so you add locks — and locks bring their own hazards (contention, and deadlock if two threads grab locks in different orders). Processes avoid all of this by construction: with isolated memory there’s nothing to race on, but you pay for communication with IPC and serialization overhead. You’re choosing between "fast to talk, dangerous to share" and "safe by isolation, costly to talk."
One infamous wrinkle: in CPython, the Global Interpreter Lock (GIL) lets only one thread execute Python bytecode at a time, so multithreading does not give you CPU parallelism in pure-Python code — threads still help IO-bound work (the GIL is released during IO), but CPU-bound Python needs multiprocessing to actually use multiple cores. This is why "use processes for CPU-bound work" is near-gospel in Python, while languages without a GIL (Java, Go, C++, Rust) get real multi-core parallelism from threads directly.
Threads trade safety for cheap, direct communication (and demand locks). Processes trade communication cost for isolation and guaranteed multi-core parallelism. The GIL tilts Python hard toward processes for CPU-bound work.
A worked scenario: a web server
You’re serving thousands of simultaneous HTTP requests. Handling each in its own process would be safe but wasteful — processes are heavy to create and switch, and thousands of them would exhaust memory. Handling all of them as threads in one process is cheap and lets them share caches directly, but one bad request that segfaults or leaks could take down every connection at once, and in Python the GIL would cap CPU throughput anyway.
So production servers layer both: a small pool of worker processes (one per core, for isolation and true parallelism — a crash kills one worker, and the manager restarts it without dropping the others), and within each worker, threads or an async event loop to juggle many IO-bound connections concurrently at low cost. Processes give you fault isolation and cores; threads/async give you cheap concurrency. Neither alone is right — the architecture uses each for what it’s good at.
Need isolation, security, or CPU parallelism (or you’re in Python) → processes. Need cheap, closely-coordinated, IO-bound concurrency → threads (or async). Most serious systems run processes on the outside and threads/async on the inside.
The picture: who can see whose memory
Cost and trade-offs, operation by operation
No timings here, because they vary by kernel and hardware. Time is the work the operation does. Space is the memory it takes or copies.
| Case | Time | Space | Why |
|---|---|---|---|
Create a process (Linux fork) | Copies the page tables, not the pages | Pages are shared copy-on-write and duplicated only when one side writes | Cheaper than it sounds, until the child or parent writes to most of its memory. |
Create a process (Python spawn, the default on macOS and Windows) | Starts a fresh interpreter and re-imports your main module | A whole interpreter per worker | Why worker code must sit under if __name__ == "__main__":, and why pools are created once and reused. |
| Create a thread | No new address space; the kernel adds a schedulable task | A stack, commonly 8 MB of reserved virtual memory on Linux | 1,000 threads reserve about 7.8 GiB of address space, but only the pages they touch become real memory. |
| Switch between threads of one process | Saves and restores registers | Nothing extra | The address space and its TLB entries stay valid. |
| Switch between processes | Registers plus a switch of page tables | Nothing extra | Cached address translations from the old process stop helping. |
| Share data, threads | A read or write, plus a lock for anything shared and mutable | One copy | Fast to share; every unguarded shared write is a possible race. |
| Share data, processes | Serialize, copy through the kernel, deserialize | Two copies, plus the encoded form in transit | Shared-memory segments avoid the copy and bring the locking problem back. |
| CPU-bound pure-Python code, 8 cores | Threads: about one core’s worth under the GIL. Processes: all 8 | Processes: 8 interpreters | CPython 3.13 adds an optional free-threaded build without the GIL; the default build still has it. |
| An async task | Switches only at an await, in user space | A small object per task, no stack of its own | Cheapest of all for waiting on I/O. One blocking call stalls every task on the loop. |
| One worker crashes | Thread: a segfault ends the whole process and every thread in it. Process: only that one exits | Process: the parent sees the exit status and can start a replacement | This is how pre-fork servers survive a bad request: a supervisor restarts the dead worker. |
| Stopping a stuck worker | Process: send it a signal; the kernel reclaims its memory, files and sockets. Thread: most runtimes give no safe way to kill one (Python has no API for it) | A killed thread could leave a lock held or data half-written | Work you may need to cancel by force belongs in a process, or must check a stop flag itself. |
| Forked Python workers sharing memory | Reading an object updates its reference count, which writes to its page | Copy-on-write pages get duplicated even though your code only reads | Shared memory erodes over a worker’s life. gc.freeze() (Python 3.7+) before forking keeps the garbage collector from touching the inherited objects, which helps. |
The pattern: processes pay up front (creation, copying, an interpreter each) and get isolation and every core. Threads pay later, in locks and in bugs, and get cheap sharing. Async is cheaper still for I/O, at the price of never blocking. That is why the page’s web server ends up with all three: processes per core, and threads or an event loop inside each. The last three rows add the operational side. A process can crash, be killed and be replaced without harming its neighbours, which is what a supervisor needs. A thread cannot be stopped from outside without risking a held lock. So when you are unsure, ask what happens when one unit of work goes wrong. If the answer must be “only that unit fails”, the work belongs in a process.
Common mistakes
- Forking a process that already has threads
The child gets a copy of all memory but only the thread that called
fork. A lock another thread held at that moment is copied in its locked state, and nobody in the child will ever release it, so the child hangs on its firstmallocor log call. Fork before starting threads, or use a spawn-style start. - Assuming counter += 1 is atomic
It is a read, an add and a write. Two threads can both read 41 and both write 42. This happens in Python too: the GIL keeps the interpreter consistent, not your counter. Guard it with a lock, or give each thread its own count and add them at the end.
- One thread or process per task, with no cap
A burst of 10,000 requests becomes 10,000 threads or processes, each with its own stack or interpreter, and the machine spends its time switching and swapping. Use a fixed pool sized to cores for CPU work, and to the downstream’s limits for I/O.
- Shipping big data to every worker
Each task sent to a process pool is serialized and copied. Passing a 500 MB array with every task can cost more than the work itself. Send a file path, an index range or a shared-memory name, and let the worker read the data where it lives.
- Blocking inside an event loop
One synchronous call (a
time.sleep, a blocking database driver, a big CPU loop) inside an async handler freezes every other task on that loop, because the loop only switches at anawait. Move the call to a thread or process pool, or use an async client.
Which would you pick?
Three situations. Pick the side you'd actually build — the explanation follows.
A CPU-bound numeric workload in Python that must use all 16 cores.
The GIL means only one thread executes Python bytecode at a time, so 16 CPU-bound threads take turns on one core's worth of work no matter how the task is divided. Processes each get their own interpreter and genuinely run in parallel — the page notes this is what tilts Python specifically.
A server handling thousands of concurrent connections that spend nearly all their time waiting on network IO.
IO-bound work releases the CPU while waiting, so the GIL is not the constraint and the cheap shared-memory option fits. A process per connection pays full memory and context-switch cost thousands of times over to isolate work that is mostly idle.
A plugin system running untrusted third-party code inside your application.
Threads share one address space, so untrusted code can read and corrupt everything in it and a crash takes the whole process down. Validation constrains inputs, not what code does once running, and being IO-bound is a performance property with no bearing on the trust boundary.
Quick answers
What's the difference between a process and a thread?
A process is an independent program with its own isolated memory space; a thread is a unit of execution inside a process that shares the process’s memory with sibling threads. Processes are isolated (safe, but expensive to create and they communicate via IPC), while threads share memory (cheap and fast to communicate, but they require synchronization and one thread crashing can take down the whole process).
Which is faster, a process or a thread?
Threads are cheaper to create and switch between, and they communicate faster because they share memory directly rather than copying data through IPC. That makes threads lighter-weight for closely-cooperating, IO-bound work. Processes cost more per operation but buy isolation and guaranteed multi-core parallelism, which threads can’t always provide (for example under Python’s GIL).
Why does Python use multiprocessing for CPU-bound work?
Because of the Global Interpreter Lock (GIL): in CPython only one thread can execute Python bytecode at a time, so multithreading does not give CPU parallelism for pure-Python code. Threads still help IO-bound work (the GIL is released during IO), but to actually use multiple cores for CPU-bound Python you use multiprocessing, which runs separate processes each with their own interpreter and GIL.
When should I use processes over threads?
Use processes when you need isolation (a crash or bug must not spread), a security sandbox for untrusted work, true multi-core parallelism (especially under a runtime lock like the GIL), or independently restartable workers. Use threads when tasks share a lot of data and coordinate closely, the work is IO-bound, and cheap creation and direct shared-memory communication matter.
Process vs Thread: your own house, or a shared one
Explore the topic
See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.
More Handbooks
- The System Design Fundamentals HandbookThe load-bearing ideas behind every distributed system — the CAP theorem and consistency models, concurrency and locking, partitioning and replication, and consensus and coordination — each tied to a real worked design you can study interactively.Read →
- CAP Theorem & Consistency ModelsWhat a distributed system can promise when the network splits — CP vs AP, why "CA" is a myth, PACELC, and the full spectrum from linearizable to eventual consistency. Part of System Design Fundamentals.Read →
- Partitioning, Sharding & ReplicationHow one dataset becomes many — range vs hash partitioning, consistent hashing and virtual nodes, hot partitions, replication topologies, replication lag, and quorums. Part of System Design Fundamentals.Read →
- Consensus, Transactions & CoordinationHow nodes that can crash still agree on one truth — majority quorums, Raft and Paxos, leader election, two-phase commit vs the saga pattern, and idempotency for exactly-once effects. Part of System Design Fundamentals.Read →
- The Kubernetes HandbookThe one idea under all the YAML — declare desired state, and a control loop makes reality match. Covers the orchestration problem, pods, deployments and replicasets, services and networking, the reconciliation loop and self-healing, the scheduler, config/secrets and health probes, autoscaling, and when you actually need Kubernetes.Read →
- The Observability HandbookSeeing inside production — monitoring vs observability, the three pillars (metrics, logs, traces) and what each answers, structured logging, metric types and the cardinality trap, distributed tracing, the golden signals and SLIs/SLOs/error budgets, alerting on symptoms not causes, and correlating all three during an incident.Read →