Redis: Which Key Goes When the Cache Is Full?
Outline
Transcript
0:00 Redis keeps a cache of things your app reads a lot, like copies of product details. When it fills up, a write to store or change a value can fail. We'll follow one write as Redis chooses which key to remove. A key is the name attached to each stored value. For example, in our test we stored 16 values and lowered the limit. The next write succeeded. That entry on the right had gone untouched the longest. How did it survive? Redis looks at a sample, just some of the keys. My first thought was that it knew the least recently touched key in the whole cache.
0:36 It can only choose among the ones it finds. We used the Redis guide to making room by removing keys and its published code, the instructions Redis follows. Both are linked in the description. So let's start with the rule. We chose least recently used, which favors the key whose last use is furthest in the past. Any key could go. Take a look at four. Then I'd cross out 9 seconds. Easy for me to say when I can see all four. What's in the bracket? That's the sample. Idle time is how long a key has gone without being touched.
1:13 Inside the bracket, 5 seconds wins. That entry you pointed to stayed outside the bracket. With the whole picture in front of me, five looks like a bad choice. What if Redis kept the keys in order of when they were last used? It could take the last one right away, but it would have to keep the order current. Redis estimates idle time when it searches for room. Which means our write waits while Redis looks for room. Now follow the write. Redis checks memory first. We're already over the limit, so it looks for something to drop.
1:51 Removing a key to free memory is eviction. The first block is the sample. Next is the pool, a short list of keys Redis might remove. In this illustration the list is full. Key A has 3 seconds saved. Now the sample brings in key B at five. So key B takes A's place on the list. That swap leaves both values in the cache. I didn't realize key C was still on the list from an earlier search. Could key C go even after someone reads it? It can. The read leaves 8 seconds on that row. Redis can still use that saved time.
2:30 Then which one does Redis check first? Redis starts with the longest idle time saved in the list and checks that key still exists. It removes the key if it's there, or tries the next one if it's gone. Once it frees enough memory, the write can run. What if Redis can check only one key? Now look at the separate run. Key nine disappeared and key zero was still there. The pool started empty. With 1 key sampled and one removed, key nine had to be the one Redis found. Key zero gets to sit there while key nine makes room.
3:06 Would asking for more keys always find the one that's been idle longest? The original comparison had three fresh runs at each setting. With a request for one key, key zero stayed twice. At 64 it went each time. Redis still has to see that key to pick it. That cache had only 16 keys, so it could never find 64 different ones. What if we lower the limit the same way with eviction turned off? Look at the bottom row. Each write returned an out-of-memory error, meaning Redis refused to store a new value under that limit.
3:42 Zero keys were removed. I'd look for that error if a write fails. Redis still answered. Once the write goes through, the missing key gives us a different clue. Now suppose the app asks for key nine and gets nothing back, a cache miss. The first panel shows what Redis reported after our separate run. The other panel shows a different way to lose a value. If Redis clears it after its deadline, that's an expiration. Same miss either way. INFO stats is a built-in report with the running totals across Redis.
4:17 Making room for our write added to one count. Could those totals tell us it was key nine? They count removals across the cache. We had to check which names were still there to see that nine was gone. If evictions rose at the limit, I'd check which keys the setting lets Redis remove. That's what happened to our write. Redis found a key it could remove, the write went through, and the first key we put in stayed right where it was. Thanks for listening to Learning Podcasts.