Intermediate 8 min
Caching and stale data
A cache keeps a reusable result so the next request can avoid repeating the original work.
How it works
Ask what makes two requests equivalent. A public lesson can often reuse the same stored HTML for several readers. A private account response depends on identity. After a lesson edit, expiry or explicit invalidation decides when its shared copy stops being useful.
Walk through an example
A lesson list can be cached after loading it once. A later request can reuse it until it expires or an update tells the cache to discard it.
What to watch for
Choose an appropriate cache key and lifetime. Avoid sharing private responses across users under one common key.
Check your understanding
What should publishing do to the old cached result?
See the example and explanation
- Worked example
- Given
The published lesson title changes but readers still see the old title. - Answer
Invalidate it or replace it.
The old result is stale. Removing or replacing it lets readers receive the updated title.