billing hacks

The Four Cache Layers

Next.js has four caching layers. Here's the 30-second overview.

requestCDNedge cacheRoutepre-renderedDatafetch cacheOrigin$$$ costlycache hit = free

TL;DR

Caching is the highest-leverage optimization. Four layers: CDN Cache (full responses), Route Cache (pre-rendered pages), Data Cache (fetch results), Request Memoization (deduping). Anything cacheable should be cached.

Caching is the highest-leverage optimization. A cached response costs nothing. An uncached response runs your function every time.

The Layers

Request → CDN Cache → Full Route Cache → Data Cache → Request Memoization → Origin
LayerWhat It CachesCost Impact
CDN CacheFull responses at edgeEliminates function calls
Route CachePre-rendered pagesEliminates SSR
Data Cachefetch() resultsReduces external calls
Request MemoizationDuplicate fetches in renderMinor optimization

Which Cache Layer Should You Use?

CACHE LAYER DECISIONWhat are you caching?Full pageAPI responseData fetchRoute CacheSSG / ISR pre-renderingCDN CacheCache-Control headersData Cachefetch() with revalidateBest for: Marketing, docs, blogsBest for: Public API endpointsBest for: External API calls99%+ savings95%+ savings80%+ savings

Which Layer Is Active?

Check your build output:

npm run build
  • ○ Static - Route Cache active
  • ● SSG with params - Route Cache active
  • ƒ Dynamic - No Route Cache, relies on CDN/Data cache

Cost Impact

ScenarioWithout CachingWith CachingSavings
Product page (10K/day)10K runs24 runs (ISR)99.8%
API endpoint (100K/day)100K runs~300 runs99.7%
Blog post5K runs1 run99.98%

The pattern: anything cacheable should be cached.


Start with CDN Cache for API routes, or Route Cache for pages.

On this page