Back to trackLesson 12 of 25

Support Desk 2: A Single-Agent Queue

Uses Only Free-Tier SpiceGrinder (Recur is a Free-tier filter in 1.0)

Problem statement

Lesson 3.1 counted tickets: how many are open at the end of each interval. It left a loose end. The backlog was unfloored, so it could go below zero, and every ticket counted the same. This lesson turns to the customers behind the tickets and asks how long each one waits.

One support agent works a single line, first come first served. Customers arrive at random, on average 0.9 a minute, so the gap between two arrivals averages about 1.1 minutes. Each takes on average 1 minute to serve. The agent is busy 90% of the time. The questions:

  • How long does a customer wait in the line before the agent picks up, on average?
  • What share of customers wait at all, and what share wait more than 5 minutes, or more than 30?
  • How long must the run be before the answer can be trusted, and how do you put an honest error bar on it?

You might expect an event simulation, with a clock, a calendar of future events and a queue object. None of that is needed. Customer n waits as long as it takes to finish the work ahead of them, and that depends only on the previous customer’s wait, the previous customer’s service time and the gap between the two arrivals. That is Lindley’s recursion:

wait(n) = max(0, wait(n-1) + service(n-1) - gap(n))

If the customer ahead waited 6 minutes, took 2 to serve, and this customer arrived 3 minutes after them, this customer waits 5 minutes. If the gap were 10 minutes, the line would have emptied and the wait would be 0, not negative. The wait is the state, one row is one customer, and Recur carries it from row to row. The max(0, ...) is the floor that the unfloored backlog of lesson 3.1 lacked.

The model

Two Exponential nodes give the gaps (rate 0.9 per minute, mean 1.11 minutes) and the service times (rate 1.0, mean 1 minute). Append puts one service time and one gap side by side in a row, and Recur keeps the wait. The model is samples/simulation/recur-lindley-queue.xml, four nodes, seed 20261006:

<dataset columns="wait, service, gap" seed="20261006">
  <root node="Wait"/>
  <nodes>
    <Exponential name="Service" lambda="1.0"/>
    <Exponential name="Interarrival" lambda="0.9"/>
    <Append name="Step">
      <input name="Service"/>
      <input name="Interarrival"/>
    </Append>
    <Recur name="Wait" initial="0" expression="y[0] = max(0, s[0] + x[0] - x[1])" appendInput="true">
      <input name="Step"/>
    </Recur>
  </nodes>
</dataset>

In the expression, s[0] is the previous wait, x[0] is the service time and x[1] the gap. initial="0" starts the line empty, and the first customer, who walks straight up, is not a row; the first row is the second customer. With appendInput="true" each output row carries the inputs after the state, so there are three columns: the wait, then the service time and the gap that row used. Run it:

grind --count 10 recur-lindley-queue.xml

The first five rows with the model’s own seed, 20261006 (you get exactly these if you leave the seed alone):

RowWait (column 0)ServiceGapCheck: previous wait + service - gap
10.6021.1630.5610 + 1.163 - 0.561 = 0.602
20.0550.3750.9220.602 + 0.375 - 0.922 = 0.055
30.1610.1060.0000.055 + 0.106 - 0.000 = 0.161
44.5895.5961.1680.161 + 5.596 - 1.168 = 4.589
56.3921.8980.0954.589 + 1.898 - 0.095 = 6.392

Row 4 is a long service (5.6 minutes) with a short gap: the line builds from a fraction of a minute to 4.6. For a different line each time, leave the seed out:

grind --no-seed --count 1000000 recur-lindley-queue.xml

Each row depends on the one before it, exactly as in lesson 3.1, so a run is one trajectory. Here is a stretch of 300 customers in a row, taken from the middle of the model’s own run (customers 100,001 to 100,300):

The waits of 300 customers in a row: a gray line that climbs through busy spells to a peak of 27.5 minutes and falls back, touching zero 26 times, each marked with a red dot, with a dashed red line at the long-run mean of 9

The wait climbs while a run of slow customers or quick arrivals builds the line, and falls when the agent catches up. The floor is visible: in these 300 customers the wait is exactly 0 for 26 of them (about 9%), the customers who arrive to an empty line. A line with the memory of one number needs no calendar.

Throw away the start

The model begins with an empty line, and an empty line is a rare thing at 90% load. The first customers wait less than the later ones will, so the early rows are not typical of the long run. Here is the running mean of the wait over the first 5,000 customers, for ten runs and for the average of 200 runs (seeds 1 to 200):

Running mean of the wait over the first 5,000 customers from an empty start: ten gray runs wander, a black line for the average of 200 runs climbs from near 0 toward a red theory line at 9 minutes, and a shaded band of one standard deviation across runs is still wide at 5,000 customers

Averaged over the 200 runs, the mean wait of the first 10 customers is 1.47 minutes, of the first 100 it is 4.68, of the first 400 it is 7.64, of the first 1,000 it is 8.55 (standard error 0.35) and of the first 5,000 it is 8.98 (standard error 0.16), against a long-run 9. The climb takes a few hundred customers to get most of the way; the line forgets its empty start slowly, because a line near full capacity takes a long time to build up or drain. The band is the other lesson of the picture: a single run of 5,000 customers still has a standard deviation of 2.3 minutes across runs.

The fix is a warm-up: run longer than you need and throw away the first rows, so that what you average comes from a line that has settled. For the long run below, the first 100,000 customers are dropped, a few hundred times the time the line takes to forget its start. Discarding is cheap and it is simple insurance, but notice what it is not: it removes a bias that is small compared with the luck of the draw, as the companion page shows.

Result: the average wait and who waits

One long run of the model’s own seed, 10,100,000 rows. The first 100,000 are discarded as the warm-up and the other 10,000,000 are kept. The mean wait is 9.036 minutes. How far is that from the right answer, and how would you know? The standard error is not what you might compute first.

Why consecutive waits are not independent. A customer who waits 20 minutes was followed by one who waits about 20 minutes. Over the 10,000,000 kept waits:

Lag between two customersCorrelation of their waits
10.991
100.918
1000.521
1,0000.028

Neighbors are almost the same number; the correlation only fades after hundreds of customers. If you treated the 10,000,000 waits as independent, the standard error of the mean would be the standard deviation of a wait (10.04) over the square root of 10,000,000: 0.0032 minutes. That would say the mean is 9.036 plus or minus 0.003, and it would be wrong by a factor of twenty.

Batch means put it right. Cut the 10,000,000 kept waits into 100 consecutive batches of 100,000, take the mean of each batch, and treat the 100 batch means as the sample. A batch is much longer than the reach of the correlation, so the batch means are nearly independent (their lag-1 correlation is 0.09), and the usual standard error of 100 numbers applies. The mean wait is 9.036 with a standard error of 0.062, twenty times the naive 0.0032. The effect of the batch length on the standard error:

Batch sizeNumber of batchesStandard error
1 (the naive way)10,000,0000.0032
101,000,0000.0099
100100,0000.0282
1,00010,0000.0548
10,0001,0000.0627
100,0001000.0624
1,000,000100.0832

The standard error grows with the batch size until the batches are longer than the correlation, then levels off near 0.062; the last row, with only 10 batches, is itself less certain. Ten million waits carry about as much information about the mean as 26,000 independent ones. Put another way, the variance of the mean times the run length is about 38,900, some 390 times the variance of one wait.

The other quantities, with standard errors from the same 100 batches:

QuantityMeasuredBatch SE
mean wait9.036 minutes0.062
P(wait > 0)0.90030.0004
P(wait > 5 minutes)0.54680.0015
P(wait > 30 minutes)0.04510.0012

So 90% of customers wait, more than half wait over 5 minutes, and about 1 in 22 waits more than half an hour. The distribution of the wait is the picture to keep in mind: a pile exactly at zero, then a long tail.

Histogram of the wait of 10,000,000 customers: a separate red bar at exactly zero holding 10.0% of customers, then two-minute gold bars falling away like an exponential curve, closely following a black theory curve

Of the 10,000,000 kept waits, 997,227 are exactly 0 (9.97%); the other 90% are spread over a tail that falls by a constant factor for each extra minute.

Check: do the numbers match the math?

One server, exponential gaps and exponential service times is the textbook M/M/1 queue, and its steady state is known exactly. With the arrival rate lambda = 0.9 and the service rate mu = 1.0, the load is rho = lambda / mu = 0.9. The probability that a customer waits longer than t minutes is rho times e^(-(mu - lambda) t) = 0.9 e^(-0.1 t). Everything follows from that:

QuantityTheoryMeasuredGap in standard errors
P(wait > 0)0.90.90030.66
P(wait > 5)0.9 e^(-0.5) = 0.54590.54680.58
P(wait > 30)0.9 e^(-3) = 0.04480.04510.21
mean waitrho / (mu - lambda) = 0.9 / 0.1 = 99.0360.58
variance of the waitsecond moment 180 minus mean squared 81 = 99100.7(no standard error given)

The mean comes from the same tail: 90% of customers wait, and those who do wait 10 minutes on average. The standard errors are the batch ones from the tables above, and all four gaps are under one standard error. The two-minute bars of the histogram are within 0.0004 of the theory bars everywhere.

The hand check is the table at the top: each wait is the previous wait plus the service minus the gap, floored at zero, and a script recomputing the recursion in Python from the first 12 rows of the model’s own output matches all 12. The 200 short runs and the 100 companion runs, with other seeds and lengths, are consistent with the same theory: their averages over runs sit at 9 within their standard errors once the runs are long enough.

Run lengths and seeds: the long run is the model’s own seed (20261006), 10,100,000 rows, 100,000 discarded, 100 batches of 100,000. The short runs are seeds 1 to 200 with 5,000 rows each. The model is valid and fits Free: ModelValidatorApp reports VALID, 4 nodes against Free’s limit of 20.

How long should a run be?

The companion page asks a practical question: if you simulate N customers once, how far is your answer from 9 minutes? It runs the model with 100 different seeds (1 to 100) at 10,100,000 rows each, and for each run reports the mean wait of the first N customers (no warm-up) and of the N customers after a 100,000-row warm-up. The spread of those 100 answers, with their means:

Customers NNo warm-up: mean (sd)Within 5% of 9With warm-up: mean (sd)Within 5% of 9
1,0008.74 (4.69)6 of 10010.74 (10.43)7 of 100
10,0009.00 (1.67)19 of 1009.27 (2.21)15 of 100
100,0008.99 (0.67)59 of 1008.96 (0.64)56 of 100
1,000,0009.01 (0.19)98 of 1009.01 (0.18)98 of 100
10,000,0009.00 (0.063)100 of 1009.00 (0.061)100 of 100

A run of 1,000 customers (after a warm-up) can report anything from about 2.4 to 85 minutes; a run of 100,000 lands within 5% of the truth only about 6 times in 10. Making the run 100 times longer makes the spread about ten times smaller, which is the square-root law of earlier lessons, but with a large constant: the spread times the square root of N is about 180 to 220, while for independent draws it would be 10. Warm-up changes little in this table: the two columns do not differ by more than their spread allows, and the no-warm-up means are not clearly lower. The empty start matters mainly in the first few hundred customers, as in the picture above; what dominates here is the spread between runs. The companion page lets you pick the run length and the method and see the 100 answers.

What this gives you: a single-agent queue with no event engine: Lindley’s recursion, wait = max(0, last wait + last service - gap), as one Recur over Appended service times and gaps, in four nodes; the floor max(0, ...) that keeps a wait from going negative; a warm-up, discarding the first rows of a run that starts empty (the first 100 customers average 4.7 minutes against a long-run 9); and standard errors from batch means rather than from the rows, because neighboring waits are almost identical (lag-1 correlation 0.99) and the naive standard error is twenty times too small. The model gets the mean wait of 9.04 (standard error 0.06) against the M/M/1 value of 9, and P(wait > 30) of 0.0451 against 0.0448. The companion page shows how widely short runs wander.

All of this was at a load of 90%, and the line took ten million customers to pin the mean to under 1%. The next lesson changes the load. As the agent gets busier, the average wait does not rise gently: it explodes as the load approaches 1, and the runs needed to measure it must get much longer. Lesson 3.3, the utilization cliff, runs the same queue at six loads from 50% to 95% and finds out how long each needs.