Study notes · 10% of the exam

Caching and Revalidation

In Next.js 16 caching is explicit. With Cache Components you mark work with "use cache", give it a lifetime with cacheLife, tag it with cacheTag, and invalidate it with updateTag, revalidateTag or revalidatePath. Know which layer you are touching: render-pass memoization, the server cache, prerendered HTML, or the client router.

Key points

  1. 1

    Caching is opt-in. A plain fetch is not stored in the persistent cache; identical GET fetches are only memoized for one render pass (not in Route Handlers).

  2. 2

    "use cache" needs cacheComponents: true. Its key is the build ID, a function ID and the serialized arguments, with captured closure variables bound as arguments.

  3. 3

    Cached scopes cannot read cookies(), headers() or searchParams. Read them outside and pass the value in, or use "use cache: private", which only caches per request and in the browser.

  4. 4

    cacheLife: stale is the client router window (minimum 30 s), revalidate triggers background regeneration, and expire is the point after which a request waits. Without a cacheLife call, default applies (5 m / 15 m / never).

  5. 5

    updateTag is for read-your-own-writes and only works in Server Actions. revalidateTag(tag, profile) serves stale content while revalidating, and { expire: 0 } is the immediate option for webhooks.

  6. 6

    Default runtime entries live in per-instance memory. Serverless rarely reuses them, so use "use cache: remote" with a cacheHandlers backend to share them. No "use cache" entry survives a new deploy.

  7. 7

    On multi-instance self-hosting, revalidation is local unless the cache handler syncs tags through updateTags() and refreshTags().

Common traps

  • revalidatePath("/blog") does not refresh other pages that share the same tagged data, and with rewrites you must pass the destination path.

  • unstable_cache keys do not include closure variables. Leave the user ID out of the key and every user is served the first user's data.

  • A short-lived cache (seconds, revalidate: 0, or expire under 5 minutes) nested in a "use cache" scope with no explicit cacheLife fails the build. Always set an explicit lifetime.

Test yourself on Caching and Revalidation

Ten questions, with the answer and explanation after each one.