# a queue theory primer so simple even I can understand it

They say brits love to queue. out here in 
[alex jones impression] 'MURICA
[fireworks, eagle screech (actually the iconic sound they use is a red-tailed hawk)]
we are very, very bad at it. There's a complete branch of mathematics dedicated to studying queue theory, though I imagine niether you nor I have a math degree. But we don't need one to do it right.

## pipeline optimization

I'm stealing this metaphor from Casey Muratori.
[https://youtu.be/xCBrtopAG80, 27:14] "it takes 2 hours to do a load of laundry, washes and dries it for you. Now, I have a washer, and a dryer. takes 1 hour to wash 1 hour to dry. Which one of these is faster at doing laundry?"

Casey goes on to explain that you can gain performance by overlapping work within a pipeline.

Now. Imagine you are the laundry.
[there has to be some stock footage or meme gif about someone popping their head up from a pile of laundry. or probably a cat]
Your goal is to get through the wash, ideally as quickly as possible. So you wait in line for the washing machine, and then wait in line for the dryer.
Even though the line for the dryer is shorter, you still have to go wait in line for the wash first.

[next line: slower]
The point of this metaphor is that the other, shorter line is an illusion. All of the laundry has to go through "the laundry room" as a whole.

## how we do it

Since the world is hellbent on doing everything the most wrong way possible, here's what we do instead.

[cgp grey clip? https://www.youtube.com/watch?v=oAHbLRjF0vo]

on airlines, you pay more to sit earlier. which is silly to me, you still have to wait for the cheap seats to board before the plane takes off. The only difference is that now your whole boarding process is slower, and you've burned more money to have to leave the airport bar earlier.

In a grocery store, what arises naturally is 1 line per register. This allows you to try and change lines if you think it'll get you up to the register faster. 
[https://www.youtube.com/watch?v=dVPzJrSIjh8 - part where he changes into the faster lane, which stops, and then his previous line goes faster]
If you're the gambling type.

## a better way

There's a better way. Let us assume the following: 
[note] Given the prevalence of "oh i just need one quick thing" and packed-solid carriages in express lines, this is apparently too much to ask.
* no customer is entitled to get through the line faster than any other.
[note] Sometimes there's cash-only or card-only registers, that breaks this down. If you're dead-set on spending more time with the most attractive clerk, this method is disadvantageous for you.
* every server is equivalent. 

[https://en.wikipedia.org/wiki/M/M/c_queue]
[on screen when "M/M/c queue" is said:] Markovian<br />Markovian<br />c servers
There's a better way to do this: It's called an M/M/c queue. You arrive, whatever server is available handles you, and you're gone. This does mean that instead of looking at 2 lines, you look at 1 line that has twice as many people in it. But it also means that a line twice as long moves *at least* twice as fast.
Which is why bank tellers tell you to wait in line for them to be ready. 
[https://youtu.be/8Jwt1mWChtw?t=166 ?]

Even better than that, suppose you have more tellers than registers. If you're at a bar and most people have a tab running, you can have a server sign in, find your tab, add your drink to it, and then sign out, freeing up that register while they pour the beer. This way with 3 staff and 2 machines, 
[https://en.wikipedia.org/wiki/Kendall%27s_notation - the graphic for the m/m/1 queue, but have 3 service nodes]
the math is calculated with 3 "service nodes" instead of 2.
Additionally, having more people than machines rotating around provides a signal that we're doing it the efficient way.

## simulation

Let's imagine an average bar in a cursed city.
[video of 1 door locked]
...maybe a mediocre bar.

Let's imagine 20 people want beer, there are 2 registers, and 3 bartenders.

### M/M/c

First, let's do it right. 

20 people start filling out the singular beer line. 

a bartender summons the customer at the front of the line to the register. He takes the customer's order. Keys it in. Goes to start filling beer. Once the order is taken, the register is free for another bartender while the glass is being poured, so the customer shifts out of the way while waiting. repeat, 20 times.
[let that play out]

### M/M/1 x2

Now, let's see what results when people think they're smart.

the first person to have to wait in line picks a register. possibly at random, possibly through some assumption about which would be faster.
The second person sees a register has 1 person in line, another register has 0, so he gets in line for register 2.
soon we have 2 lines of 10.
Every single delay in each line is compounded through the entire line - line 1, guy drops his credit card before handing it over? add a second to the wait time for everyone behind him. Another guy needs to count out coins? add 3 minutes for everyone behind him. Register 2 falls off the internet and has to reconnect? add 5 minutes for everyone in line 2.
[simulate M/M/1 x2]

## MATH

so far, my source has been "trust me bro". 
[LRR dech tech for phyrexia all will be one] It's time... for.. MATH


...actually I think this whole video is invalid. the math implies that an m/m/c queue is equivalent to c m/m/1 queue's. I think.
https://rossetti.github.io/RossettiArenaBook/app-qts1sb2.html
