First, the key terms.
- Cache key
- The inputs identifying a reusable result.
- Invalidation
- Ending or replacing the validity of a cached item.
The idea, explained
Caches reduce repeated work but can serve stale information. A time-to-live limits reuse duration; explicit invalidation responds to known changes. HTTP distinguishes browser caches, shared caches and validation using mechanisms such as entity tags. Cache keys must include the inputs that change a response. Personalized or sensitive data requires special care because a shared cache can accidentally expose one user’s result to another.
Ask what changes the response
A public edition may depend on date and category. A private preferences response also depends on the subscriber’s authorized identity. Omitting a relevant input can return the wrong result even when the cache itself works perfectly.
Specify acceptable staleness
A daily public briefing can tolerate different freshness from an account balance. Set lifetimes and validation policies around those consequences. Immediate invalidation can also fail, so consider how stale content is labeled and recovered.
Follow a worked example.
A public daily edition can be reused across readers. A private preferences page cannot safely share a cache entry keyed only by its path, because the signed subscriber token changes whose settings are shown.
Walk it through, one step at a time
- Cache /news by edition date and category for public readers.
- Do not place every /preferences response in one shared cache entry keyed only by the path. That could expose another reader’s settings.
- Test with two authorized identities and inspect cache-control behavior as well as content.
Where people get stuck
A high cache-hit rate can coexist with incorrect or dangerously stale results.
Now try it yourself.
1. Check the core idea
Why does a cache key need to include relevant request variations?
Reveal the answer & reasoning
Otherwise different requests may reuse the same stored response even when their correct results differ.
2. Apply what you learned
Is a cache hit necessarily a correct response?
Reveal the answer & reasoning
No. A hit means a stored result matched the cache lookup. Correctness also depends on key design, authorization, freshness and the stored content.