System design concepts · Compute and concurrency

Process vs Thread

Short answer

A process is a running program with its own virtual address space, open files and other operating system resources. A thread is a path of execution inside a process; every process has at least one, and all threads of a process share its memory and file descriptors while keeping their own stack and registers. Processes are isolated from each other, so a crash in one does not corrupt another, and they talk through inter-process communication such as pipes, sockets or explicitly shared memory. Threads are cheaper to create and to switch between, and they talk by reading and writing the same memory, which is fast but needs locks or atomics to stay correct. Reach for processes when you need isolation and fault containment, and for threads when you want cheap concurrency over shared state.

Read the video transcript

A process and a thread both run your code. The difference is what they share. A process gets its own address space. Here are two processes, each with its own copy of a counter. When one changes it, the other never sees the change. That isolation is the point. If one process crashes, the other keeps running. To talk, they need a pipe, a socket, or memory they share on purpose.

Threads live inside one process. Each thread has its own stack and registers, but they all share the heap. So two threads can both read the counter at 41, both add one, and both write back 42. One update is lost. That is a race condition. A lock fixes it. One thread holds the lock, the other waits its turn, and the counter correctly reaches 43.

And because threads share everything, one bad pointer in any thread can take down the whole process. So, use processes for isolation, like PostgreSQL with a process per connection, or a browser isolating sites. Use threads to share memory cheaply, like a Java server with a thread pool. Isolation, or cheap sharing. That is the trade.

Process vs Thread: comparison table

AspectProcessThread
MemoryOwn virtual address spaceShares the address space of its process; own stack and registers
Creation costHigher: new address space and page tables (fork uses copy-on-write to soften this)Lower: a stack and a kernel scheduling entity
Context switchSwitches address space, which also disturbs the TLB and cachesCheaper between threads of the same process, since memory mappings stay the same
CommunicationPipes, sockets, message queues, shared memory segmentsShared variables, guarded by mutexes, atomics or channels
Failure isolationA crash usually stays inside the processA segfault or fatal error in one thread takes down the whole process
Security boundaryYes: the OS enforces separation and can apply different privilegesNone between threads of the same process
Real examplesPostgreSQL forks a backend process per connection; browsers isolate sites in renderer processesJava servers run requests on a thread pool; MySQL serves each connection on a thread
Two threads increment a shared counter: without a lock one update is lost, with a lock both land.

When to use each

Use processes when

  • You run untrusted or crash-prone code and one failure must not take the others down.
  • Parts of the system need different privileges, memory limits or restart policies.
  • Your runtime cannot run threads in parallel, such as the standard CPython build with its global interpreter lock, and the work is CPU bound.
  • You want to deploy, scale and restart pieces independently.

Use threads when

  • Tasks share large in-memory state such as caches, connection pools or a model loaded once.
  • You need fast hand-offs between tasks without serializing data across a process boundary.
  • The work is CPU bound and the language runs threads truly in parallel (Java, C++, Go, Rust).
  • You are serving many concurrent requests inside one service instance.
The verdict

Use processes when

You run untrusted or crash-prone code and one failure must not take the others down.

Otherwise

Use threads when

Tasks share large in-memory state such as caches, connection pools or a model loaded once.

A crash in one process leaves its neighbours running; a crash in one thread takes down every thread in the process.

Where green threads and event loops fit

Many runtimes add a lighter layer on top of operating system threads. Go schedules goroutines onto a small pool of OS threads, and each goroutine starts with a stack of a few kilobytes, so a server can hold far more goroutines than it could hold OS threads. Node.js and Python asyncio go further and run many tasks on a single thread, switching only when a task waits on I/O.

These are still threads or processes underneath. The question an interviewer cares about is the same: how much memory does each in-flight request cost, what happens when one of them blocks, and what is shared between them.

Why the database connection model matters

PostgreSQL uses a process per client connection. That gives strong isolation, but each connection carries real memory overhead, which is why busy PostgreSQL deployments put a pooler such as PgBouncer in front and keep the server-side connection count modest. MySQL serves connections on threads instead, which makes each connection cheaper. Knowing this lets you explain why a fleet of stateless app servers should share a connection pool rather than each opening hundreds of connections.

PostgreSQL starts a process per connection while MySQL runs a thread per connection, so each Postgres connection costs more memory.
Interview angle

How it comes up in a system design interview

You will rarely be asked to define a process in a system design round. The distinction shows up when you size a server, choose a worker model, or explain why a component falls over. Interviewers listen for whether you know what each in-flight unit of work costs and what a single failure takes down with it.

Where you will meet it

  • How many concurrent connections can one gateway host hold, and does that change with a thread per connection versus an event loop?
  • How should a crawler or job runner structure its workers: one process with many threads, many processes, or both?
  • Why does the database need a connection pooler when the app tier scales out?
  • In an operating system task scheduler, what state does the scheduler keep per process and per thread?
How a strong answer sounds
“Each fetcher host runs a few worker processes, one per core, and each process runs an async event loop with a few hundred in-flight requests. Processes give us isolation if a parser crashes on a bad page, and the event loop keeps memory per request small because most of the time is spent waiting on the network.”

Common mistakes

  • Saying threads are faster than processes. They are cheaper to create and switch, but a thread does not compute any faster.
  • Forgetting that one thread crashing usually kills the whole process, including every request it was serving.
  • Assuming more threads always means more throughput. Past the number of cores, CPU-bound threads only add switching and contention.

Frequently asked questions

Can a process have multiple threads?

Yes. Every process starts with one thread, and it can create more. All of them share the process memory and open files, and each has its own stack, registers and program counter.

What happens to other threads if one thread crashes?

In most runtimes a fatal error such as a segmentation fault terminates the entire process, so every thread in it dies. Some managed runtimes let an uncaught exception end only the thread that threw it, but shared state may be left inconsistent.

Is multiprocessing or multithreading better for a web server?

Most production servers use both. A common layout is one worker process per CPU core for isolation and parallelism, with threads or an event loop inside each worker to handle many concurrent requests.

Do threads run in parallel?

Threads of one process can run at the same time on different cores. The exception is a runtime with a global lock, such as the standard CPython build, where only one thread executes Python bytecode at a time.

Learn and practise

Lessons

Problems where this choice matters

Knowing the trade-off is half of it

Practise saying it under follow-up questions: a timed system design interview with an AI interviewer that probes your choices and grades against a rubric.

Start a mock interview