Sub topic 1
Latency, throughput and concurrency: what each one measures
This chapter defines the three quantities that every later target in this tutorial builds on. You will learn what each one measures, how they relate, and which mistakes come from mixing them up.
Prerequisites
This chapter assumes you have the following background:
- Basic client-server and request/response concepts.
- Familiarity with common backend components such as load balancers, services and databases.
- Comfort reading simple Python code and doing arithmetic on rates and durations.
The three quantities
Each quantity answers a different question about a system.
| Quantity | Question it answers | Typical unit |
|---|---|---|
| Latency | How long does one request take? | milliseconds |
| Throughput | How many requests complete per unit of time? | requests per second |
| Concurrency | How many requests are in progress right now? | requests in flight |
Latency is the time between a client sending a request and receiving the complete response. It is a property of a single request. Because requests differ, latency is a distribution, not one number. The next chapters cover that in detail.
Throughput is the rate at which the system completes work. It is a property of the system over a time window. Count only successful completions, or report errors separately. A system that fails fast can show high throughput and still serve nobody.
Concurrency is the number of requests that have started but not yet finished at one moment. Thread pools, connection pools and database connection limits all cap concurrency, so it is the quantity that consumes resources.
How they relate
The three quantities are linked. If requests arrive at a steady rate and each spends some time in the system, the average number in flight follows from those two values. This relation is Little's law, covered in its own chapter. The short form is:
concurrency = throughput × latencyTwo consequences follow from this relation:
- If latency rises while throughput stays constant, concurrency rises. A slow database can exhaust a connection pool even when traffic has not changed.
- If you want more throughput at the same latency, you need more concurrency, and so more threads, connections or memory.
A worked example
Suppose a service receives 2,000 requests per second and each one takes 50 ms on average. The average concurrency is 2,000 × 0.050 = 100 requests in flight. The code below computes this and then shows what happens if latency doubles.
arrival_rate_per_s = 2000 # requests per second
avg_latency_ms = 50 # average time a request spends in the system
in_flight = arrival_rate_per_s * avg_latency_ms / 1000
print(in_flight) # 100.0 requests in flight on average
# Same traffic, but a dependency slows down and latency doubles
slow_in_flight = arrival_rate_per_s * (avg_latency_ms * 2) / 1000
print(slow_in_flight) # 200.0If the service has a pool of 150 worker threads, it copes with the first case and not the second. Traffic did not change, but half of the load now has no thread to run on. The excess waits in a queue, which adds more latency.
Common mistakes
- Treating latency and throughput as one number. A system can have low latency and low throughput, such as a single fast worker. It can also have high latency and high throughput, such as a batch job with many parallel workers.
- Using the average for latency. Averages hide slow requests. Use percentiles, as the third chapter explains.
- Ignoring concurrency until it breaks. Pool sizes and limits are set from concurrency, so estimate it early.
- Mixing units. Convert milliseconds to seconds before multiplying by a per-second rate.
Exercises
Exercise
An API handles 500 requests per second at an average latency of 200 ms. How many requests are in flight on average? If the connection pool allows 80 concurrent requests, is it large enough?
Show answer
In flight = 500 × 0.200 = 100 requests. The pool of 80 is too small, so about 20 requests would wait at any moment. The pool needs at least 100 slots, plus headroom for bursts and latency spikes.
Exercise
Throughput stays at 1,000 requests per second, but average latency rises from 20 ms to 80 ms. By what factor does concurrency change, and why does it matter?
Show answer
Concurrency goes from 1,000 × 0.020 = 20 to 1,000 × 0.080 = 80, a factor of 4. Any pool or limit sized for 20 in-flight requests is now overloaded, even though traffic is unchanged.