Skip to content

Feature Request: Add support for non-destructive Redis hydration via NX option in registerInitialCache (for v2.0.0) #23

@zto-sbenning

Description

@zto-sbenning

Is your feature request related to a problem? Please describe.

Yes — currently, the Redis hydration behavior during registerInitialCache() is destructive by default. When the application starts, it writes all static/build-time cache entries into Redis, overwriting any existing keys.

This causes two significant problems:

  1. On app restart: if any cache entries have been updated at runtime (e.g., via revalidateTag() or ISR revalidations), restarting the app will overwrite the fresher data with outdated build-time values.
  2. On horizontal scaling: if a new instance of the app starts later (for scaling), it can invalidate runtime-generated cache data by writing build-time defaults.

This results in stale content being served even though Redis held fresher versions.

Describe the solution you'd like

We should be able to configure the instrumentation (registerInitialCache) to perform non-destructive hydration — meaning:

  • Use the NX option of the Redis SET command,
  • So Redis keys are only written if they don’t already exist.

This could be introduced via a config flag in the instrumentation options:

registerInitialCache({
  redisClient,
  cacheEntries,
  skipIfExists: true // or: redisNX: true, or other ..
});

Internally, this would translate into Redis SET calls like:

client.set(..., { NX: true });

Additional context

This kind of feature is especially important in ISR production setups where:

  • Cache freshness is critical,
  • Redeployments or horizontal scaling are common,
  • Runtime revalidations must take precedence over build-time defaults.

Possible oversight?

It's possible that this behavior is already implemented or configurable and I may have missed it.

However, based on my testing, the current behavior results in runtime cache being overwritten by build-time values during both application restarts and horizontal scaling scenarios, as described above.

Version compatibility

From what I understand, this feature may not be feasible to implement before version v2.0.0.

Currently, registerInitialCache() relies on the legacy package @neshca/cache-handler, which sits between it and the underlying Redis client. Even if a config flag was added at the registerInitialCache level, the legacy package would not propagate it to the Redis SET operation — making the NX behavior unimplementable in practice.

However, since v2.0.0 removes this dependency and gives full control back to @fortedigital/nextjs-cache-handler, I believe this feature could be cleanly introduced as part of that version or shortly after.


Let me know if you'd like help with a PR — I'd be happy to contribute!
I'd also really appreciate your thoughts on this — curious to hear your perspective.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions