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:
| Question | Example |
|---|---|
| Owner | Product service |
| Visibility | Public |
| Key | product ID + locale |
| Freshness | 15 minutes |
| Mutation events | Product edit, publish |
| Consistency | Stale briefly acceptable |
| Invalidation | product tag + list tag |
| Storage | default/remote cache |
| Dynamic dependencies | none |
| Failure fallback | serve 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:
- route rendering tree
- cache-boundary map
- cache profiles
- tag taxonomy
- path invalidation cases
- dynamic request boundaries
- remote-cache decision
- security/isolation review
- incident runbook
- performance metrics and success thresholds
The result should be understandable by another engineer without reading every implementation file.