Skip to main content
18

Processes vs threads vs async/event loop

MediumConcurrencyThreadsAsync

What's the difference between processes, threads and async I/O (event loop), and when does each fit?

TL;DR

A process has its own memory space: strong isolation, but heavier, and it communicates through IPC. Threads live inside one process and share memory: cheap communication, but you need locks to avoid race conditions. Async I/O runs many tasks on one thread with an event loop: while one task waits on I/O, others run. It handles thousands of concurrent connections cheaply but stalls if any task does heavy CPU work. Rule of thumb: I/O-bound work fits async or threads; CPU-bound work needs real parallelism across cores (processes or threads without a global lock).

Key points

  • Concurrency is dealing with many things at once (interleaving); parallelism is doing them at the same time on multiple cores.
  • Node.js: one event loop for JavaScript, a libuv thread pool for some I/O, and worker_threads or cluster for CPU work.
  • Python: the GIL limits threads for CPU work, so use multiprocessing for CPU and asyncio or threads for I/O. (Free-threaded Python is arriving but not yet the norm.)
  • Go: goroutines are lightweight threads scheduled across cores, communicating via channels.
  • Java: virtual threads (Java 21+) give thread-per-request code at async-like scale.
  • Shared-memory hazards: race conditions, deadlocks (fix with a consistent lock order), and starvation.

Example

js
// Blocks the Node.js event loop: every other request waits
app.get('/hash', (req, res) => res.send(slowSyncHash(req.query.input)))

// Better: offload CPU work to a worker thread
app.get('/hash', async (req, res) => res.send(await workerPool.run(req.query.input)))

Common follow-ups

  • What's a race condition? The result depends on the timing of concurrent operations, such as two threads incrementing a counter without a lock and losing an update.
  • Why is Node.js good for I/O-heavy APIs? One thread handles many waiting connections without the memory cost of a thread per connection.

What interviewers look for

Tip

Memory isolation vs sharing, I/O-bound vs CPU-bound, and how your runtime of choice handles each.

  • Red flag: "async makes code run in parallel", or CPU-heavy work inside an event loop.

Was this helpful?