When cached answers go away
After this: Say when an entry expires under a time to live, and which key leaves a full cache under least recently used.
On this page · 3 sections · 3 checks
The cache in lessons 1 to 3 kept every answer for ever. Real caches cannot. Memory runs out, and an answer kept for a week may no longer be true. This lesson shows the two ways an answer leaves a cache: its time runs out, or the cache is full and needs the room.
A time limit on each answer
Think of milk in a fridge. It has a date on it, and after that date you throw it away and buy fresh, even if it looks fine. A cache can put a date on each answer in the same way.
When the app stores an answer, it gives it a fixed number of seconds to live. After that the copy is gone, and the next read misses and fetches a fresh one from the database. That number is the . The rule: a copy stored at time t is used while the time now is before t + TTL. Reading it does not reset the clock.
Take a TTL of 30 s and six reads of p1, at 0, 10, 25, 35, 50 and 70 s.
| Read at | Copy in the cache | Result |
|---|---|---|
| 0 s | none | miss; store a copy until 30 s |
| 10 s | stored at 0 s, lives until 30 s | hit |
| 25 s | stored at 0 s, lives until 30 s | hit |
| 35 s | expired at 30 s | miss; store a new copy until 65 s |
| 50 s | stored at 35 s, lives until 65 s | hit |
| 70 s | expired at 65 s | miss |
That is M H H M H M: 3 hits and 3 misses. Step through it and watch the seconds left on the cache card.
- Cache-aside: 6 requestsRun it and step through. The cache card shows how many seconds p1 has left. Before each read, work out whether its time is up.
Press Run it, then step through and watch the requests move.
A longer TTL gives more hits, because each copy is used for longer. But an answer that changes in the database keeps its old value in the cache until its time runs out. Lesson 5 is about that problem.
When the cache is full
A cache lives in memory, and memory has a size. When the cache is full and a miss needs to store a new key, one of the keys already there has to leave. Removing a key to make room is .
Which key should leave? A good guess is the one nobody has asked for in the longest time, because a key read recently is often read again soon. This rule is .
Take a cache with room for 2 keys and the reads a b a c b. The cards list keys with the least recently used on the left.
a: miss. The cache holdsa.b: miss. The cache holdsa, b. It is now full.a: hit.ais now the most recently used:b, a.c: miss, and the cache is full.bis on the left, sobis evicted:a, c.b: miss again, because it was evicted. Nowais the least recently used and leaves:c, b.
1 hit and 4 misses, and the cache ends holding c, b. Step through it.
- Cache-aside: 5 requestsRun it and step through. The cache card lists its keys with the least recently used on the left. When c arrives the cache is full: which key leaves?
Press Run it, then step through and watch the requests move.
If the third read had been b instead of a, then a would have been the least recently used, and c would have pushed out a. LRU follows what was read last, not what was stored first.
Check yourself
The time to live is 30 s. A read at 35 s misses and stores p1 in the cache. At what time does that copy expire?
Time to live 30 s, cache empty at the start. The app reads p1 at 0 s, at 20 s and at 40 s. Which reads are hits?
A cache has room for 2 keys and evicts the least recently used. It starts empty. The app reads a, b, b, c, a. What does the cache hold at the end, least recently used first?
- Say when a copy expires: stored at t with a time to live, it is used while the time is before t + TTL, and reads do not reset the clock.
- Work the six reads of p1 under a 30 s time to live: M H H M H M, 3 hits and 3 misses.
- Say what eviction is: removing a key from a full cache to make room for a new one.
- Evict by least recently used: with room for 2, reads a b a c b give M M H M M and leave c, b.