How do you implement caching strategies in Next.js?

Decide per piece of data how fresh it must be, then use the matching tool: prerender static content, cache shared data with "use cache" plus cacheLife and cacheTag, stream per-request data behind <Suspense>, deduplicate within a request with React.cache, and invalidate on writes with updateTag or revalidateTag. This describes Cache Components, the default for new apps in Next.js 16.4.

A practical decision table

DataStrategy
Marketing copy, docsStatic: no I/O, prerendered automatically
CMS content"use cache" + cacheLife("max") + cacheTag, webhook calls revalidateTag(tag, "max")
Product catalog"use cache" + cacheLife("hours")
User's own editsupdateTag in the Server Function for read-your-writes
Per-user dashboardRequest-time inside <Suspense>; cache per user with arguments or "use cache: private"
Live pricesUncached, streamed; or client polling with SWR

Layers to know

  1. Request memoization: fetch and React.cache dedupe within one render.
  2. use cache entries: in-memory LRU per server by default; "use cache: remote" for a shared durable store (configure cacheHandlers).
  3. Prerendered output: the static shell and ISR pages (served by a CDN).
  4. Client cache: the router keeps RSC payloads for the stale time (minimum 30 seconds), and keeps visited pages in <Activity>.
  5. HTTP/CDN caching: Cache-Control headers on Route Handlers and assets.

Rules

  • Pair every "use cache" with a cacheLife.
  • Never cache per-user data under a shared key.
  • Prefer tags over revalidatePath; they are precise.
  • Keep request-time reads deep in the tree so the static shell is large.
  • Cache entries are scoped to a deployment; use fetch cache or unstable_cache if data must survive deploys.

Without Cache Components

fetch options (force-cache, next.revalidate, next.tags), unstable_cache, and segment config (revalidate, dynamic).

Code example

tsx
1// lib/data.ts
2import "server-only";
3import { cache } from "react";
4import { cacheLife, cacheTag } from "next/cache";
5import { db } from "@/lib/db";
6
7// Shared across users, revalidated on CMS publish
8export async function getArticle(slug: string) {
9 "use cache";
10 cacheLife("max");
11 cacheTag(`article:${slug}`);
12 return db.article.findUnique({ where: { slug } });
13}
14
15// Shared, time-based refresh
16export async function getCategories() {
17 "use cache";
18 cacheLife("hours");
19 cacheTag("categories");
20 return db.category.findMany();
21}
22
23// Per user: user id is part of the cache key
24export async function getUserStats(userId: string) {
25 "use cache";
26 cacheLife("minutes");
27 cacheTag(`stats:${userId}`);
28 return db.stats.findUnique({ where: { userId } });
29}
30
31// Per request dedupe for uncached reads
32export const getCart = cache(async (cartId: string) => db.cart.findUnique({ where: { id: cartId } }));

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app