Concept

Threading Model

Definition

A threading model is the architectural decision about how a program maps units of work — requests, messages, market ticks, simulation steps — onto operating-system threads. Four canonical models recur. Single-thread keeps everything on one thread and serialises work through a queue. Thread-per-request spawns or reuses a thread for each incoming unit. An async event loop runs cooperatively scheduled tasks on one thread, yielding at I/O boundaries. The actor model gives each logical entity its own mailbox and worker, isolating mutable state behind message passing.

The right choice depends on the workload's I/O-to-CPU ratio, on the latency budget per unit, on how much state must be shared, and on the language's runtime — Python's GIL, Java's preemptive scheduler, and JavaScript's single-threaded loop each tilt the answer differently.

Why it matters

How it works

Choosing well starts with measuring where the program waits. If most time is spent in network or disk I/O, an async event loop or a small thread pool with non-blocking syscalls delivers high concurrency with minimal context-switch overhead. If the program is genuinely CPU-bound, async buys nothing — the work needs separate cores, which on Python means subprocesses and on most other languages means real threads. If multiple writers touch shared state, an actor or single-threaded model removes a class of bugs by making concurrent mutation impossible by construction; the same architecture in a thread-per-request setting would require locks at every boundary.

The interaction with state matters as much as the raw choice. A state machine running on a single dedicated thread can be made deterministic and replayable — a property invaluable for trading systems, simulators, and debuggable services. The same state machine running across a thread pool with shared mutable state would be neither. As a default for new services, start with the simplest model that meets the latency budget, measure the actual bottleneck under load, and graduate to a more complex model only when the data demands it.

Where it goes next

Continue exploring

Tags