Skip to the page

Caching in Next.js 16 · senior

"use cache", cacheLife and cacheTag

"use cache" caches the return value of an async function, component or whole file; its key is built from the build id, the function id, its serialized arguments and captured variables, its lifetime comes from cacheLife, and cacheTag names it for on-demand invalidation.

The notes

What you must know

  • Place at the top of an async function, component or file (file level caches every export, all must be async). It does nothing inside proxy.ts.
  • Cache key = build id (or deploymentId) + function id + serialized arguments + captured closure variables. So entries do not survive a new deploy.
  • Arguments must be serializable (RSC rules); return values can include JSX. Non-serializable values like children or Server Actions can be passed through if the function does not read them (interleaving).
  • You cannot call cookies(), headers() or read searchParams inside a cached scope (or in helpers it calls). Read them outside and pass the values in as arguments; they become part of the key.
  • cacheLife presets (stale / revalidate / expire): default 5m/15m/never; seconds 30s/1s/1m; minutes 5m/1m/1h; hours 5m/1h/1d; days 5m/1d/1w; weeks 5m/1w/30d; max 5m/30d/1y. Always set one explicitly. stale is the client cache time (minimum 30 s).
  • Nesting: an explicit outer cacheLife wins; without one, a shorter inner lifetime shrinks the outer; a short-lived inner cache inside an outer one without explicit cacheLife fails the build.
  • Runtime storage is an in-memory LRU per instance: on serverless it often does not persist between requests. "use cache: remote" uses a shared cache handler (Redis/KV; network round trip, pays off at high hit rates). "use cache: private" may read cookies/headers, is stored only in the browser, and can join prefetches.
  • React.cache values from outside are not visible inside a "use cache" scope; pass data as arguments. Draft Mode bypasses caches.

The red pen

Common traps: mistake → fix

  • Mistake: "Put 'use cache' on every page"

    Fix: personalised or per-request data then needs the user in the key, or it leaks.

  • Mistake: "Read cookies inside the cached function"

    Fix: error; read outside and pass values.

  • Mistake: "It survives deploys like the old Data Cache"

    Fix: keys include the build id.

Easy to hard

Questions interviewers ask, easy to hard

  1. Explain "use cache". What becomes the cache key?
  2. Can you read cookies inside a cached function?
  3. Page-level "use cache" on a dashboard: what goes wrong?
  4. In-memory cache on serverless: why might it re-run every request?
  5. When would you use "use cache: private" or "use cache: remote"?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview