0
0
NextJSframework~10 mins

Cache invalidation strategies in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Cache invalidation strategies
Start: Data Cached
Check Cache Validity
Use Cache
Fetch Fresh Data
Update Cache
End
This flow shows how cache is checked for validity, used if valid, or invalidated and refreshed if not.
Execution Sample
NextJS
export async function getStaticProps() {
  const data = await fetchData()
  return {
    props: { data },
    revalidate: 10,
  }
}
This Next.js code fetches data at build time and sets cache to revalidate every 10 seconds.
Execution Table
StepActionCache StateConditionResult
1Initial buildCache emptyNo cacheFetch data and cache it
2Request at 5sCache valid5 < 10 secondsUse cached data
3Request at 12sCache expired12 > 10 secondsInvalidate cache and fetch fresh data
4Request at 15sCache valid3 seconds since last fetch < 10Use cached data
5Request at 25sCache expired10 seconds since last fetchInvalidate cache and fetch fresh data
6EndCache updatedN/AStop
💡 Cache is invalidated and refreshed when time since last fetch exceeds revalidate period (10 seconds).
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
cacheTimestampnull0s (build time)0s (still valid)12s (updated)12s (valid)25s (updated)25s
cacheDatanulldata fetcheddata reuseddata refresheddata reuseddata refresheddata refreshed
Key Moments - 2 Insights
Why does the cache get invalidated at 12 seconds but not at 5 seconds?
Because the revalidate time is 10 seconds, at 5 seconds the cache is still valid (5 < 10), but at 12 seconds it is expired (12 > 10), so it triggers invalidation and data refresh as shown in execution_table rows 2 and 3.
What happens to the cache data after invalidation?
After invalidation, fresh data is fetched and the cache is updated with new data and timestamp, as seen in execution_table rows 3 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the cache state at step 4?
ACache valid
BCache expired
CCache empty
DCache invalidated
💡 Hint
Check the 'Cache State' column for step 4 in the execution_table.
At which step does the cache get invalidated for the first time?
AStep 2
BStep 3
CStep 1
DStep 5
💡 Hint
Look for the first 'Cache expired' and 'Invalidate cache' action in the execution_table.
If the revalidate time changed to 20 seconds, when would the cache invalidate next after step 1?
AAt 12 seconds
BAt 5 seconds
CAt 25 seconds
DAt 21 seconds
💡 Hint
Compare the revalidate time with the timestamps in variable_tracker to find when cache expires.
Concept Snapshot
Cache Invalidation Strategies in Next.js:
- Use revalidate in getStaticProps to set cache lifetime.
- Cache is valid until revalidate seconds pass.
- After expiry, cache is invalidated and fresh data fetched.
- This keeps data fresh without rebuilding every request.
- Helps balance speed and data accuracy.
Full Transcript
This visual execution shows how Next.js cache invalidation works using the revalidate property in getStaticProps. Initially, data is fetched and cached at build time. Subsequent requests within the revalidate period use cached data. Once the revalidate time passes, the cache is invalidated and fresh data is fetched and cached again. This cycle repeats to keep data fresh while improving performance by avoiding unnecessary fetches. Variables like cacheTimestamp and cacheData track cache freshness and content. Key moments clarify why cache is used or refreshed at specific times. Quiz questions test understanding of cache state changes and timing.