Question presented to candidate: "You have a getProductStats() function that runs an expensive SQL query. When would you wrap it in React's cache, when in unstable_cache, and what would you use in Next.js 16?"
What a strong answer should cover:
- React
cache()memoizes a function for one server request: duplicate calls in the same render run once, and the next request starts fresh. unstable_cachefrom next/cache stores the result across requests (and deployments) withrevalidateandtags, like the Data Cache for non-fetch functions.- They solve different problems and can be combined: persistence plus per-request dedupe.
- In Next.js 16 the unstable_cache docs say it "has been replaced by use cache" (the 16.4 migration guide adds that it keeps working, so migrating is optional); with Cache Components you mark the function with 'use cache' and use
cacheLifeandcacheTag. - Never persist per-user results in a shared cache unless the user id is part of the key.
Clarifying questions expected:
- "Must the result be fresh on every request?" — then only per-request dedupe applies.
- "Is Cache Components enabled?" — decides between unstable_cache and 'use cache'.
Code / implementation expected: Yes — the same function wrapped both ways, and the Next.js 16 version.