Section 14.21

Cache invalidation

Definition

Cache invalidation is the removal, or marking as unusable, of stored results whose premises have changed, so that the next request computes a fresh result instead of reusing a stale, out-of-date one. A premise is anything a result depended on: an input, a version, a configuration, the behavior of a routine the computation called. Invalidation is needed for every changeable premise that the cache key, the value used to find a stored result, does not include. It must reach results that depend on the change indirectly as well as directly.

Where it fits

Main entry: Memoization and caching (§14.20).

The memoization and caching entry explains keys and when a stored result is correct. Invalidation maintains one of the correctness conditions stated there: every premise outside the key stays unchanged while the entry exists. Three methods are in use.

  1. Versioned keys. Put a version number of each changeable premise into the key. A change increases the number, old entries stop matching, and eviction removes them later.

  2. Dependency tracking. Record, for each stored result, the premises and other stored results it used. On a change, find every result that depends on the changed item, directly or through other results, by a graph reachability search §14.18 that starts at the changed item and follows recorded edges from each item to the results that used it, and delete all of them.

  3. Expiry. Discard each entry after a fixed time, often called its TTL (time to live). Expiry bounds how long a stale result is served; it does not prevent stale results.

Indirect dependence is where invalidation usually fails. Suppose a shared routine HH was used to compute a cached analysis PP, and a cached decision RR was computed from PP. A change to HH invalidates PP directly and RR indirectly: RR never called HH, yet HH is a transitive dependency §10.43 of RR. Deleting only PP leaves RR stale. A third result UU may be kept only if the dependency record shows no path from HH to UU, and that conclusion is sound only if the record lists every real dependency.

Limits and common mistakes

A key made from a name misses changes of content: a file replaced under the same path name has new bytes and an unchanged key. Key by a cryptographic hash §5.1 of the content, or by a version number that every change increases. A computation that began before an invalidation can store its result after it and so put a stale result back; recheck the version just before storing.

Questions answered

Does changing a shared helper invalidate cached consumer results?

Changing a shared helper invalidates every cached consumer result that depends on the helper, directly or through other cached results, unless the key of that result already includes the helper’s version. A shared helper is a routine that several other pieces of code call; a consumer result is a stored result computed by code that called the helper, or computed from another such result. Dependence covers more than the values the helper returns. A result also depends on the helper’s contract: which memory the helper reads and writes, its side effects, the rights it needs, the errors it reports, and its running time when the result is a bound on time or cost.

Results with no dependency path to the helper stay valid. That conclusion is only as good as the dependency record: a text search of the source code for the helper’s name misses calls through function pointers (variables that hold a function’s address), calls through other routines, and results computed from other results. When the record cannot be shown complete, discard every result that might depend on the helper. A discarded result costs only the time to compute it again; a stale one is a wrong answer.

Related entries: Memoization and caching (§14.20), Graph reachability search (§14.18), Security claim scope (§1.45).