Skip to main content

Caching & Rendering Architecture Design Review

A senior Next.js cache design should be explainable without starting from API names.

Start from the product and data model:

Who owns this data?
Who may see it?
How fresh must it be?
What changes it?
What should happen immediately after a write?
How expensive is recomputation?
Where is the app deployed?

Only then choose use cache, fetch caching, tags, paths, dynamic rendering, or remote handlers.

The architecture worksheet​

For each data domain, record:

QuestionExample
OwnerProduct service
VisibilityPublic
Keyproduct ID + locale
Freshness15 minutes
Mutation eventsProduct edit, publish
ConsistencyStale briefly acceptable
Invalidationproduct tag + list tag
Storagedefault/remote cache
Dynamic dependenciesnone
Failure fallbackserve stale during refresh

This table is more valuable than a blanket β€œcache all fetches” rule.

Step 1: draw the data graph​

Example commerce route:

/product/[id]
β”œβ”€β”€ product details
β”œβ”€β”€ category metadata
β”œβ”€β”€ inventory
β”œβ”€β”€ reviews summary
β”œβ”€β”€ user price entitlement
└── cart

Now classify each dependency:

product details β†’ public, hours
category metadata β†’ public, days
inventory β†’ public-ish, seconds/minutes
reviews summary β†’ public, minutes
user entitlement β†’ private, request-time
cart β†’ private, request-time

One route, six different cache contracts.

Step 2: design the rendering tree​

A strong tree might be:

Page shell
β”œβ”€β”€ Cached Product Details
β”œβ”€β”€ Cached Category Metadata
β”œβ”€β”€ Suspense
β”‚ └── Inventory
β”œβ”€β”€ Cached Reviews Summary
└── Suspense
└── Account Area
β”œβ”€β”€ Entitlement
└── Cart

This keeps public stable work reusable while private state stays request-time.

Step 3: define invalidation topology​

Product edit can affect:

product:p_42
products
category:lighting
homepage:featured

Inventory update may affect only:

inventory:p_42

Do not couple product content and inventory to one giant tag if they have different write frequency and freshness.

Step 4: choose consistency semantics​

For each mutation, choose:

immediate fresh read
β†’ updateTag where supported

stale-while-revalidate
β†’ revalidateTag(tag, 'max')

route output regeneration
β†’ revalidatePath

The API follows the consistency requirement.

Step 5: choose cache storage​

Ask:

single process?
many containers?
serverless?
multi-region?
how expensive is a miss?

Then decide:

  • local/default use cache
  • remote/shared handler
  • source-level cache
  • no persistent cache

Do not add remote caching merely because the deployment is distributed. A cheap query may not justify another network lookup.

Public CMS architecture​

A CMS-driven marketing site often benefits from:

long cache life
+
tag-based webhook invalidation
+
stale-while-revalidate fallback

Example tags:

pages
page:about
navigation
footer

A webhook should authenticate before invalidating.

SaaS dashboard architecture​

A dashboard may combine:

public plan catalog β†’ cached
organization metadata β†’ tenant-scoped cache or dynamic
permissions β†’ dynamic/short carefully controlled cache
usage metrics β†’ short-lived cache
current user β†’ dynamic
notifications β†’ client/live or dynamic

Do not cache the entire dashboard as one user-specific blob unless you have a deliberate per-user cache system and a strong reason.

B2B multi-tenant design​

For tenant-private cacheable content:

cache key includes trusted tenant scope
invalidation tags include tenant scope
permission check happens before returning data

Example:

tenant:t_42:catalog
tenant:t_42:catalog:item:p_9

If tenant ID comes from URL/cookie, validate and authorize it before cache use.

Search architecture​

Search queries create high cardinality:

?q=a
?q=ab
?q=abc
...

Caching every query may have poor reuse.

Consider:

  • only cache popular canonical queries
  • cache expensive shared search indexes, not every response
  • keep personalized ranking dynamic
  • bound query length/filter combinations
  • normalize cache keys

A technically cacheable result is not automatically worth caching.

Inventory architecture​

Inventory often needs tighter freshness than catalog content.

Avoid:

product details + inventory
β†’ one 24-hour cache

Instead:

product metadata β†’ hours
inventory β†’ seconds/minutes or dynamic

The UI can render the stable product shell while inventory resolves separately.

Authorization architecture​

Never make cache presence proof of access.

Wrong:

if cached project exists β†’ return it

Correct:

resolve identity
↓
authorize resource scope
↓
read/reuse permitted data

For public data, authorization is irrelevant. For private data, it is foundational.

Build-time vs runtime review​

For each route, ask:

what executes during next build?
what can be part of prerender shell?
what waits for request?
what runs after navigation?

This catches hidden assumptions about time/randomness and request APIs.

Cache Components adoption strategy​

For an existing previous-model App Router app:

1. inventory route segment config​

Find:

dynamic
revalidate
fetchCache

2. identify real cache boundaries​

Map each route-wide rule to actual data/functions.

3. enable Cache Components in a controlled branch​

cacheComponents: true

4. add use cache only to reusable work​

5. define profiles/tags​

6. add Suspense around request-time holes​

7. run production build​

8. test hard load, soft navigation, mutation, and invalidation​

Do not enable the flag and then mechanically copy every old route-wide policy.

Migration from unstable_cache​

Legacy code may use unstable_cache.

The modern direction is Cache Components and use cache.

Migration should preserve:

  • cache identity
  • freshness
  • invalidation tags
  • security scope
  • deployment behavior

Do not migrate by API shape alone.

Migration from old PPR flags​

Historical experiments may use old standalone PPR/dynamicIO/use-cache-related flags.

Next.js 16 consolidates the current model under cacheComponents.

Treat old flags as migration history, not the primary curriculum.

Performance budget​

Cache design should have measurable goals:

origin DB QPS
server render latency
cache hit ratio
remote cache latency
RSC payload time
navigation latency
revalidation frequency

A cache change is successful only if it improves the intended metric without violating correctness.

Cost model​

Example:

DB query = 20 ms
remote cache lookup = 15 ms
hit ratio = 10%

Remote cache may be pointless.

Another:

aggregation = 1.5 s + expensive vendor API
remote cache = 20 ms
hit ratio = 95%

Remote caching may be high-value.

Measure real workloads.

Failure-mode design​

For each cache, decide behavior when:

  • cache unavailable
  • source unavailable
  • invalidation unavailable
  • entry corrupted
  • schema changes
  • deployment rolls back

Possible policies:

serve stale
fallback to source
fail request
fallback to local cache
return partial UI

Security-sensitive data may require stricter failure than public content.

Design review checklist​

Correctness​

  • cache owner identified
  • freshness contract documented
  • invalidation events mapped
  • read-your-own-write requirement explicit
  • previous-model and Cache Components APIs not mixed

Security​

  • shared entries contain only safe data
  • key includes every required isolation dimension
  • authorization remains authoritative
  • cache persistence/compliance reviewed

Rendering​

  • stable shell maximized without caching private state
  • dynamic holes are behind useful Suspense boundaries
  • request APIs live close to their consumers
  • connection() used only for intentional runtime work

Operations​

  • hit/miss metrics available
  • invalidation observable
  • multi-instance behavior understood
  • cold-start/stampede behavior acceptable
  • production build tested

Senior interview scenario​

Scenario: A team has a /dashboard route with dynamic = 'force-dynamic' because one widget reads a session cookie. The page also loads a large public analytics benchmark that changes once per day.

Strong redesign:

Cache Components on
↓
public benchmark β†’ `use cache` + days profile + tag
session widget β†’ request-time subtree behind Suspense
rest of shell β†’ prerender/cache as appropriate

The goal is not to make the dashboard static. It is to stop one private dependency from forcing unrelated public work to recompute.

Senior interview scenario: mutation consistency​

Scenario: Admin edits a product and must immediately see the new title. Public users can tolerate the old category list for a few seconds.

Strong policy:

product detail tag β†’ updateTag for immediate read-your-own-write
category list tag β†’ revalidateTag(..., 'max') for background refresh

Different consumers can have different consistency requirements after one mutation.

Milestone project​

Design caching for a marketplace with:

  • public home page
  • product/category/search
  • inventory
  • seller dashboard
  • buyer cart
  • CMS pages
  • webhook updates
  • three deployment regions

Deliver:

  1. route rendering tree
  2. cache-boundary map
  3. cache profiles
  4. tag taxonomy
  5. path invalidation cases
  6. dynamic request boundaries
  7. remote-cache decision
  8. security/isolation review
  9. incident runbook
  10. performance metrics and success thresholds

The result should be understandable by another engineer without reading every implementation file.