EMZETT.
Login

Caching

In short: The process of temporarily storing data in a cache, to speed up future access.

In more detail: Caching strategies differ in how long (TTL) and how a cache is updated — e.g. “cache invalidation” when the original data changes, or “stale-while-revalidate”, where outdated data is briefly still delivered while it’s reloaded in the background.

In Depth

Common caching strategies in practice:

  • Cache invalidation: when the original data changes, the associated cache entry is actively deleted or overwritten — precise, but effortful to implement (“there are only two hard problems in computer science: cache invalidation and naming things”).
  • TTL-based expiry: entries simply expire after a fixed time (see TTL), regardless of whether the data has really changed — simple, but imprecise (either too short and the cache does little good, or too long and users see outdated data).
  • Stale-while-revalidate: an expired cache entry is still delivered once more, while it’s reloaded in parallel in the background — users never notice a slow request, at the cost of briefly slightly outdated data.
  • Cache tags: entries are tagged with keywords, so all affected entries can be specifically invalidated at once when, say, a particular product changes, without clearing the entire cache.

Next.js internally uses several of these strategies at once (router cache, data cache, full-route cache) — which one applies depends on the specific route and the fetch options used.

See also: Caches, TTL