TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† Back to Distributed caching

๐Ÿ“‹ Distributed caching: cheat sheet

๐ŸŽจ The analogyA shared answer board beside a slow, faraway library: check the board first, and walk over only if needed.
๐ŸŽ“ The regular explanationKeeping copies of frequently read data in fast shared memory, such as Redis, so every application server can skip slow database work, and keeping those copies correct as the data changes.
Look on the board firstStep 5 of 5
๐Ÿ“‹ Hallway answer board๐Ÿง’Curious visitor๐Ÿ“Œ Toy 7 answerTTL 10 min๐Ÿ“š Library (far away)
  1. Someone asks about toy number 7. Before walking all the way to the library, the app checks the hallway board.
  2. It isn't on the board yet. That's called a miss.
  3. So the app walks to the slow library and looks it up.
  4. On the way back it writes the answer on the board, with a note saying "rub this out in 10 minutes".
  5. The next person asks about toy 7. It's on the board, so nobody walks to the library. That's a hit.
Why the cache is sharedStep 5 of 5
๐Ÿ“‹ Hallway answer board๐ŸซClassroom 1๐ŸซClassroom 2๐Ÿ“Œ Pencil priceprice 12๐Ÿ“š Library
  1. Two classrooms each keep their own little notebook of answers. Both say a pencil costs 10.
  2. The price goes up. Classroom 1 hears about it, updates the library and fixes its own notebook.
  3. But classroom 2's notebook still says 10. Ask two classrooms the same question and you get two answers.
  4. So the school puts one board in the hallway that every classroom uses instead.
  5. Now every classroom reads the same answer from the same board.
Expiry and evictionStep 5 of 5
๐Ÿ“‹ Board with room for 3๐Ÿง’Curious visitorAna answerread just nowReviews answerread just nowPhotos answerread just now
  1. The board only has room for three answers. Each one says when it was last read.
  2. Every answer also has a rub-out time. When it passes, the answer vanishes by itself, so nothing stays wrong forever.
  3. A new answer is written in the free space. The board is full again.
  4. Someone reads Ana's answer, so it's fresh again.
  5. One more answer arrives, but there's no room. The one nobody has looked at for longest gets rubbed out to make space.
Keeping the board correct when data changesStep 5 of 5
๐Ÿ“‹ Hallway answer board๐Ÿง’Visitors๐Ÿ‘ฉโ€๐Ÿ’ผShop owner๐Ÿ“Œ Pencil price12๐Ÿ“š Library
  1. The board says a pencil costs 10, and everyone who asks gets that answer quickly.
  2. The shop owner changes the price in the library's big book to 12.
  3. Then they rub the old price off the board. They don't write the new one; the next person to ask will fetch it.
  4. The next person finds the board empty, walks to the library, and writes the new price up.
  5. The board and the library agree again, and the rub-out time is a safety net in case anything was missed.
Many cache servers, and what happens when one failsStep 5 of 5
๐Ÿซ Hallway of boards๐Ÿง’Lots of visitors๐Ÿ“‹Board A to Hslots 0-5460๐Ÿ“‹Board I to Qslots 5461-10922๐Ÿ“‹Board R to Zreplica promoted๐Ÿ“š Library
  1. One board is too small for the whole school, so there are three. Every answer always goes on the same board, chosen by its name.
  2. To find Maya's answer, you go straight to board 2. No need to check the others.
  3. Board 3 falls off the wall!
  4. Everyone who needed board 3 runs to the library at the same moment, and the librarian is swamped.
  5. Good schools keep a spare copy of every board, and let only one person fetch each missing answer while the others wait for it.
A hot key expires and everyone misses at onceStep 4 of 4
๐Ÿ“‹ Hallway answer board๐Ÿง’Crowd of visitorsothers wait๐Ÿ“Œ Popular answerreloaded๐Ÿ“š Library
  1. A very popular answer sits on the board, and everyone who asks reads it without walking to the library.
  2. The answer's rub-out time arrives and it vanishes from the board, right in the middle of the crowd.
  3. Everyone checks the board, finds it empty, and they all walk to the library at once. The librarian is swamped.
  4. With a rule that only one person fetches a missing answer, the rest wait at the board. One walk, one answer, and everyone reads it. The cost is a short wait.

๐Ÿงฉ Key questions

Why put a board in the hallway?
So nobody has to make the slow walk to the library for an answer someone already fetched. The library is always right, but slow and busy. The board saves the walk.
The price changes in the library. What does whoever changed it do to the board?
Rubs the old answer off the board. The next person fetches it fresh, so the board never ends up older than the library.
One board falls down. Why is that dangerous?
Everyone runs to the library at once and swamps the librarian. That's why good schools keep a spare copy of every board.
A very popular answer is rubbed out and a crowd arrives. What stops them all walking to the library?
Only one person fetches the answer while the rest wait for it. One walk serves everyone, at the price of a short wait. Spreading rub-out times helps too.
People keep asking for an answer that exists nowhere. Why does every question still reach the library?
The board only shows answers that were found, so there is nothing to read. A short 'no such thing' note, or a quick check against a list of real names, saves the trips.