NewWork mode: your Claude Code or Codex on work calls, answering from your project's codeSkip to content
Middle+0 views

How did you invalidate the cache?

Practice

AI answer

We chose the invalidation strategy based on the type of data and freshness requirements. For frequently changing data, we used a short TTL so stale values wouldn’t persist for too long. For data that needed to be refreshed immediately, we deleted keys when the source changed: for example, after writing or updating an object, we called delete for the associated cache key.

We built keys using the object identifier and the request’s version/parameters so that different data variants wouldn’t get mixed together. For bulk changes, we used a namespace or key versioning: switching the version allows you to logically reset a set of cached data without deleting each key individually. It’s important not to scan all of Redis on every reset, and to avoid a flood of requests after a large number of keys expire — TTL jitter helps with this, as does cache-stampede protection when needed.

We also checked consistency during concurrent updates and monitored the hit rate, latency, and cache errors. The specific mechanism depends on whether a briefly stale copy is acceptable and how large the key set is.