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

System Design the lesson

The slate next to the stove a show in 9 chapters

Goal: most orders answered from the slatemost requests answered from the cache

customercounterback roomregisteryou, the wala

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.

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.

customercounterthe slateback roomregisteryou, the wala

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.

customercounterthe slateback roomregisteryou, the wala

0 walks to the back + 0 from the slate

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 = ?

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?

10 ms

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.

one line on the slatethe cached value

chai 12

the register, after milk went upthe database, after an update

chai 12 14

  1. 124
  2. 5
  3. 6
  4. 7
  5. 8
  6. 129
  7. 1210
  8. 1211
  9. 1212
  10. 13
  11. 1214
  12. 1215
  13. 1216
  14. 17
  15. 1218
  16. 19
  17. 20
  18. 21
  19. 22
  20. 23
  21. 24
  22. 25
  23. 26
  24. 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 next to the stoveKeep the answers asked most where you answer, and only the new questions walk.chai 12vadalassi3 lines on the slate90%of orders answered from the slate (1 line: 34%)1 walk = 80 slate reads40 ms ÷ 0.5 ms = 80, so walks set the average: 4.45 ms at 90%Full? Rub out the line unasked longest (LRU): 108 of 120 in a busy hour.The price: a stale answer, never older than the lifetime on its line (TTL).chalkbrain.com
The slate next tothe stoveKeep the answers asked most where youanswer. Only the new questions walk.chai 12vadalassi3 lines on the slate90%from the slate(1 line: 34%)1 walk = 80 slate reads40 ms ÷ 0.5 ms = 80, so walks set theaverage: 4.45 ms at 90%Full? Rub out the line unaskedlongest (LRU): 108 of 120 in a busy hour.The price: a stale answer, never olderthan the lifetime on its line (TTL).chalkbrain.com
A small cache, and its hit rateKeep the most requested answers next to the server, and only misses go to the database.chai 12vadalassia cache of 3 entries90%hit rate over 120 requests (1 entry: 34%)1 miss = 80 hits40 ms ÷ 0.5 ms = 80, so walks set the average: 4.45 ms at 90%Full? Evict the least recently used key (LRU): 108 of 120 hits.The price: a stale read, never older than the entry's TTL.chalkbrain.com

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 lesson