The slate next to the stove a show in 9 chapters
Goal: most orders answered from the slatemost requests answered from the cache
walks to the back: 0
1 of 9 The problem
The wala's day
You are the wala, the man who runs the chai stall, and you serve at the counter. Every price lives in the register in the back room, and nothing is written at the front.
A customer is waiting.
Your goal: answer most orders without the walk. Each walk is 40 ms. Reading something already written by the stove is 0.5 ms.
2 of 9 Your guess
Before we fix it
Which picture is closest to how you think a server stops repeating slow work?
Pick one. There is no wrong answer yet.
0 served = 0 walks to the back + 0 from the slate
3 of 9 Try it
Be the wala
Now there is a slate by the stove, with room for 3 lines. A line is one answer, like "chai 12". Ten orders are coming. A blank means a walk, then you chalk the answer.
Order 1 is chai.
0 walks to the back + 0 from the slate
| a busy hour, 120 | these 10 | |
|---|---|---|
| Longest unasked (LRU) | 108 ÷ 120 = 90% | 4 ÷ 10 = 40% |
| Erase at random (Random) | 104 ÷ 120 = 87% | 6 ÷ 10 = 60% |
| Oldest chalk (FIFO) | 103 ÷ 120 = 86% | 5 ÷ 10 = 50% |
| Seeing the future (Bélády) | needs the future | 6 ÷ 10 = 60% |
| You |
4 of 9 The fix
A rule for erasing
The same ten orders, a slate of 3. When it is full, a rule picks the line to rub out. Pick a rule and play it.
Longest unasked (LRU, least recently used): when the slate is full, rub out the line nobody has asked for the longest.
Why can random win on these ten?
At order 7 the slate is full. Longest unasked rubs out chai, unasked since order 3, and order 8 is chai. Random happened to rub out vada, which never comes back.
20 orders, each bar as long as the answer took
a slate read: 0.5 ms a walk to the back: 40 ms
9 in 10 orders at 0.5 ms, 1 in 10 at 40 ms: 0.9 × 0.5 + 0.1 × 40 = ?
4.45 ms on average.
0.9 × 0.5 ms + 0.1 × 40 ms = 0.45 + 4 = 4.45 ms
5 of 9 Your guess
Your guess
Nine orders in ten end at the slate. The bars show where the time goes.
A hit costs 0.5 ms, a miss 40 ms. The share of hits is the hit rate. Each bar is one request, as long as it took.
At a 90% hit rate, what is the average answer time?
What changed
- Average answer: 40 ms, every order a walk → 4.45 ms, 9 in 10 from the slate
4 of the 4.45 ms are the walks, so the last few points of hit rate matter most.4 of the 4.45 ms are the misses, so the last few points of hit rate matter most.
hit rate: 34% average answer: 26.5 ms
6 of 9 Try it
Real traffic
A busy hour: 120 orders, and most are for 3 favourites. Each dot is one order, filled when the slate answered it.
120 requests, 97 in 100 for 3 popular keys out of 24. Each dot is a request, filled on a hit. Change the cache size.
chai 12
chai 12 14
- 124
- 5
- 6
- 7
- 8
- 129
- 1210
- 1211
- 1212
- 13
- 1214
- 1215
- 1216
- 17
- 1218
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 1427
- order 4: chai chalked at 12, then the price goes up to 14
- underlined: the line is alive, orders 5 to 20
- chai orders that read the old 12: 8
- blank: orders for other dishes
- order 27: the line is blank, a walk, 14 chalked
7 of 9 The cost
Yesterday's price
Milk went up: the register says 14, the line still says 12. So each line gets a lifetime, counted in orders. When it runs out the line is blank, and the next chai order walks and chalks 14. Same busy hour each time, chai chalked at order 4.
Each cache entry gets a TTL, time to live, counted here in requests. Until it expires, a changed value is served stale. Below: the same 120 requests each time, with chai cached at request 4 and changed in the database straight after.
What does a lifetime in orders mean?
Once a line is chalked, that many more orders of any dish may read it. After that it counts as blank, so the next order for it walks and chalks the price the register says now.
Below the 90% of chapter 6: a line past its lifetime is blank even for chai, so lifetimes add walks. The trade: a longer life, more from the slate, more sold at 12.
8 of 9 Your guess
Your picture now
In the busy hour a slate of 3 lines answered 90% of orders (chapter 6). A lifetime trades some of that for fresh prices (chapter 7). Which picture is closest now?
Pick one.
The slate is c, a cache: small, close, and rubbed out to make room. A full copy is a replica, and a faster back room is a faster database.
9 of 9 Keep this
Keep this
Keep the answers asked most where you answer. A walk is 40 ms and a slate read 0.5 ms, so one walk costs as much as 40 ÷ 0.5 = 80 slate reads. The walks set the average, so a small slate that holds the favourites does most of the work.
The hit rate decides the speed because misses dominate the average. LRU keeps the recent keys. A TTL bounds how stale an answer can be.
Left for the lesson: a popular line expiring at peak, when 400 orders walk to the back at once.Left for the lesson: a hot key expiring at peak, when 400 requests miss at once (the thundering herd).
Read the full lessonComing up The slate saves the walk to the back room. Next: the counter freezes because the stove is two orders behind, and orders bunch up instead of arriving spaced out, as a Copenhagen telephone engineer found in 1909.