Threadsthreadsasyncevent loopprocessesconcurrency

Threads versus Async versus Processes

There are three ways to have many things in flight — a thread per task, an event loop that parks tasks while they wait, or a process per task — and the choice is decided by whether the work is CPU-bound or I/O-bound, how many tasks you need, and what it costs to have one of them fail.

ConceptualRuntime-specific
Interview question
Progress

Builds on Processes in the OS roadmap.

The problem

A service must keep 10,000 client connections open, run a CPU-heavy image transform for a few of them, and never let one bad request take the rest down. No single concurrency strategy does all three well. What does each one actually cost, and where does each one win?

Three strategies for one problem

The problem is always the same: you have more logical tasks than cores, and most tasks spend most of their time waiting. Thread per task hands each task a kernel thread; when it blocks, the kernel parks it and runs another. Async (an event loop) keeps one thread per core, never blocks it, and expresses each task as a chain of callbacks or an async function that the runtime resumes when its I/O completes. Process per task is thread-per-task with isolation: each task gets an address space, so it can crash, leak or be killed alone.

They are not exclusive. A modern server typically runs *processes* for isolation (one per core, or per tenant), each running an *event loop* for the I/O-bound majority, offloading the CPU-bound minority to a *thread pool*. Knowing which layer does what is the point of this lesson; The Event Loop and Async I/O: What `await readFile()` Actually Does cover the async mechanism in detail, and The Thread Pool Server and The Event-Driven Server build the server versions.

Concurrency is not parallelism

Concurrency is structure: many tasks in progress, interleaved. Parallelism is execution: many tasks running at the same instant on different cores. An event loop on one thread is fully concurrent (10,000 open connections, each mid-request) and has zero parallelism. Eight threads on eight cores are parallel. Threads on a single core are concurrent but not parallel — Concurrency versus Parallelism spells it out.

The distinction decides the strategy. I/O-bound work (waiting on sockets, disks, databases, other services) needs concurrency, not parallelism: the CPU is idle anyway, and what matters is how cheaply you can keep a task suspended. CPU-bound work (compression, image transforms, JSON parsing at scale, cryptography) needs parallelism: a task that never waits gains nothing from an event loop and everything from another core. An async runtime that receives a CPU-bound task stalls every other task on the loop for its duration — the single most common async performance bug.

  • I/O-bound: async or many threads; throughput is bounded by the remote system and by how many waits you can hold cheaply.
  • CPU-bound: threads or processes up to the core count; more than that is context-switch overhead.
  • Mixed: event loop for I/O, thread pool (or process pool) for CPU, with a queue between them.

Cost per unit

Conceptual

The table gives typical figures. Per-thread memory is dominated by the stack reservation — 8 MB virtual on glibc Linux, 1 MB on Windows, 512 kB for Rust’s default, with only the touched pages committed — plus the kernel’s task state. An async task is a heap object: a Promise and its closure in JavaScript (hundreds of bytes to a few kB), a coroutine frame in Python (~1–2 kB), a Future state machine in Rust (as small as tens of bytes). The 100× to 1,000× gap in memory and switching cost is why 10,000 kernel threads is a serious number and 1,000,000 async tasks is routine.

Typical cost per unit of concurrency (order of magnitude; measure on your platform)
Thread per taskAsync taskProcess per task
Memory per unit~MB virtual stack, ~10s of kB committed~100 B – few kB heap~MBs private data + page tables
Creation~10–50 µs~100 ns – 1 µs (allocate an object)~50 µs – 1 ms
Switch between units~1–5 µs (kernel)~10–100 ns (a function return / resume)~2–5 µs + TLB/cache cold
Practical ceiling~thousands to low tens of thousands~millions~hundreds to low thousands
Uses multiple coresyesno, per loop; run one loop per coreyes
A task blocks (sync call)only that threadthe whole looponly that process
A task crasheswhole processwhole process (loop)that process only
Code shapesequential, blocking callsasync/await or callbacks; colouringsequential; IPC to share

Where each wins

Threads win when tasks are CPU-heavy or call blocking libraries you do not control (a legacy C client, a driver with no async API), when the count is modest (a few hundred), and when sequential code is worth more than peak connection count. Thread pools bound the count and reuse the expensive creation. Async wins when the work is dominated by waiting and the count is large — proxies, chat servers, API gateways, anything holding thousands of idle sockets — and when a runtime already does it well (Node, asyncio, Tokio, Go’s scheduler, which is async underneath sequential-looking code). Processes win when isolation matters more than sharing: untrusted code, crash containment, per-tenant limits, or a runtime that cannot use cores otherwise (CPython in the default build).

The failure mode of picking wrong is asymmetric. Threads where async was needed gives a server that runs out of memory or scheduler capacity at a few thousand connections — the C10K problem, see C10K: Ten Thousand Connections, Then a Million. Async where threads were needed gives a server whose p99 latency is dominated by the longest CPU-bound task, because everything else waits behind it. Processes where threads were needed gives a system that spends its time copying data through pipes.

Mixing them correctly

Runtime-specific

The production shape is layered. Node runs its event loop on one thread and delegates file I/O, DNS and crypto to a libuv thread pool (4 threads by default); heavy CPU work goes to worker_threads or a separate process. Python’s asyncio offers loop.run_in_executor to push a blocking call to a thread pool or a ProcessPoolExecutor for CPU work. C++ servers pair an io_uring/epoll reactor per core with a work-stealing pool. Go hides the whole arrangement: goroutines are async tasks, the runtime multiplexes them over GOMAXPROCS kernel threads, and blocking syscalls are detected and moved off the loop.

Two rules keep the mix honest. Never call a blocking function on a loop thread — a synchronous file read, a sleep, a lock held across a slow section — because it stalls every task on that loop, and the symptom is latency with idle CPU (The Event Loop shows how to see it). And size the pools from the resource they consume: CPU pools at the core count, I/O pools at whatever the downstream can absorb, not "as many as possible".

Key points

  • Thread per task, async task on an event loop, and process per task are three answers to "more tasks than cores"; production systems layer all three.
  • I/O-bound work needs concurrency (cheap suspended tasks); CPU-bound work needs parallelism (more cores); an async loop given CPU-bound work stalls every other task.
  • A kernel thread costs a MB-scale stack reservation and microsecond switches; an async task costs kilobytes and nanosecond resumes — a 100–1,000× gap that sets the practical ceilings.
  • Threads and async share the process’s fate on a crash; only processes contain it.
  • Never block a loop thread; size CPU pools to cores and I/O pools to what the downstream can take.

Why does this exist?

Mechanisms are answers to constraints. Open each question before reading the answer.

▸Why does async exist if the OS already provides threads?

Because a kernel thread is too expensive to hold per idle connection: at 10,000 connections the stacks, the kernel state and the scheduler’s work dominate. Async makes the suspended task a small heap object the kernel never sees.

▸Why not make everything async, then?

Async gives no parallelism by itself and cannot suspend a CPU-bound computation or a blocking library call; those still need threads or processes.

▸Why keep processes around at all?

They are the only unit the kernel can isolate, limit and kill independently; every strategy that needs crash containment or untrusted code ends at a process boundary.

How it fails

What the failure looks like from inside real software.
Reference · show
  • A Node or asyncio service shows p99 latency of seconds with CPU at 30%: a CPU-bound task (JSON parse of a 50 MB body, sync compression) is blocking the loop.
  • A thread-per-connection Java service hits OutOfMemoryError: unable to create native thread at ~8,000 connections: stack reservations and the pids limit, not heap.
  • A CPython service uses threads for a CPU-bound batch job and gets no speed-up on 16 cores in the default build; the work needed a process pool.
  • A Go service stalls under load because a cgo call blocks a kernel thread without yielding; the runtime has to spawn threads to compensate and thread count balloons.
  • A thread pool sized at 500 for "throughput" on 8 cores makes latency worse: the CPU is oversubscribed and every task waits behind 60 others.

Check yourself

Answer out loud or on paper first, then compare. These come from the interview questions linked to this lesson.

  1. When would you choose async I/O over threads, and when the reverse? What does each cost?

    Show a strong answer

    Both are ways to have many tasks in progress. Threads give each task its own stack and let the kernel schedule it: blocking calls are fine, CPU-bound work runs in parallel (outside the GIL), and a slow task is preempted so it cannot hurt the others’ latency. The cost is per-thread state — an 8 MB virtual stack of which tens of KB are resident, a kernel task — plus context switches and the synchronization needed for shared data. Ten thousand threads is possible; a hundred thousand is where scheduler cost and memory become the ceiling (Threads versus Async versus Processes).

    Async multiplexes many tasks on one thread by never blocking it: every wait becomes a registration with epoll/kqueue/IOCP and a continuation. The per-task cost is a few hundred bytes to a few KB, so a million idle connections is feasible, and there are no data races between tasks on one loop. The price is cooperation — a CPU-heavy or accidentally blocking step stalls every task — and function colouring: everything in the call path must be async-aware, so one synchronous driver or library poisons the design (Async I/O: What `await readFile()` Actually Does).

    Choose async when tasks are numerous and mostly waiting: websocket fan-out, proxies, chat, long-polling, an API gateway. Choose threads (or processes) when tasks are CPU-bound, when you depend on blocking libraries, or when you need the kernel’s fairness so one heavy request cannot degrade all the others. The practical answer in most runtimes is the hybrid: one event loop per core for I/O, a bounded thread pool for CPU and blocking calls — the shape of Node.js, Tokio, .NET and nginx.

    Full interview question, with follow-ups and red flags →
  2. What does a context switch actually cost, and why is the answer "more than saving the registers"?

    Show a strong answer

    The direct work is: enter the kernel (a timer interrupt, a blocking syscall, or a wake-up of a higher-priority task), save the running task’s registers and stack pointer into its kernel-side state, run the scheduler to pick the next task, switch kernel stacks, and — if the next task belongs to a different process — load a new page-table root (CR3 on x86). Then restore and return. That is roughly 1–5 µs on a modern server, more if the scheduler has a long run queue to consider (Context Switching).

    The indirect cost is usually larger. The task that resumes finds the L1/L2 caches full of someone else’s lines and, if the address space changed, a TLB that has been flushed or whose entries belong to another process (PCID/ASID tagging lets the hardware keep them but they still compete for the same slots). The next few thousand memory accesses miss; the branch predictors are cold. A switch that "costs 2 µs" can degrade the next 50–100 µs of execution (The TLB).

    That is why the *kind* of switch matters. A switch between two threads of the same process does not change CR3, so no TLB flush. A system call is a mode switch, not a context switch: the same task continues in kernel mode, on the order of 100 ns–1 µs, though Spectre/Meltdown mitigations made it more expensive (System Calls). Green threads and goroutines switch in user space — a few hundred nanoseconds — because they skip the kernel entirely.

    Switches are also either voluntary (the task blocked) or involuntary (preempted). A process doing 50,000 involuntary switches a second on one core is being time-sliced against too many competitors; 50,000 voluntary switches a second usually means a lock or a tiny I/O pattern is bouncing threads between sleep and wake.

    Full interview question, with follow-ups and red flags →
  3. An async Python service uses a sync database driver. What happens under load?

    Show a strong answer

    Every query blocks the loop; concurrency collapses to one query at a time. Use an async driver or run_in_executor.

    Full interview question, with follow-ups and red flags →