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.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe 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
childrenor Server Actions can be passed through if the function does not read them (interleaving). - You cannot call
cookies(),headers()or readsearchParamsinside a cached scope (or in helpers it calls). Read them outside and pass the values in as arguments; they become part of the key. cacheLifepresets (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.staleis the client cache time (minimum 30 s).- Nesting: an explicit outer
cacheLifewins; without one, a shorter inner lifetime shrinks the outer; a short-lived inner cache inside an outer one without explicitcacheLifefails 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.cachevalues 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
- Explain
"use cache". What becomes the cache key? - Can you read cookies inside a cached function?
- Page-level
"use cache"on a dashboard: what goes wrong? - In-memory cache on serverless: why might it re-run every request?
- When would you use
"use cache: private"or"use cache: remote"?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.