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

Caching lab1 of 5
orders counter the slate chaivada pavpoha on the slate:5 ms kitchen ledger not on it?walk: 45 ms

the story

How big should the slate be?

A chai stall writes its most-asked dishes on a small slate. If the dish is on it, the customer waits 5 ms. If not, someone walks to the ledger: about 45 ms.

Most orders are for 3 favourites. A bigger slate takes space by the stove. So how many lines is enough?

today: 3 lines chaivada pavpoha 9.0 msmean wait double it: 6 lines chaivada pavpoha+ 1+ 1+ 1 ? msyour guess

guess first

Double the slate. What happens to the wait?

31 msmean wait, 1 line on the slate

41 on the slate 79 walked

run it

Make the slate bigger. When does it stop helping?

1 line

Each square is one of 120 orders. Green was on the slate (5 ms). Red walked to the ledger.

chaivada pavpoha dosakulfisamosa asked againand again asked once,then neveragain

why it stops

3 dishes fill the slate. The rest are one-offs.

Once the 3 favourites fit, an extra line holds a dish nobody asks for twice. It sits there unused.

the working

3 lines: 108 × 5 ms = 540 ms+ 12 walks = 535 ms1,075 ms ÷ 120 orders = 9.0 ms6 lines: 117 × 5 ms = 585 ms+ 3 walks = 136 ms721 ms ÷ 120 orders = 6.0 ms
lunch rush, slate full chai8 amvada pav9 ampoha10 amdosa11 am time each line was written a new order kulfi? chai was askeda minute ago.dosa: an hour ago.

your call

One line has to go. Which rule wipes the right one?

System DesignCaching: eviction

The slate next to the stove

Most orders are for three favourites. How big does the slate need to be before a bigger one stops helping?

A chai stall keeps its answers on a small slate next to the stove, and a stream of orders comes through the counter, most of them for three favourites. Each one is found on the slate or walked to the ledger in the back. Grow the slate and watch the waits shrink, then stop.

try this Tap scenarios 1, 2 and 3 below to grow the slate, then drag Asks that go to the 3 favourites and watch the mean wait.

3
97%

24 dishes on the menu. The bottom of the second dial spreads asks evenly, so the favourites are no more popular than anything else.

Breaking point: Cache thrash. The orders loop round 4 dishes and the slate holds 3, so longest unasked erases each dish just before it is asked for again. Not one of 120 requests is found, and every one walks to the ledger.

90%of requests found on the slate

9.0 msmean wait

5.0 msthe slowest 1 in 10 wait at least (p90)

A bigger slate helps until the favourites fit

One stall's requests, most of them for three favourite dishes. Watch where each request spends its time, and the row of waits along the front. Then move the two dials, lines held and asks for the favourites, and it plays your setting.

#1

#1: in through the load balancer to the app.

A request goes from the client through the load balancer to the app, which looks on the slate first and walks to the kitchen ledger only when the slate has no line for it.clientload balancerappslatekitchen ledger/menu/42 · MISS · 46 ms
Show the code and trace
The request handler, running
// One GET, start to finishvoid handle(request r) {  key = r.path;              // via the load balancer  if (slate_get(key, &v)) {  // the slate first    respond(r, v);           // HIT    return;  }  v = ledger_query(key);     // MISS: walk to the ledger  slate_set(key, v);         // chalk it, maybe erase one  respond(r, v);
Tracepaused
#1 GET /menu/42 · slate MISS · ledger 40 ms · 46 ms total

The working

Where the hit rate and the waits come from, redone every time you move a dial or pick a rule.

  1. Found on the slatefoundwalked1 dot = 2 requests108 of the 120 requests found their dish already chalked on the slate. The other 12 walked to the kitchen ledger. 108 ÷ 120= 90%
  2. All the waitingslate540 msledger535 msAn answer off the slate takes 5 ms there and back, so the 108 found add 540 ms. The 12 walks to the ledger take 535 ms between them, 36 to 51 ms each. 540 + 535= 1,075 ms
  3. The mean waitmean053651 msAll that waiting, shared out over the 120 requests. A slate answer is 5 ms and a walk 36 to 51 ms, so the more requests the slate answers, the nearer the mean sits to 5 ms. 1,075 ÷ 120= 9.0 ms
  4. The slowest 1 in 10the restthe slowest 121 dot = 2 requestsLine the 120 waits up, fastest first. One in ten of them is 12, and the 12 slowest all waited at least 5.0 ms. That cut is the p90, and here even it was answered off the slate.= 5.0 ms

Ten orders, by hand

The same slate at the counter, one question at a time, so you can see which line each rule erases. The dial and tabs above drive this too.

The queue

  1. 1chaiwalk
  2. 2chai
  3. 3chai
  4. 4vada
  5. 5tab
  6. 6vada
  7. 7lassi
  8. 8chai
  9. 9tab
  10. 10lassi

The slate

3 lines, chalked as they're asked.

  • chaiasked 1×

The register

1

walk to the back so far

0%never left the counter

Now: chai

1 of 10 asked

Why longest unasked matters

Longest unasked tracks when each line was last read and erases the one read longest ago. A line nobody has asked for recently is usually not about to be asked for. In this run it erases chai after a few orders without a request. The very next order asks for chai, so the slate needs an extra walk to the back.

The tradeoff: Tracks recency well. A rare line having one busy morning can look permanently popular.

Call 1: the lunch rush

Lunch rush. 9 in 10 orders are for the three favourites, and the rest are one-off dishes nobody asks for twice in a while. The slate holds 4 lines: the three favourites and one spare.

Call 2: fresh prices

The milk price rose and the slate still said ₹10. So you switch to Expiry: a line is good for the next 4 orders after it is chalked, then it goes blank, asked or not. 97 in 100 orders are for the favourites.

Call 3: the loop

A strange day. The orders go round four dishes in turn, the same four in the same order, again and again. The slate holds 3 lines, one fewer than the loop.

The two curses

Curse one: staleness. "Milk price rose in the register. The slate still says ₹10." A line can look right and be wrong, and the customer gets a confident, wrong answer. This lab touches on it only briefly. Switch the policy above to Expiry and watch a line go blank on a timer, whether or not the answer underneath changed.

Curse two: the slate is small. When it's full, something has to go. This is the curse the lab models: five rules for choosing, and a hit rate that changes with the one you pick.

Limits of this model

One slate, whole-line keys and ten fixed questions. A real system layers several caches: browser → CDN → app → database, each one faster, smaller and staler than the one behind it.

The hit rate here is arithmetic on a fixed list. A real cache's hit rate depends on a live, shifting access pattern. This one repeats the same ten orders every run so the five policies can be compared fairly.

Every key here is a whole answer ("chai", "₹10"). A real cache often stores partial objects, ranges or computed fragments, and invalidating a fragment is harder than erasing a line.

Sources

A hundred and twenty requests and one slate, played in your browser.

the lesson The slate next to the stove (caching)
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 Slate Next to the Stove

see also Consistent Hashing and Random Trees, Kafka: a Distributed Messaging System for Log Processing, Zanzibar: Google's Consistent, Global Authorization System and Scaling Memcache at Facebook in Papers.

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