How should related use cache entries share the same freshness boundary?
Unanswered
Bumble bee posted this in #help-forum
Bumble beeOP
I'm trying to understand the recommended caching pattern in Next.js 16 when multiple cached functions return overlapping data from the same upstream source.
For example:
And elsewhere:
Consider this timeline:
Since both cache entries were created at different times, their revalidation windows are also offset.
Conceptually, this could result in:
So for some period of time, different parts of the application may expose different versions of the same underlying resource.
One important constraint is that the upstream source does not provide webhooks or any other notification mechanism when data changes, so I cannot invalidate the cache exactly when an update happens.
What is the recommended way to model this with Cache Components in Next.js 16?
Ideally, I'd like related cache entries to share a synchronized freshness boundary.
For example, everything cached during:
would belong to the same logical cache generation, and after 14:00 any subsequent access would use/fetch data belonging to the next generation, regardless of whether a particular cache entry was initially created at 13:05 or 13:45.
Some approaches I've considered:
- using a shared
- passing a time-based generation/window as an argument to cached functions
- caching only the aggregate resource (
- coordinating invalidation externally with a cron job and
Is there an intended/recommended Next.js pattern for this?
I'm specifically interested in synchronizing the freshness boundary of separate cache entries, rather than making them share the same cache entry.
For example:
async function getAllItems() {
'use cache'
cacheLife('hours')
return fetchAllItems()
}And elsewhere:
async function getItemById(id: number) {
'use cache'
cacheLife('hours')
return fetchItemById(id)
}getAllItems() contains item 39, so both functions can represent the same underlying data, but they are separate cache entries.Consider this timeline:
13:00 → getAllItems() is accessed and cached
13:30 → getItemById(39) is accessed and cached
13:45 → the upstream source updates item 39Since both cache entries were created at different times, their revalidation windows are also offset.
Conceptually, this could result in:
getAllItems() → eligible for refresh around 14:00
getItemById(39) → eligible for refresh around 14:30So for some period of time, different parts of the application may expose different versions of the same underlying resource.
One important constraint is that the upstream source does not provide webhooks or any other notification mechanism when data changes, so I cannot invalidate the cache exactly when an update happens.
What is the recommended way to model this with Cache Components in Next.js 16?
Ideally, I'd like related cache entries to share a synchronized freshness boundary.
For example, everything cached during:
13:00–13:59would belong to the same logical cache generation, and after 14:00 any subsequent access would use/fetch data belonging to the next generation, regardless of whether a particular cache entry was initially created at 13:05 or 13:45.
Some approaches I've considered:
- using a shared
cacheTag() and invalidating the tag on a fixed schedule- passing a time-based generation/window as an argument to cached functions
- caching only the aggregate resource (
getAllItems) and deriving getItemById() from it- coordinating invalidation externally with a cron job and
revalidateTag()Is there an intended/recommended Next.js pattern for this?
I'm specifically interested in synchronizing the freshness boundary of separate cache entries, rather than making them share the same cache entry.