キャッシュの無効化 — キャッシュされたデータを信頼できる情報源と一致させ続けること — は、コンピューティングにおける最も難しい問題の一つとして有名です。課題は、パフォーマンス、一貫性、複雑さのバランスを取りながら、キャッシュが 古いデータ(stale data) を提供しないようにすることです。理解しておくべきいくつかの戦略と落とし穴があります。
中核となる問題
When the source data changes, the cached copy becomes STALE.
→ Serve stale data? (fast but wrong) vs invalidate? (consistent but complex/slower)
→ "There are only two hard things in CS: cache invalidation and naming things."
The difficulty: knowing WHEN and WHAT to invalidate, across distributed systems,
without races, while keeping good cache hit rates.
無効化の戦略
1. TTL (expiration) — data auto-expires after a time → eventually consistent
✓ Simple, no tracking ✗ Stale until expiry; pick TTL by staleness tolerance
2. EXPLICIT invalidation — delete/update the cache when source data changes
✓ Fresh quickly ✗ Must reliably catch ALL writes; easy to miss some paths
3. WRITE-THROUGH — update cache and DB together on writes → cache stays fresh
4. EVENT-BASED — invalidate via events/CDC (e.g. DB change → invalidation message)
→ Often combined: explicit invalidation + a TTL as a safety net.
