Skip to content
← All posts

How to Choose a Useful use cache Boundary

A useful use cache boundary surrounds work that is expensive enough to reuse, has identifiable inputs, and can tolerate a defined freshness policy. It should not be placed around a whole page merely because the page is slow.

Start with the result you want to reuse. Then decide what must be true for two requests to share it.

Describe the result before adding the directive

Take a product page with a description, regional price, and cart. “Cache the product page” is too broad. A better starting point is “reuse the public description for this product and language.”

The use cache reference explains that inputs participate in the cache key and that request APIs must be read outside ordinary cached scopes. Treat those details as design constraints, not syntax trivia.

This example deliberately gives each result its own contract. Different applications may choose different lifetimes or keep price fully dynamic.

Make the inputs boring

A cache key is easier to reason about when its inputs are stable primitives. Pass a product ID and a supported language, not an entire request object or an arbitrary query-string collection.

Normalize only what is safe to normalize. Two language aliases may represent the same content. Two signed URLs may not. Do not remove information merely to increase the hit rate.

For a code review, write the invariant next to the function: the same product ID and language should return the same public description until the declared freshness or invalidation rule allows it to change.

Keep the policy at the boundary

Here is a schematic data-layer function. readPublicDescription is your application's validated public-data reader, not a Next.js API.

import 'server-only';
import { cacheLife, cacheTag } from 'next/cache';
import { readPublicDescription } from './catalog-data';

export async function getDescription(productId: string, language: 'en' | 'ja') {
  'use cache';
  cacheLife('hours');
  cacheTag(`description:${productId}:${language}`);

  return readPublicDescription(productId, language);
}

This uses a preset for illustration, not a recommendation that every product description should use that lifetime. cacheLife makes the freshness choice explicit. An editor-triggered invalidation may still be needed when a correction should appear sooner.

Keep authorization outside this public helper. If the data is not public, redesign the function's contract before adapting the example.

Data-level and UI-level reuse solve different problems

A cached data function can be shared by several views. A cached UI component can reuse the result of rendering a particular presentation.

Choose the boundary based on what repeats. If several pages read the same normalized catalog record but render it differently, the data boundary is a natural candidate. If an expensive public section appears unchanged, UI-level reuse may be worth testing.

Avoid layering several caches by habit. Each additional layer introduces another place where old data might remain. Record which layer owns the freshness decision.

Prove the boundary with three changes

Make repeat requests with the same inputs and observe the expensive reader. Then change a meaningful input, such as language. Finally, update the underlying description and invoke its invalidation path.

The test should demonstrate reuse, separation, and refresh. A high hit rate proves only the first of those.

A good cache boundary makes the application easier to explain: this result is shared by these requests, for this long, and refreshed by this event. When that sentence is hard to finish, the boundary is probably still too broad.

About the author