Choosing a Concurrency Model

Topics Covered

Characterize the Workload

CPU bound and I/O bound work

Amdahl law

The universal scalability law

Latency, throughput and sharing

Measuring the CPU to wait ratio

Memory bandwidth and cache effects

Concurrency versus parallelism

The right concurrency model depends far more on the workload than on fashion or familiarity. Before choosing between threads, async, actors or data parallelism, it pays to understand what the work actually does: whether it computes or waits, how much of it can run in parallel, how it shares state and what latency and throughput it needs. This section builds that vocabulary.

CPU bound and I/O bound work

The first question is where the time goes. CPU bound work spends its time computing, such as image encoding, compression, parsing, numerical simulation or model inference on the CPU, and it speeds up only when more cores run it in parallel; adding more threads than cores gains nothing and adds switching overhead. I/O bound work spends most of its time waiting, for network responses, databases, disks or other services, and it benefits from having many operations outstanding at once, far more than the number of cores, since each one uses the CPU only briefly. Most services mix both, and the mix matters: a web service that spends 5 milliseconds of CPU and 95 milliseconds waiting per request can keep a core busy with about 20 concurrent requests, while a service that spends 50 milliseconds of CPU and 50 waiting needs only two per core. Profiling with wall clock and CPU time side by side shows the ratio, which is the single most useful number for choosing a model and sizing pools.

The ratio of waiting to computing sets how much concurrency each core can usefully hold.

Amdahl law

Amdahl law bounds the speedup from parallelism by the fraction of the work that must run serially. If a fraction s of a task cannot be parallelized, the speedup on N cores is at most one divided by s plus the remaining fraction over N. With 5 percent serial work, 8 cores give a speedup of about 5.9, 64 cores about 15.4, and no number of cores can exceed 20. The serial fraction includes obvious parts, such as reading input or merging results, and hidden ones, such as a lock that every worker takes, a single allocator, a database row that every request updates or a single threaded event loop in front of a pool. Amdahl law explains why many systems scale well to a few cores and then flatten: the serial part that was negligible at 4 cores dominates at 64. Finding and shrinking the serial fraction, by partitioning state, batching updates or removing global locks, usually matters more than adding cores.

Speedup flattens as cores grow, because the serial part does not shrink. Five percent serial caps speedup at 20.

The universal scalability law

Neil Gunther universal scalability law extends Amdahl law with a second cost: coherency, the work needed to keep parallel workers consistent with each other, such as cache line transfers, lock handoffs and cross node messages. Its throughput model has a contention term, like Amdahl serial fraction, and a coherency term that grows with the square of the number of workers, since every pair may need to coordinate. With any coherency cost at all, throughput does not just flatten; it reaches a peak and then falls as more workers are added. This retrograde scaling is common in practice: adding threads to a lock heavy service, adding nodes to a cluster with chatty replication, or raising a connection pool beyond what a database can serve can all reduce throughput. Fitting the law to a few load test measurements estimates both coefficients and predicts the concurrency at which throughput peaks, which is the point to size pools and limits around.

Contention flattens the curve. Coherency costs between workers bend it down after a peak.

Latency, throughput and sharing

Beyond the CPU to wait ratio and the serial fraction, three more properties shape the choice. Latency requirements decide whether work can be batched and queued or must be handled immediately: a trading system or game server cares about every microsecond of tail latency, while a nightly report cares only about total time. Throughput requirements decide how much parallelism and how many machines are needed at all. And the sharing pattern decides how much coordination the model must support: work that is embarrassingly parallel, such as independent requests or image tiles, needs almost none; work partitioned by key, such as users or accounts, needs coordination only within a partition; and work on a single global structure, such as an order book or a shared graph, needs either careful synchronization or a single owner. Writing these properties down for a system, with numbers, often makes the right model obvious and stops long debates based on preference.

Measuring the CPU to wait ratio

The ratio of computing to waiting can be measured rather than guessed. For a single request, comparing wall clock time with CPU time consumed by the thread that served it gives the ratio directly; many frameworks and APM tools record both per endpoint. At the process level, CPU utilization at a known request rate gives the CPU cost per request, and Little law with measured latency gives the concurrency needed. Profilers help too: a CPU profile shows where computation goes, while a wall clock or off CPU profile shows where threads wait and on what, such as socket reads, database calls or locks. These numbers often surprise teams. A service believed to be I/O bound can turn out to spend most of its time serializing JSON or compressing responses, which changes both the model and the optimization priorities, and a service believed to be slow because of computation can turn out to wait on a single overloaded dependency. Measuring under realistic load matters, since ratios change as caches warm, payloads grow or dependencies slow.

Memory bandwidth and cache effects

Cores are not the only resource that parallel code competes for. Memory bandwidth is shared by every core on a socket, and many data heavy workloads, such as scanning large arrays, decoding media or running analytics over columns, saturate it with a handful of cores, so adding more threads only makes them wait for memory. Caches are shared too: threads working on unrelated data evict each other from the shared last level cache, and threads writing to nearby memory suffer false sharing, as covered earlier in the course. On machines with several sockets, memory attached to a different socket is slower to reach, so where threads run relative to their data, known as NUMA placement, can change performance noticeably. These effects show up as speedups well below what Amdahl law predicts from the serial fraction alone. Measuring hardware counters, such as cache misses and memory bandwidth, with tools like perf or Intel VTune, explains them, and fixes usually involve better data layout, partitioning data so each thread works on its own region, and pinning threads near their memory.

Data heavy work can hit the memory bandwidth limit long before every core is busy. Extra threads then just wait.

Concurrency versus parallelism

It helps to keep two ideas distinct, as Rob Pike emphasized in a well known talk. Concurrency is about structure: dealing with many things at once, such as many connections, tasks or events, which may or may not run at the same instant. Parallelism is about execution: doing many things at the same time on multiple cores to finish faster. An event loop on one core is concurrent but not parallel; a data parallel matrix multiplication is parallel but has little concurrency in its structure. I/O bound workloads mainly need concurrency, since the goal is to have many operations outstanding while waiting, and CPU bound workloads mainly need parallelism, since the goal is to keep many cores busy. Many systems need both, and the models in this course differ in which they provide: async gives concurrency on one core, process pools and data parallel libraries give parallelism, and goroutines, virtual threads and actors on multicore runtimes give both. Asking which of the two a workload needs is a quick way to narrow the options.

Measure the workload before choosing a model

Find the ratio of CPU time to waiting, because it sets how much concurrency each core can hold. Use Amdahl law to see how the serial fraction caps speedup, and the universal scalability law to find where extra concurrency starts lowering throughput. Write down latency, throughput and sharing needs with numbers before debating models.