Caching a Missing Lookup Result

Caching a missing result can avoid repeated expensive work, but the application needs a policy for when absence can change.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace creation after a miss
  2. Separate two kinds of absence
  3. Distinguish missing data from failed retrieval
  4. Define how a newly inserted record becomes visible

Trace creation after a miss

Searching a catalog for a code may initially find nothing. If someone creates that item, a cached miss can still tell callers it is absent. An invalidation or time limit chosen for the catalog controls that interval. Dictionary caches make the retained decision visible.

Separate two kinds of absence

A lookup that could not contact storage is not evidence the item is absent. Store distinct outcomes so a temporary failure does not become an ordinary negative entry. See error and absence results for this contract.[1]

Distinguish missing data from failed retrieval

Suppose a catalogue has no record for code B7. Caching that successful absence can avoid repeated work. A timed-out catalogue request gives no evidence that B7 is absent. Caching it as missing would make the service seem to have lost data until expiry. Store an explicit status or reserve a sentinel for confirmed absence. See error results and exceptions.

Define how a newly inserted record becomes visible

A positive cache and a negative cache can use different expiry or invalidation rules. When B7 is created, invalidate its old absence marker after the creation commits if the cache must reflect it promptly. Test an actual miss followed by insertion and then another lookup. Include a temporary error to verify it does not overwrite a previously known value without a policy. See caches made from a mapping.

References

  1. Python Software Foundation, Python Standard Library: Built-in Types ↩