Latency vs Throughput
Two words describe performance, and they are easy to mix up:
- Latency is how long one request takes, from start to finish (milliseconds).
- Throughput is how many requests the system completes per unit of time (requests per second, MB/s).
A motorway makes the difference visible: latency is how long your drive takes; throughput is how many cars pass per hour. Adding lanes raises throughput without making any single car faster. A traffic jam increases latency for everybody even though the road is "at full throughput".
Little's law
One formula links the two, and it holds for any stable system (a server, a queue, a coffee shop):
L = λ × W
L average number of requests in the system (in progress or waiting)
λ throughput (arrival rate, requests per second)
W average time a request spends in the system (latency, seconds)It is incredibly practical: it tells you how many concurrent connections, threads or worker processes you need.
def concurrency_needed(throughput_rps, latency_ms):
return throughput_rps * latency_ms / 1000
cases = [
("API, fast DB", 2_000, 25),
("API, slow partner call", 2_000, 400),
("batch job", 50, 4_000),
]
for name, rps, ms in cases:
print(f"{name:<24} {rps:>5}/s x {ms:>5} ms -> {concurrency_needed(rps, ms):6.0f} requests in flight")API, fast DB 2000/s x 25 ms -> 50 requests in flight
API, slow partner call 2000/s x 400 ms -> 800 requests in flight
batch job 50/s x 4000 ms -> 200 requests in flight
The same 2,000 requests per second need 16 times more concurrent workers when each request waits 400 ms on a slow dependency. That's why slow downstream calls exhaust thread pools and connection pools long before CPUs are busy.
Utilisation and queueing
When requests arrive randomly, a server at 50% utilisation is mostly idle, but as utilisation approaches 100% a queue forms and latency explodes. For the simple single-server queue (M/M/1), the average time in the system is W = S / (1 - ρ), where S is the service time and ρ the utilisation. We can check the formula with a seeded simulation:
import random
def simulate(utilisation, service_ms=10, n=20_000, seed=42):
rng = random.Random(seed)
arrival_rate = utilisation / service_ms # requests per ms
clock = server_free_at = 0.0
total_time = 0.0
for _ in range(n):
clock += rng.expovariate(arrival_rate) # next arrival
start = max(clock, server_free_at) # wait if the server is busy
server_free_at = start + rng.expovariate(1 / service_ms)
total_time += server_free_at - clock
return total_time / n
print("util formula simulated")
for u in [0.5, 0.7, 0.8, 0.9, 0.95]:
formula = 10 / (1 - u)
print(f"{u:4.0%} {formula:8.0f} ms {simulate(u):8.0f} ms")util formula simulated
50% 20 ms 20 ms
70% 33 ms 33 ms
80% 50 ms 50 ms
90% 100 ms 112 ms
95% 200 ms 210 ms
Going from 50% to 95% busy multiplies latency by about ten even though each request still needs only 10 ms of work. This is the reason capacity plans leave headroom and target 60-70% utilisation: the last 30% of throughput is bought with a huge latency cost.
Batching: trading latency for throughput
Many systems have a fixed cost per operation (a network round trip, a disk flush, a transaction commit) plus a small cost per item. Batching shares the fixed cost across many items. Throughput goes up, but each item may wait for its batch to fill.
FIXED_MS = 5.0 # per round trip / commit
PER_ITEM_MS = 0.05 # per row written
print("batch throughput time per batch")
for batch in [1, 10, 100, 1000]:
batch_ms = FIXED_MS + batch * PER_ITEM_MS
throughput = batch / batch_ms * 1000
print(f"{batch:5} {throughput:9,.0f} rows/s {batch_ms:10.1f} ms")batch throughput time per batch
1 198 rows/s 5.0 ms
10 1,818 rows/s 5.5 ms
100 10,000 rows/s 10.0 ms
1000 18,182 rows/s 55.0 ms
Writing rows one by one tops out near 200 rows/s; batches of 1,000 reach about 18,000 rows/s, but the first row of each batch waits for 55 ms. Kafka producers, database bulk inserts, TCP (Nagle's algorithm) and GPU inference servers all expose this same dial, usually as "max batch size" plus "max wait time".
Tail latency and fan-out
When one user request fans out to many back-end calls in parallel, the user waits for the slowest of them. If each back-end call is slow only 1% of the time, a fan-out to 100 servers is slow most of the time:
p_slow = 0.01
for fan_out in [1, 10, 50, 100]:
p_any_slow = 1 - (1 - p_slow) ** fan_out
print(f"fan-out {fan_out:3}: {p_any_slow:6.1%} of user requests hit at least one slow call")fan-out 1: 1.0% of user requests hit at least one slow call
fan-out 10: 9.6% of user requests hit at least one slow call
fan-out 50: 39.5% of user requests hit at least one slow call
fan-out 100: 63.4% of user requests hit at least one slow call
Techniques that tame the tail: hedged requests (send a duplicate request to a second replica if the first hasn't answered by the p95 time), timeouts with fallbacks, and keeping fan-out small.
Tip: When someone says a system is "fast", ask whether they mean latency or throughput, and at which percentile. A system can have excellent throughput and terrible p99 latency at the same time.
Test yourself
A service handles 500 requests per second with an average latency of 200 ms. How many requests are in flight on average (Little's law)?
- 2.5
- 100
- 250
- 2,500
Why do capacity plans aim for about 60-70% utilisation instead of 100%?
- To save electricity
- Queueing makes latency grow sharply as utilisation approaches 100%
- Servers overheat at 100%
- Load balancers require it
What does batching usually trade?
- Durability for consistency
- Some extra latency per item for much higher throughput
- Throughput for lower latency
- Security for speed