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?
guess first
Double the slate. What happens to the wait?
31 msmean wait, 1 line on the slate
wait as the slate grows
41 on the slate79 walked
run it
Make the slate bigger. When does it stop helping?
12345678
Each square is one of 120 orders. Green was on the slate (5 ms). Red walked to the ledger.
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
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.
Preparing for interviews? The ten-week plan puts this in order with DSA and the design rounds.
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 slate of 3 lines holds the 3 favourites, and the mean wait is 9.0 ms. You double it to 6 lines. What happens to the mean wait?
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 on its way
a wait answered from the slate
a wait that walked to the ledger
slate lines, the current dish lit
Show the code and trace
The request handler, running
1// One GET, start to finish2voidhandle(request r) {3 key = r.path; // via the load balancer4if (slate_get(key, &v)) { // the slate first5respond(r, v); // HIT6return;7 }8 v = ledger_query(key); // MISS: walk to the ledger9slate_set(key, v); // chalk it, maybe erase one10respond(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.
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%
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
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
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
1chaiwalk
2chai
3chai
4vada
5tab
6vada
7lassi
8chai
9tab
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.
Which rule sends fewer orders walking to the kitchen ledger?
The dials wait until you make the call.
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.
With 3 lines, 67 of 120 orders are found on the slate. You double it to 6 lines. How many are found now?
The dials wait until you make the call.
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.
Which rule finds the most orders on the slate?
The dials wait until you make the call.
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.