Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Chai stall lab1 of 5
customers the wala kettles each order: 5 ms the wala takes it 40 ms the kettle brews 1 ms the wala pours

the story

Can one wala serve sixteen customers faster?

A wala is the person who runs the stall. Every order needs 5 ms to take, 40 ms for the kettle to brew, and 1 ms to pour. The wala can stand at each kettle until it is done. Then 16 orders take 736 ms.

Or the wala can put a kettle on and take the next order while it brews. How much sooner is everyone served?

watching each kettle 736 msuntil the last cup juggling ? msuntil the last cup 16 customers, one wala, 16 kettles

guess first

If the wala stops watching each kettle, how much sooner?

736 msuntil the last cup. Wala works 13% of it

22 orders a second watching: 736 ms

run it

Put more kettles on at once. When is everyone served?

1 burner

One wala, 16 customers. A burner is one kettle brewing. The big number is when the last cup goes out.

watching: stands at each kettle orange: the wala works. Dashed: standing juggling: takes the next order takes come first, while kettles brew, then the pours. Last cup at 121 ms. 0121 ms

why it helps

While a kettle brews, the wala can take the next order.

Brewing needs nobody. A watching wala just stands for 40 ms. A juggling wala spends that time on other orders, so the waits overlap. This is concurrency.

the working

Wala work per order: 5 + 1 = 6 ms16 orders: 16 × 6 = 96 ms of workWatching: 16 × 46 = 736 msBusy: 96 ÷ 736 = 13%Juggling: last cup at 121 ms, 96 ÷ 121 = 79%
64 customers, a kettle for each +59 busy 100% of the time a second wala? last cup now at 384 ms

your call

Rush hour: 64 customers, one juggling wala. What speeds it up?

System DesignConcurrency: waiting vs working

One wala, many kettles

Sixteen customers, one wala. If the wala stops standing at each kettle, how much sooner is everyone served?

Every order at the stall is five milliseconds of taking, forty of the kettle brewing, one of pouring. One wala can stand at each kettle, or put it on and take the next order. The timeline shows each order's waiting, working and brewing.

try this Switch to Juggle the kettles, drag Customers at once and Burners to 64, then watch Walas busy.

16
1
16

Breaking point: one order that holds the wala for 200 ms stops the event loop: 6 brewed kettles wait for a pour, and 16 orders take 316 ms instead of 121.

The stall, order by order

One row per customer, time running left to right in ms (a millisecond, a thousandth of a second). Each order is 5 ms of taking, 40 ms of brewing and 1 ms of pouring.

123456789101112131415160200400600

All served in

736 ms

from the first customer to the last cup

Orders a second

21.7

served, on average, over the whole run

Walas busy

13%of the time working

The working

Where the time, the rate and the busy share come from, redone every time you move a control.

  1. The wala's workorders1 dot = 1 order, 6 ms of workEach order needs a wala for 5 ms to take it and 1 ms to pour it, 6 ms in all. The 40 ms brew needs nobody. 16 customers bring this much work. 16 × 6= 96 ms
  2. All served inorders in a row1 dot = 46 msThe wala stands at each kettle until it is poured, so the 16 orders go one after another, 46 ms each. 16 × 46= 736 ms
  3. Orders a second736 ms is 0.736 of a second, and 16 orders went out in it. 16 ÷ 0.736= 21.7 a second
  4. Walas busywala time736 msworking96 msThe wala worked 96 ms of the 736 ms the stall was open. The rest was standing and waiting. 96 ÷ 736= 13%

Each wala stands at a kettle for 40 of every 46 ms, doing nothing. The walas are busy only 13% of the time, and the line still waits.

Call 1: hire, or juggle?

Sixteen customers, and the one wala watches each kettle: 736 ms until the last cup. The owner can hire a second wala who also watches each kettle, or teach the one wala to juggle 16 kettles.

Call 2: the rush

Sixty-four customers, one wala juggling, a burner for every kettle. The last cup goes out at 384 ms and the wala never stops moving. Now a second wala joins, juggling too.

Call 3: the heavy order

Back to sixteen customers, one wala juggling on 16 burners: everyone served in 121 ms. Then customer 7 orders masala ground by hand, 200 ms of the wala's hands instead of 5.

Waiting is not working

Watching each kettle, one wala takes 736 ms for sixteen orders, and is busy for only 96 ms of it. Juggling doesn't make a kettle boil faster. It fills the forty milliseconds of idle time with the next order's work, so the waits overlap instead of queueing end to end. That overlap is concurrency.

Push the customers and the burners to 64 while juggling, and the walas-busy number climbs to 100%. Now every millisecond is spent working, and 64 orders times 6 ms of work is 384 ms however cleverly they are juggled. To go below that floor, you need less work per order or a second wala working at the same time. That isparallelism: more cores.

The burners cap the orders in flight. Without a cap, juggling would put every customer's kettle on at once. In a real system, the thing doing the brewing (usually a database) has a limited number of burners of its own.

Limits of this model

These are teaching values. Five, forty and one milliseconds make the pattern visible: a request that spends most of its time waiting on something else. That is true of most web requests. It is not true of work like resizing an image.

The brew here always takes forty milliseconds, however many kettles are on. A real database slows down as more requests pile onto it, so juggling harder can make every wait longer. This lab models scheduling only.

Every customer arrives at once, and a free wala always picks the task that has waited longest. Real servers take arrivals as they come and schedule in their own ways. A greedy choice like this one can even make an extra burner cost a few milliseconds.

Sources

On a narrow screen the timeline narrows and the three readouts stack under it.

the lesson The chai stall that got famous (load balancing)
The whole idea this lab came from, told start to finish.

on the path Module 1 of 32 in System Design: The stall gets famous
The lessons, boards and videos around this lab, in reading order.

Board: The Chai Stall That Got Famous

next lab How cost grows
A million items. O(n log n) finishes in a blink. How long does O(n²) take?