Self-hosting and cache handlers
Running next start yourself is fully supported, but caching deserves attention.
By default the Next.js server cache — used by ISR and other server cache paths —
lives in memory and on the local disk of each instance. With one server and
persistent disk that is fine. With several instances, each keeps its own copy, so
they drift apart.
Fix it with a custom cache handler:
// next.config.js
module.exports = {
cacheHandler: require.resolve('./cache-handler.js'),
cacheMaxMemorySize: 0, // disable the default in-memory cache
};
The handler exports a class implementing get, set, revalidateTag, and
resetRequestCache, and stores entries where every instance can reach them —
Redis, S3, or another shared store.
Cache Components' use cache entries use the plural cacheHandlers config
instead:
// next.config.js
module.exports = {
cacheHandlers: {
default: require.resolve('./cache-handlers/default-handler.js'),
remote: require.resolve('./cache-handlers/remote-handler.js'),
},
};
With a shared handler, cached data survives a restart and is shared across
instances, so a revalidateTag on one server is seen by them all.
You are about to answer a short quiz on server cache storage.