โ 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.
- Someone asks about toy number 7. Before walking all the way to the library, the app checks the hallway board.
- It isn't on the board yet. That's called a miss.
- So the app walks to the slow library and looks it up.
- On the way back it writes the answer on the board, with a note saying "rub this out in 10 minutes".
- The next person asks about toy 7. It's on the board, so nobody walks to the library. That's a hit.
- Two classrooms each keep their own little notebook of answers. Both say a pencil costs 10.
- The price goes up. Classroom 1 hears about it, updates the library and fixes its own notebook.
- But classroom 2's notebook still says 10. Ask two classrooms the same question and you get two answers.
- So the school puts one board in the hallway that every classroom uses instead.
- Now every classroom reads the same answer from the same board.
- The board only has room for three answers. Each one says when it was last read.
- Every answer also has a rub-out time. When it passes, the answer vanishes by itself, so nothing stays wrong forever.
- A new answer is written in the free space. The board is full again.
- Someone reads Ana's answer, so it's fresh again.
- One more answer arrives, but there's no room. The one nobody has looked at for longest gets rubbed out to make space.
- The board says a pencil costs 10, and everyone who asks gets that answer quickly.
- The shop owner changes the price in the library's big book to 12.
- Then they rub the old price off the board. They don't write the new one; the next person to ask will fetch it.
- The next person finds the board empty, walks to the library, and writes the new price up.
- The board and the library agree again, and the rub-out time is a safety net in case anything was missed.
- 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.
- To find Maya's answer, you go straight to board 2. No need to check the others.
- Board 3 falls off the wall!
- Everyone who needed board 3 runs to the library at the same moment, and the librarian is swamped.
- 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 very popular answer sits on the board, and everyone who asks reads it without walking to the library.
- The answer's rub-out time arrives and it vanishes from the board, right in the middle of the crowd.
- Everyone checks the board, finds it empty, and they all walk to the library at once. The librarian is swamped.
- 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.