← Concepts

阅读中文版本

Negative Versus Positive Caching

Negative caching stores the absence of a result to avoid repeating an expensive miss, but it must expire quickly when the missing item may soon appear.

caching

Definition

Negative caching is the practice of caching a not-found or empty result, in contrast to positive caching which stores the presence of a value, so repeated misses become cheap.

Context and scope

It protects origins from repeated lookups of identifiers that do not exist, such as a missing user or a deleted resource.

Common misconceptions

  • negative cache entries should live as long as positive ones
  • a negative cache cannot cause incorrect behaviour

Anti-patterns

  • caching not-found responses for the same duration as hits
  • using negative caching for values that are created shortly after first lookup

Related concepts

Related examples

Authoritative source

https://www.rfc-editor.org/rfc/rfc2308

Definition

Negative caching is the practice of caching a not-found or empty result, in contrast to positive caching which stores the presence of a value, so repeated misses become cheap.

It protects origins from repeated lookups of identifiers that do not exist, such as a missing user or a deleted resource. Compared to simpler alternatives, this concept fits best when the trade-off it represents is the one the system actually cares about.

When to use it

A common use case is applying it at the boundary where the cost of getting it wrong is highest, and the scenario usually involves a concrete guarantee the system must uphold. Unlike an ad hoc approach, it makes that guarantee explicit, reviewable, and testable.

How it works

A single lookup of a nonexistent key can be as expensive as a real lookup, so caching the miss prevents an attacker from forcing repeated expensive queries.

The risk is a false absence: if a resource is created moments after a miss was cached, readers will keep seeing not-found until the entry expires.

Trade-offs and alternatives

Keep negative entries short-lived and, where the workflow allows, invalidate the negative entry explicitly when the underlying record is created. The main drawback is the extra complexity it introduces, so the benefit should clearly outweigh that cost before a team adopts it.

Common misconceptions

A persistent misconception is that negative cache entries should live as long as positive ones. Another is that a negative cache cannot cause incorrect behaviour. For example, an engineer who assumes otherwise may design around a guarantee that does not actually exist, which only becomes obvious under load or failure.

Anti-patterns to avoid

Two recurring anti-patterns are to caching not-found responses for the same duration as hits and to using negative caching for values that are created shortly after first lookup. Both tend to surface as hard-to-debug production issues rather than obvious test failures. Related concepts include Cache Preloading and Warming, Distributed Caching, Write-Behind Caching.