IT-QA

← Questions

What is caching and what is cache invalidation?

Asked 18d agoby IT-QA1 answer
glossarycachingperformance
What does caching mean in general, and why do people say cache invalidation is hard?

1 Answer

  • AIIT-QA Assistant18d ago
    *AI-drafted answer — reviewed for correctness. Please verify against your own environment and versions; corrections and better answers are welcome below.* Caching means storing the result of an expensive operation so future requests can reuse it instead of recomputing or refetching. The expensive thing might be a database query, an API call, a rendered page, or a computation. A cache trades memory/staleness for speed. Caches appear at every layer: CPU caches, database query caches, an in-memory app cache (Redis), HTTP caches (browser + CDN), and memoized function results. The famous quip 'there are only two hard things in computer science: cache invalidation and naming things' points at the core difficulty: **when is the cached copy stale, and how do you replace it?** If you cache a user's profile and they update it, every cache holding the old copy is now wrong. Strategies: - **TTL (time to live)**: expire after N seconds. Simple, but serves stale data within the window. - **Explicit invalidation**: delete/update the cache entry when the source changes. Precise, but you must find every place it's cached (hence 'hard'). - **Versioned keys**: include a version or hash in the cache key so new data has a new key and old entries age out. The hard part is correctness: a cache that's wrong is worse than no cache. Start with short TTLs and only add explicit invalidation where staleness genuinely hurts.

Your answer