Cache Invalidation Strategies
Phil Karlton's famous quote still holds: cache invalidation is one of the two hardest problems in computer science. When the underlying data changes, stale cache entries must be removed or updated — otherwise users see outdated information. TTL (time-to-live) is the simplest strategy: every cached entry expires after a fixed duration. Set product cache to 5 minutes and stale data self-corrects. Event-based invalidation is more precise: when a product is updated, publish an event that tells every cache node to delete product:42. Versioned keys add a version number to cache keys (product:42:v3) — when data changes, bump the version and old entries become unreachable without explicit deletion. Stale-while-revalidate serves the stale value immediately while refreshing the cache in the background — users never wait, but may briefly see old data.