What it is
Concurrency is the number of requests (or tasks) a system has in progress at the same time. It is not the same as parallelism, which is how many are actually executing on hardware at the same instant: a server with 4 cores can have 1,000 requests in progress, of which at most 4 are running on a CPU while the rest wait for a database, a network reply or a lock.
Every service has concurrency limits: thread pools, connection pools, in-flight request limits, worker counts, and shared resources that only one request can use at a time.
Why it exists
Most backend requests spend most of their time waiting: on databases, other services, disks. If a server could only work on one request at a time, it would sit idle during every wait. Concurrency lets it overlap those waits. How a runtime implements concurrency (threads, event loops, coroutines) determines how cheap each waiting request is, and therefore which limit you hit first.
How it works
Slots and hold time. By Little's law, a resource with N concurrent slots, each held for T seconds per request, completes at most N ÷ T requests per second. A pool of 4 threads, each held 103 ms per request, tops out near 39 req/s; with 32 threads near 310 req/s.
Blocking I/O (thread per request). The thread calls the database and is suspended until the answer arrives. It uses no CPU while waiting, but it is not available to anyone else. An I/O-bound service with blocking calls hits its thread limit while the CPU is mostly idle, as the lesson shows (threads 100%, CPU under 15%). Raising the pool helps up to the point where per-thread memory (each platform thread has its own stack, commonly about 1 MB reserved on the JVM and 8 MB of virtual address space by default on Linux), scheduler overhead, or the downstream system becomes the problem.
Non-blocking I/O. Event-loop runtimes (Node.js, Netty, Python asyncio, Rust's tokio) and green or virtual threads (Go goroutines, Java 21+ virtual threads) let a waiting request park as a small heap object; a handful of OS threads, typically one per core, run whichever requests are ready. In-flight requests become cheap, so concurrency is limited by memory and by explicit limits you set. The lesson's async step shows 300 req/s with about 38 requests in flight on 4 cores. The catch: anything that blocks an event-loop thread (CPU-heavy code, a blocking library call) stalls every request on that loop; virtual threads can be pinned to their carrier thread by some blocking operations.
CPU-bound work. If a request needs 10 ms of CPU, 4 cores complete at most 400 req/s. More threads or coroutines than cores do not raise that; they only lengthen the run queue and add context switches. The levers are less CPU per request, more cores, or more machines.
Contention. When requests need exclusive access to something shared (a row lock, a mutex, a single-writer partition, a global counter), that critical section is a stage with concurrency 1. Its throughput is 1 ÷ hold time, and requests queue for it no matter how many threads or cores exist. This is the practical face of Amdahl's law: the serialised fraction bounds the speedup. Cures: hold the lock for less time (no network calls inside transactions, atomic operations instead of read-modify-write), split the shared state (striped counters, sharding, per-partition ownership), or avoid sharing (immutable data, per-request state, commutative updates merged later).
Limits are also protection. A concurrency limit in front of a fragile dependency (a connection pool, a bulkhead, an in-flight limit) keeps a slow dependency from consuming every thread or exploding memory. Unlimited async concurrency can overwhelm a database just as surely as too many threads.
When to use it
- Sizing thread pools, connection pools and in-flight limits from rate × hold time, plus headroom.
- Choosing a runtime model: async or virtual threads for high-concurrency I/O-bound services (gateways, chat, streaming, fan-out to many APIs); simpler blocking code with a bounded pool when concurrency needs are modest.
- Diagnosing throughput plateaus where CPU is low (thread or pool exhaustion) or where adding instances does not help (contention on shared state).
When not to
- Do not add concurrency to CPU-bound work beyond the core count; add cores or reduce work.
- Do not raise pool sizes to "fix" a slow dependency: more concurrent requests against a saturated database make every query slower.
- Do not rewrite a working blocking service to async just for fashion; if threads are not the binding limit, it buys nothing and adds complexity.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Thread per request (blocking) | Simple, debuggable sequential code | Memory and scheduling cost per waiting request; thread limits |
| Event loop / async | Very high concurrency for I/O-bound work | Coloured functions (async/await everywhere), one blocking call stalls the loop |
| Virtual threads / goroutines | Blocking-style code with cheap waiting | Runtime support; pinning pitfalls; still need explicit limits on downstream concurrency |
| Large pools | Fewer rejections under bursts | More concurrent load on dependencies, more memory |
| Fine-grained locks / striping | Less contention | More complex code, harder invariants |
Alternatives
- Horizontal scaling: more processes or machines multiply every limit, except shared contention points (one database row stays one row).
- Queues and asynchronous processing: move slow work out of the request path so request handlers hold slots briefly.
- Batching: amortise a contended operation over many requests (one lock acquisition per batch).
Failure modes
- Thread pool exhaustion: a slow dependency makes every thread wait; the service stops answering even health checks while CPU is idle.
- Unbounded async fan-out: thousands of concurrent calls overwhelm a downstream service or exhaust memory.
- Blocking the event loop: one CPU-heavy or blocking call stalls every request on that loop.
- Hot lock or hot row: throughput flatlines at 1 ÷ hold time; adding instances adds waiters, and lock waits turn into timeouts.
- Deadlocks when several locks are taken in inconsistent orders.
In production
Expose and alert on pool utilisation and wait time (thread pools, connection pools, semaphores), event-loop lag for async runtimes, and lock wait time in the database. Size pools from Little's law with measured hold times. Put explicit concurrency limits (bulkheads) in front of each dependency. Keep transactions short and free of network calls. When throughput plateaus, check in this order: are slots full (threads, connections)? are cores saturated? is something waiting on a lock?
Common misconceptions
- "More threads means more throughput." Only while threads are waiting rather than computing, and only up to the next limit.
- "Async is faster." Each request is not faster; waiting is cheaper, so more requests fit.
- "Concurrency and parallelism are the same." Concurrency is in progress at once; parallelism is executing at once.
- "Scaling out fixes lock contention." A shared row or global lock admits one holder however many servers compete for it.
Interview angle
Expect questions like "how many threads does this service need?" (rate × latency, plus headroom), "why is CPU at 10% but the service is timing out?" (blocked threads or pool exhaustion), "why would you choose an async framework or virtual threads?" (I/O-bound, high concurrency), and "how would you implement an inventory counter at high write rates?" (avoid a single hot row: striping, atomic operations, reservations, or partitioned ownership). Naming the three kinds of limit (slots, compute, contention) and giving numbers stands out.
Model assumptions
The simulation is a simplified model, not a benchmark. The engine has no CPU model, so the application is drawn as three simulated stages a request visits while it holds one app slot: the slots themselves (threads, or in-flight requests in async mode), a "CPU" stage with 4 workers and 10 ms per request, and the payments API (80 ms plus 5 ms each way, practically unlimited concurrency). A slot is held while the request is on the CPU and while it waits, which is what a blocking thread does; in async mode the slot count is simply very large, and waiting costs nothing in the model. Context-switch overhead, per-thread memory and event-loop blocking are not modelled; they are described in the text. The stock row is a database node with 1 connection (4 with striping), 4 ms per update and a 300 ms wait limit. Arrivals are Poisson; service times are lognormal (cv 0.3). Utilisation is the busy fraction of slots, not CPU percentage. Numbers are illustrative.