Core Concepts
Concurrency vs Parallelism
Trace what can overlap while a request waits, what needs more compute, and where two tasks can race for the same work.
Concurrency lets tasks be in progress over the same period. If the first request waits for its database result asynchronously, the application thread can handle a second request in the meantime. Parallelism means executing work at the same instant, such as running two calculations on two CPU cores.
Make progress while a request waits
Take a ChatGPT gateway with one application thread on one CPU core. Request A spends 5 ms preparing a database write and waits 50 ms for its result. While A waits, the thread spends 20 ms on independent request B. When the result arrives, A needs 5 ms more to finish. The requests overlap, and the application thread executes one piece of application code at a time.
An event loop runs ready callbacks and lets pending asynchronous operations signal when their results are ready. Input/output, or I/O, includes waiting for a database or a socket. A web runtime can interleave requests while that work happens elsewhere. A long synchronous calculation on the application thread can still hold up every other callback. Marking a function async alone leaves its CPU work on that thread. Runtimes can also use threads or scheduled tasks to overlap waits. Check where each operation runs and what it blocks. Node.js event loop and worker pool
Run independent computation at the same instant
Parallelism means two pieces of work really execute at the same instant. Consider a separate example with two independent prompt-processing calculations. Each needs 20 ms of CPU time. One core needs 40 ms of compute time to run both. Two cores can complete both in about 20 ms if the runtime schedules one calculation per core and overhead is negligible.
Dependencies set limits. The next stage of a calculation may need the previous stage's output. We may still run that stage for another prompt in parallel. Splitting a tiny calculation can cost more than it saves if scheduling the pieces or moving their data dominates. Two runnable threads can share one core, or compete for memory bandwidth across cores. A count of workers is therefore an input to scheduling, and actual execution depends on where the runtime runs them. Go maintainers on concurrency and parallelism
Protect a decision that can overlap
In the ChatGPT walkthrough, two workers can read the same pending reply job before either claims it. If each starts the model based only on that read, the service can generate the reply twice. Tasks can interleave this way even if their application code takes turns on one CPU.
ChatGPT stores each reply job in a row called a turn. To claim a job, a worker asks PostgreSQL to change that row to running only while it is still pending. Here $1 is the ID of the job both workers saw. The full claim also records the worker's ownership token and deadline.
UPDATE turns
SET status = 'running'
WHERE turn_id = $1 AND status = 'pending'
RETURNING turn_id;At PostgreSQL's Read Committed isolation level, a competing update can wait for the first transaction, then PostgreSQL checks its condition against the updated row. If A commits the change, B finds zero rows to claim. A starts generation after its claim commits. The short database write settles who owns the job while other workers can claim different turns. PostgreSQL Read Committed and competing updates · PostgreSQL UPDATE
Rules involving several records may need a transaction and a shared lock. A worker that pauses past its ownership deadline poses a different problem. Keep track of which store can reject its next write. The claim and publication rules are separate stages of the work.
Bound the amount of work in flight
We can keep several requests in progress while each waits on a different socket. That can improve throughput when the process and downstream dependencies have spare capacity. It also uses memory, connections, and queue space. If all requests are waiting for a saturated database pool or GPU, adding more in-flight work can increase waiting time without increasing the completion rate.
In the ChatGPT design, admission reserves room for an accepted turn against both its account and its worker pool. That reservation covers pending and running work. It is distinct from how many instructions the model hardware can execute in parallel. Measure queue time, requests in flight, completions per second, and the saturation of the relevant worker or dependency. A useful concurrency cap keeps the actual bottleneck busy while bounding the amount of work that can wait for it.
See also
- Latency vs Throughput - measure waiting time and the rate at which work finishes.
- Backpressure and Load Shedding - keep the queue and in-flight work bounded when arrivals outrun completions.
- Locking Strategies - decide when to lock a row or check its version at write time.
- Distributed Locking and Fencing Tokens - protect writes when a worker loses its claim.
Used in
- ChatGPT - workers race to claim a pending turn, and the gateway limits admitted work by account and worker pool.
- Batched Inference Service - the service groups waiting requests for GPU execution and sizes the number of concurrently running model instances.
Done reading? Mark it so it sticks in your dashboard.