Sub topic 5
Little's law: connecting concurrency, latency and throughput
This chapter assumes you know what latency, throughput and concurrency measure, and that you can read short Python programs. It also assumes you are comfortable with percentiles and with estimating average and peak request rates. Little's law ties three of those quantities together. You will use it to size thread pools, connection pools and concurrency limits, and to check whether a target is consistent with itself.
The law in one line
Little's law says that in a stable system, the average number of items inside the system equals the average arrival rate times the average time each item spends there.
L = λ × W- L is the average number of requests in the system. This is the concurrency, also called the number in flight.
- λ (lambda) is the average arrival rate in requests per second. In a stable system it equals the throughput.
- W is the average time a request spends in the system, from arrival to completion. For a service this is the average latency, including any time spent waiting in a queue.
The law makes no assumption about arrival patterns, service-time distributions, or how many servers there are. It holds for a single thread, a pool, a whole service, or a checkout queue in a shop. The only requirements are that you measure over a long enough window and that the system is stable (explained below).
A first calculation
Suppose a service receives 2000 requests per second and each request spends 50 ms in the system on average.
# Little's law: in_flight = arrival_rate * time_in_system
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 averageOn average, 100 requests are being handled at any moment. Note the unit conversion. Latency is in milliseconds and the rate is per second, so you divide by 1000 to put both in seconds. Mixing units is the most common arithmetic error with this law.
Rearranging the formula
The same relationship answers three different questions.
| You know | You want | Formula |
|---|---|---|
| Arrival rate and latency | Concurrency | L = λ × W |
| Concurrency and latency | Maximum throughput | λ = L / W |
| Concurrency and throughput | Average latency | W = L / λ |
The second row is the one that surprises people. If a client may have only 10 requests outstanding and each takes 200 ms, its throughput cannot exceed 10 / 0.2 = 50 requests per second, no matter how fast the server could go. A concurrency limit therefore sets a ceiling on throughput that depends on latency.
def max_throughput(concurrency_limit, latency_s):
"""Upper bound on requests per second for a fixed concurrency limit."""
return concurrency_limit / latency_s
def required_concurrency(rate_per_s, latency_s):
return rate_per_s * latency_s
print(max_throughput(10, 0.200)) # 50.0
print(required_concurrency(500, 0.040)) # 20.0What "in the system" means
L counts everything between arrival and departure. Where you draw the boundary decides what W must include.
- If the system is the whole service, W is the full response time, including time queued before a worker picks the request up.
- If the system is only the worker pool, W is service time (the time a worker is busy), and the queue is a separate system with its own L and W.
- If the system is a database connection pool, W is the time a connection is held, which may be shorter than the request latency because the request also spends time outside the database.
Pick the boundary first, then use a W measured at that boundary. A common mistake is to use the end-to-end request latency to size a database connection pool. The pool only needs to cover the time connections are held.
Sizing pools and limits
Little's law gives the average number of busy workers. You need more than the average, because load fluctuates.
A worked example. A service must handle a peak of 800 requests per second. Each request holds a database connection for 15 ms on average.
peak_rate_per_s = 800
conn_hold_s = 0.015
avg_busy = peak_rate_per_s * conn_hold_s
print(avg_busy) # 12.0 connections busy on average at peak
headroom_factor = 1.5
pool_size = int(avg_busy * headroom_factor + 0.999) # round up
print(pool_size) # 18Twelve connections are busy on average at peak. If the pool has exactly 12, then about half the time demand exceeds supply and requests queue. A pool of 18 leaves 50% headroom. The right factor depends on how bursty the traffic is and how much queueing delay you can tolerate. A later chapter explains why latency rises sharply as utilization approaches 100%.
Check the other side too. A bigger pool is not free. Each connection uses memory on the database server, and too many concurrent queries can slow every query. If the database can only run about 20 queries efficiently at once, a pool of 18 per instance across 10 instances would send 180 concurrent queries. Compute the total across all instances, not per instance.
Use the law to check a target
Targets can contradict each other. Little's law is a quick way to find out.
Say the plan states: 5000 requests per second at peak, average latency 80 ms, and the service runs on 4 instances each with 50 worker threads.
rate = 5000
avg_latency_s = 0.080
instances = 4
threads_per_instance = 50
needed = rate * avg_latency_s
available = instances * threads_per_instance
utilization = needed / available
print(needed) # 400.0
print(available) # 200
print(round(utilization, 2)) # 2.0The plan needs 400 requests in flight but provides only 200 slots. Utilization would be 200%, which is impossible. Either latency must drop to 40 ms, the capacity must double, or the throughput target must fall. You found this with one multiplication, before building anything.
Even a utilization of 90% is dangerous at this stage. Queueing delay grows quickly as utilization approaches 1, so the average latency of 80 ms would no longer hold. The law assumes W is the latency you actually get at that load, not the latency you measured on an idle system.
Conditions and edge cases
Stability
The law applies to a stable system, where arrivals on average equal departures over the measurement window. If requests arrive faster than they complete, the backlog grows without bound and L keeps climbing. Throughput then equals the completion rate, not the arrival rate. In an overloaded system, use the completion rate for λ and expect W to keep growing with the length of the test.
Averages only
L, λ and W are all long-run averages. The law does not say that every request takes W, and it does not give percentiles. A system with an average latency of 50 ms and a p99 of 900 ms satisfies the law with the average. Do not plug a p99 into the formula and read the result as a p99 concurrency. You can use p99 latency to get a conservative upper estimate of concurrency, but it is a deliberately pessimistic number and not a statistical result.
Window effects
Measure over a window much longer than a single request. Requests that are still in flight at the start or end of a short window distort the result. For a service with 50 ms latency a one-minute window is fine. For a batch job where each item takes 10 minutes, you need hours.
Retries and failed requests
Count every attempt that occupies the system. If 10% of requests are retried once, the real arrival rate at the backend is 1.1 times the client rate. Timeouts also matter. A request that times out after 5 seconds held a slot for 5 seconds, and those long holds raise L sharply during an incident. This is one reason slow dependencies exhaust thread pools so quickly. A dependency that goes from 20 ms to 5 s multiplies the in-flight count by 250 at the same arrival rate.
rate = 400 # requests per second
normal_s = 0.020
timeout_s = 5.0
print(rate * normal_s) # 8.0 in flight normally
print(rate * timeout_s) # 2000.0 in flight if every call hangs until timeoutMixed workloads
If requests have different latencies, use the weighted average. A service where 90% of requests take 10 ms and 10% take 500 ms has an average of 0.9 × 10 + 0.1 × 500 = 59 ms. The slow 10% account for most of the concurrency: 50 of the 59 ms of average hold time. Sizing from the typical 10 ms would underestimate by a factor of about six.
Using the law with measurements
You can measure any two of L, λ and W and compute the third, then compare with a direct measurement. If they disagree, something is wrong with the instrumentation or the system is not stable.
# Samples taken once per second over a 60 s window
in_flight_samples = [92, 105, 98, 110, 97, 101] * 10 # 60 samples
completed = 6000 # requests completed in the window
window_s = 60
measured_avg_latency_s = 0.048
L = sum(in_flight_samples) / len(in_flight_samples)
throughput = completed / window_s
predicted_latency_s = L / throughput
print(round(L, 1)) # 100.5
print(throughput) # 100.0
print(round(predicted_latency_s, 3)) # 1.005Here the numbers do not agree: L divided by throughput gives about 1.0 s, but the measured latency is 48 ms. The arithmetic shows that either the throughput was counted wrongly (6000 completions in 60 s is only 100 per second, which would need 100 in flight at 1 s each) or the latency metric excludes queue time. Disagreement is a signal to investigate. In practice, the usual causes are a latency metric that starts after the queue, a completion counter that misses a code path, or sampling in-flight counts at the wrong boundary.
Summary of how the pieces connect
Common errors
- Mixing milliseconds and seconds in W.
- Using end-to-end latency for a resource that is held for only part of the request.
- Sizing to the average without headroom.
- Sizing from average daily rate instead of peak rate.
- Ignoring retries, which raise the real arrival rate.
- Forgetting that latency rises under load, so a latency measured at low load understates W at peak.
- Computing pool size per instance and forgetting the total across the fleet.
Exercise
A service must sustain 1200 requests per second at peak. Its average latency at that load is 75 ms. Each instance has 40 worker threads. How many in-flight requests are expected on average, and what is the minimum number of instances if you want average thread utilization at or below 60%?
Show answer
In flight = 1200 × 0.075 = 90 requests. At 60% utilization, each instance carries 40 × 0.6 = 24 requests on average. The minimum is 90 / 24 = 3.75, so 4 instances. With 4 instances, utilization is 90 / 160 = about 56%.
Exercise
A client library allows 20 outstanding requests. The server answers in 250 ms on average. What is the highest throughput the client can reach, and what happens to that ceiling if the server slows to 1 s?
Show answer
Throughput is at most L / W = 20 / 0.25 = 80 requests per second. At 1 s latency the ceiling drops to 20 / 1 = 20 requests per second, so a slower server cuts client throughput even if the client wants to send more.