Reliability and Cost Engineering › Concurrency, Capacity, and Little's Law

Michael BrenndoerferDecember 2, 20264 min read
Language AI HandbookAll chapters
Part 64 of 67Compound AI System Design
Chapter 490Reliability and Cost Engineering › Concurrency, Capacity, and Little's Law
Reading section 6 of 12 · Concurrency, Capacity, and Little's Law · 4 minSection 6 of 12 · 4 min

Section 6 of 12 · 4 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

The retry simulator changes the number of calls without tracking a queue. Little's law relates time in a queueing system to its occupancy.

L=λ WL = \lambda \, W

Here LL is the long-run mean number of requests inside a boundary, λ\lambda is the rate of requests entering that boundary, and WW is the mean time those requests spend inside it. For a stable system with finite long-run averages, requests per second times seconds per request gives mean occupancy in requests. The identity includes queued work if the boundary includes the queue. Little's original proof states the conditions on the stochastic process and its means.

At 50 requests per second and a mean time in the system of 1.2 seconds, mean occupancy is 60 requests. Use a load test to decide how many active slots or how much headroom a latency target needs. In the idealized M/M/1 queue with Poisson arrivals and exponential service times, W=1/(μ−λ)W=1/(\mu-\lambda) for arrival rate λ\lambda below service rate μ\mu. It grows as the arrival rate approaches capacity. The next example keeps service time fixed, so its curve is for a different queue.

The simulation is a continuous-time single-server FIFO queue with exponential interarrival gaps and deterministic service. It starts empty and drops the first 5,000 jobs from the latency measurement.

PythonIn [14]
CollapseExpand
def simulate_queue(arrival_rate, service_time, n_slots=200_000, seed=3):
    """Single-server FIFO queue with continuous event times.
    arrival_rate: Poisson arrivals per second.
    service_time: deterministic service time per request.
    Returns average latency W over the run (excluding warmup)."""
    rng = np.random.default_rng(seed)
    # Exponential gaps generate the Poisson arrival times.
    gaps = rng.exponential(1.0 / arrival_rate, size=n_slots)
    arrival_times = np.cumsum(gaps)
    # Single server: each job starts at max(arrival, previous_finish).
    finish = np.empty(n_slots)
    prev_finish = 0.0
    for i in range(n_slots):
        start = max(arrival_times[i], prev_finish)
        finish[i] = start + service_time
        prev_finish = finish[i]
    warm = 5_000
    latency = finish[warm:] - arrival_times[warm:]
    return latency.mean(), latency


mu = 1.0 / 0.05  # server capacity = 20 jobs/second
results = {}
for frac in (0.3, 0.6, 0.8, 0.9, 0.95):
    lam = frac * mu
    W, _ = simulate_queue(lam, service_time=0.05)
    results[frac] = (lam, W)
ConsoleOut [15]
CollapseExpand
server capacity mu = 20.0 jobs/s, service time = 0.05 s
 util   lambda   W(measured)  L=lambda*W
 0.30     6.00         0.061        0.36
 0.60    12.00         0.087        1.05
 0.80    16.00         0.149        2.38
 0.90    18.00         0.272        4.89
 0.95    19.00         0.510        9.69

At 95% utilization, a 10% relative increase in arrival rate raises offered load to 104.5% of capacity. If that rate persists in this unbounded model, a finite steady-state queue cannot be maintained. In a real service, test admission limits and burst headroom against the measured tail rather than relying on a fixed utilization target.

PythonIn [16]
CollapseExpand
utilizations = np.linspace(0.1, 0.98, 60)
service_time = 0.05
drawn_latencies = np.array(
    [
        simulate_queue(
            rho / service_time, service_time, n_slots=35_000, seed=5
        )[0]
        for rho in utilizations
    ]
)
# M/D/1 mean waiting time plus the fixed service time.
modeled_latencies = service_time * (1 + utilizations / (2 * (1 - utilizations)))
VisualizationOut [17]
CollapseExpand
Simulated and analytical mean latency rise sharply at high utilization. They are close until the highest loads near full capacity.
Simulated mean total latency in a single-server queue with Poisson arrivals and fixed 50 ms service. The dashed line is the steady-state M/D/1 mean. The finite simulation is least stable close to capacity.

The dashed curve adds mean waiting time to fixed service time s=0.05s=0.05 seconds. If ρ=λs<1\rho=\lambda s<1 is utilization, the M/D/1 mean is s[1+ρ/(2(1−ρ))]s[1+\rho/(2(1-\rho))]. The waiting term grows as utilization approaches one. The sample mean varies with the seed, especially close to capacity. Use the table and Little's law to connect time in the queue to mean in-flight work. A system that scales after a planning window can still queue during a short burst. Load-test the headroom and scaling delay around the operating point you intend to use.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy