Problem solving · should-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix

Process vs Thread vs Coroutine

mediumsystems-fundamentals

Problem

A Python service does three kinds of work: it resizes uploaded images (CPU-heavy, a few hundred milliseconds each), it calls five downstream HTTP APIs per request and combines the results (each 50 to 200 ms of waiting), and it handles thousands of idle WebSocket connections that occasionally receive a message.

For each workload, choose between processes, OS threads, and coroutines (async/await), and justify the choice in terms of what each unit costs, what it can run in parallel, how it shares memory, and how it fails. Then explain what changes if the service were written in Java or Go instead of Python.

  • Be concrete about per-unit memory, creation cost, and context-switch cost.
  • State precisely what Python's Global Interpreter Lock (GIL) serializes and what it does not.

Examples

  • Image resizing, 8 cores: 8 worker processes each handle a resize at full speed, giving about 8× throughput. 8 threads in one CPython process give about 1× because the GIL lets only one thread execute Python bytecode at a time, unless the resize library releases the GIL inside its C code, which Pillow and NumPy largely do.
  • Five API calls per request: sequentially, 5 × 100 ms = 500 ms. With 5 threads or 5 coroutines issuing them concurrently, about 100 ms, the slowest call. The GIL is irrelevant, because waiting on a socket releases it.
  • 10,000 idle WebSockets: 10,000 threads at 8 MB reserved stack each is 80 GB of virtual address space and thousands of context switches per second for nothing. 10,000 coroutines are 10,000 small heap objects on one thread, idle at zero cost until a socket becomes readable.

Hints

  1. Ask of each workload: is it waiting or computing? Only computing needs a second CPU; waiting needs a way to not block.
  2. What does an OS thread cost to create and to switch, and what does a coroutine cost? They differ by roughly two orders of magnitude.
  3. A process has its own memory and its own GIL. What does that buy, and what does it cost when the workers need to share state?

Last reviewed 8 September 2026