S448 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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, S/(1−ρ), 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?

Name it

The models

  • M/M/1: Poisson arrivals at rate λ, exponential service with mean S, one server: T=S/(1−ρ) with ρ=λS. 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: 1−(1−p)k. Hedged across two independent replicas: 1−(1−p2)k.
  • Sampling: n random samples of a quantity with coefficient of variation CoV estimate its mean within ε=zCoV/n at confidence z (1.96 for 95%), so n=(zCoV/ε)2.

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?