The chai stall that got famous a show in 8 chapters
Goal: send 300 people an hour to open stalls, with a short wait even for the unluckiest
1 of 8 Your guess
Before we start
A website gets famous overnight. Thousands of people ask for its pages at once, far more than one machine can answer.
Which picture is closest to how a busy website serves them all?
Pick the closest. You find out which fits at the end.
300 people ÷ 40 cups = 7.5 stalls. Half a stall cannot be built, so 8.8 stalls × 40 cups = 320 cups an hour, enough for 300
2 of 8 The problem
The stall gets famous
A chai stall on a corner. One stove, one wala, 40 cups an hour, and that is plenty, because 30 people come.
Then someone posts about it. Now 300 people come every hour. The cups still come out at 40 an hour.
Press Play to watch one hour, a quarter at a time.
The obvious answer is a bigger stove. It works for a while, then fails twice over.
Stoves have a largest size, so one day you own the biggest one there is. And a bigger stove is still one stove.
The biggest stove made. Now crack it.
So you do the other thing. Open a second stall beside the first, then a third, then more. Each new stall adds 40 cups an hour.
The stove stopped being the limit. That buys the real problem: 300 people are standing in one road, and nobody has told them which stall to walk to.
What changed
- Cups an hour: 40 → 320
Your goal: send every customer to a stall that is open, so that even the unluckiest one waits only a short time.
1,600 customers ÷ 8 stalls = 200 each, a fair sharebusiest stall 303, quietest 85the list itself was even: 70 asks over 8 stalls in turn, 8 or 9 each
3 of 8 The idea
1994: hand out the doors
This really happened, and the stall was a website.
At the University of Illinois, the NCSA had written Mosaic, the browser that turned the web into something ordinary people used. It pointed at NCSA's own pages. Then the web took off, and NCSA's one machine was the stall on the corner.
NCSA stood up a row of identical machines. Then they changed the internet's phone book, the system that turns a site's name into a machine's address, which is DNS.
It now handed out several addresses in a new order each time. The lesson asked for amazon.com three times in a row. Ask, and watch the order.
Three doors, A, B and C. Nobody has asked yet.
Take the next in the list, then the next, then start again at the top: that rotation is round robin. Done by DNS, it is DNS round robin, still running today.
Rotating people across stalls only works while the stalls are interchangeable. If your half-paid tab is in the second wala's head, you must go back to him.
The tab is in wala 2's head. Move it to the register.
A stall that keeps nothing of its own between customers is stateless. NCSA's machines were: every page sat on a shared shelf all of them read from.
Your computer keeps the address it was given, for a while. That keep-for time is the TTL, time to live. Until it runs out, every visit goes to the same door.
In the lab, 1,600 customers come from 24 offices. Each office keeps the stall it was told for 10 seconds. One office sent 425 customers, another 16.
The list spreads offices evenly, once each. It does not spread the orders they send.
4 of 8 The problem
1995: the list is blind
In April 1995, Thomas Brisco wrote the first problem down in the public memos the internet is designed in: the rotating answer put the addresses in an order, but how busy the machines were never came into it.
The phone book is not standing at the stalls. It cannot see that stall 2 is halfway through sixty cups for an office while stall 3 is idle.
The second problem is worse. When a stall dies, the list does not find out. Its address stays on the list and keeps being handed out.
The lab runs 1,600 customers through 8 stalls, and stall 3 dies at customer 600.
Press Play to kill stall 3.
1,600 customers ÷ 100 = 16 in the slowest 1 in 100line all 1,600 waits up, shortest first: only 16 waited longer than the 1,584thevery order 100 ms: the 1,584th waited 84 ms, next in turn or shortest line
7.4 s the slowest 1 in 100 waited
1 ÷ 8 = 12.5% for one pick1/8 × 1/8 = 1/64, about 1.6%, for twoslowest 1 in 100: 2.9 s asking 2 stalls, 2.8 s asking all 8
5 of 8 The fix
Late 1995: the man at the gate
Netscape's site was one of the busiest on the web, running the rotating list. Its webmaster, Robert Andrews, told John Mayes some machines were very busy and others did almost nothing. He guessed ten customers in the world had this problem.
Mayes drew a box: everything passes through the middle. It shipped in April 1996 as Cisco's LocalDirector. The ten customers turned out to be everybody.
That box is a load balancer: at the stall, a man at the gate with a book of tokens.
The man can still work blind: next stall, next stall, round robin again. Or he can look, and send each customer to the shortest line.
To compare them, watch the unlucky customers, not the average. Line everyone up by how long they waited. The slowest 1 in 100 is the tail.
Times here are in ms, milliseconds: a thousandth of a second each.
With every order the same size, 100 ms each, the slowest 1 in 100 waited 84 ms under both rules. Blindness is free when there is nothing to see.
Why is there any wait when every order is the same?
Customers arrive in clumps, not one exactly every few milliseconds. A clump at one stall makes a short line even when the work is even.
Orders are not the same size. In the lab, 29 of 1,600 orders are big ones, about 2 in 100, each 60 times an ordinary order.
Same customers, same stalls, same big orders. Switch the rule.
Next in turn sends the next customer to a stall stuck on a big order, because it never looks.
Is there more work now?
No. 1,571 ordinary orders × 48 ms is about 76 s, and 29 big ones × 2.9 s is about 84 s. 76 + 84 = 160 s, the same as 1,600 orders × 100 ms. Only the shape changed.
Shortest line has a cost: before every customer, the man asks all 8 stalls how long their line is.
Ask just two stalls at random, and take the quieter. That goes wrong only when both picks are the busiest stall.
This is the power of two choices: 2 questions instead of 8, and nearly the same tail.
What changed
- Stalls asked before each customer: 8 → 2
- Slowest 1 in 100: 2.8 s → 2.9 s
6 of 8 Try it
Are you up?
Here is what the man at the gate can do that a phone book cannot. He asks. Every 2 seconds he puts a question to each stall that only a working stall can answer.
All eight answer. Now kill stall 3.
That probe is a health check. It is why a load balancer is a reliability device before a speed one.
Same lab, same death at customer 600. This time the gate hands out stalls in turn and probes every 2 seconds.
Press Play to kill stall 3 again.
Why does the gate hand out stalls in turn here?
A dead stall fails every customer at once, so its line always looks empty, and shortest line would send it more. In turn, it gets only its turns, so the count is the probe's gap and nothing else.
One convenience undoes all of it. The man remembers you and writes your usual wala on your token, so your tab lives in that wala's head again. That is a sticky session.
A deploy, a push of new code, restarts every wala. Try it both ways.
Sticky. Press Deploy.
The cure is the register again. Its bill is one trip there per customer, so the register becomes the busiest thing in the shop. That is the next lesson.
7 of 8 Your guess
Your picture now
Goal met: 8 stalls that keep nothing, a gate that counts the lines (2.8 s for the slowest 1 in 100, not 7.4 s), and asks every 2 seconds (15 customers lost to a dead stall, not 55). Which picture is closest now?
Pick one.
c is a load balancer in front of stateless stalls. b is DNS round robin from 1994: still the first step, but blind to lines and to dead stalls. a is the bigger stove, which runs out twice.
8 of 8 Keep this
Keep this
Grow by adding stalls, not a bigger stove. Keep every tab in the register so any stall will do. Put a man at the gate who counts the lines and asks each stall whether it is up.
The price: the gate is now one thing that can die, and the register is now the busiest thing in the shop.
Read the full lessonComing up Any wala can serve any customer because every wala forgets. Next: the price of a cutting chai, asked hundreds of times an hour, and a slate by the stove that saves the walk to the back room.