L22 Caches and storage
24 minSuggested first: L21 Streams and subscriptions
Outcome
Distinguish cache identity, freshness, invalidation, and eviction; infer the cost of a stated mechanism.
Learn
A cache key identifies a result. A TTL (time to live) says how long an entry counts as fresh in this example. Invalidation marks an entry unusable, or removes it, after a relevant change. Eviction frees space under a capacity policy. LRU evicts the least recently used entry.
How much an operation costs depends on the implementation, not on the policy's name.
Worked example
Capacity is 2 and every entry is fresh. Insert A, insert B, read A, then insert C. Which entry is evicted?
After insert A: A
After insert B: A B oldest first
After read A: B A
After insert C: A C B evicted
Predict first, then show the worked explanation
Reading A makes it the most recent, so inserting C evicts B. In a simple list, finding or removing a key may need a scan. A hash map plus a linked list supports expected constant-time lookup and recency updates, under the usual hashing assumptions, at the cost of extra memory.
Practice and check
Common mistake
Assuming an LRU cache guarantees fresh data, or that private results can share a cache key without regard to who owns them.
Practice on a real repository
Try the first puzzle below, then one of the others. This lesson's example gives no detail of their implementations.
- Reading a key in an LRU cacheDesign · Easy · pallets-jinja
- Batch writes across two backendsDesign · Medium · unjs-unstorage
- Coming back before a cache entry expiresDesign · Hard · reduxjs-redux-toolkit
Related topics: Caching and storage
Practice is optional. Solving a puzzle doesn't complete a lesson, and lessons never change a puzzle's points.
References
- RFC 9111: HTTP Caching (IETF)
- API (Jinja documentation)
- RTK Query cache behavior (Redux Toolkit documentation)