S4 · Lesson 48 · Queueing and sampling lab
Simulating an M/M/1 server, the tail at scale with hedged requests, and sampled simulation, in Python
Lessons 39, 43 and 44 used three formulas that are easy to misapply: the M/M/1 response time, the tail-at-scale probability, and the number of samples a sampled simulation needs. Each comes from a model, and the best way to trust a model is to simulate the system it describes and see the formula land on the measurement. This lab simulates a single-server queue, a fan-out service with and without hedged requests, and random sampling of a program's CPI, and checks each against its formula.
- The curve is the M/M/1 mean response time, , against utilization ρ for a service time S.
- It doubles from ρ = 0 to 0.5, quintuples by 0.8, and grows without bound toward 1.
- The lab's event simulation reproduces it within a percent or two with a few hundred thousand requests.
Predict
A request fans out to 100 leaves; each is slow 1% of the time. Hedging sends each leaf request to two replicas and takes whichever answers first. What fraction of requests are slow with and without hedging, if replicas are slow independently?
Without hedging, . With two replicas a leaf is slow only if both are: , so . Doubling the leaf requests (or sending the second only after a short delay, which costs far less) turns the common case back into the rare one.
Name it
The models
- M/M/1: Poisson arrivals at rate λ, exponential service with mean S, one server: with . Simulate it by drawing exponential gaps and service times; each request starts at the later of its arrival and the previous request's finish.
- Tail at scale: k parallel sub-requests, each slow with probability p: . Hedged across two independent replicas: .
- Sampling: n random samples of a quantity with coefficient of variation CoV estimate its mean within at confidence z (1.96 for 95%), so .
Check
Write mm1(lam, S, n, seed): simulate n requests of an M/M/1 queue with random.Random(seed) (draw each interarrival gap, then each service time, with expovariate) and return the mean response time.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write tail(k, p, trials, seed, hedge): simulate trials requests, each fanning out to k leaves that are slow with probability p (with hedge=True a leaf is slow only if two independent draws both are), and return the fraction of slow requests.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write samples_needed(cov, eps, z) and sample_error(values, n, trials, seed): draw n values at random (with replacement) trials times and return the 95th percentile of the relative error of the sample mean.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does the mean response time of a queue grow so sharply as utilization approaches 1?
Arrivals and service times vary at random, so even when the server is idle on average a fraction of the time, requests sometimes arrive in clumps or meet long services, and a queue forms. The server can only work off a queue at the rate its spare capacity allows, which is the fraction of time it would otherwise be idle, 1 − ρ. As that spare capacity shrinks, each backlog takes longer to drain and new arrivals pile on before it clears, so the typical waiting time grows like 1/(1 − ρ). At 90% utilization a request waits about nine service times on average, at 99% about ninety-nine.