Repository navigation
Next.js Cache Invalidation (revalidateTag / revalidatePath) on Google Cloud Run with Multiple Containers #17271
|
Hi everyone, I'm running a Payload CMS project with a Next.js App Router frontend. Current setup
My concernSince Cloud Run scales horizontally, each container runs in its own isolated process. From what I understand, Next.js' Data Cache and Full Route Cache are local to each instance. This makes me think that if one container calls Is this understanding correct? GoalI'd like cache invalidation to work consistently across all Cloud Run instances while making as few code changes as possible. BackgroundWe originally hosted the project on Vercel, but we ran into several issues under load:
We moved to Google Cloud Run, and the infrastructure itself has been much more stable. However, I'm now concerned about cache consistency across multiple running containers. Questions
I'd appreciate hearing how others have solved this problem in production. Thanks! |
Replies: 3 comments 1 reply
|
Yes, your understanding is right for the default self-hosted setup: with multiple Cloud Run instances, assume each Next.js process has its own local cache, so The supported self-hosted fix is to put the Next.js cache behind shared storage with a custom cache handler, then disable the local in-memory layer: // next.config.js
module.exports = {
cacheHandler: require.resolve('./cache-handler.js'),
cacheMaxMemorySize: 0,
}For tag-based invalidation, the important part is not only storing cached entries in Redis/S3/etc.; the handler also needs distributed tag coordination ( |
|
Short version: The clean fix on Cloud Run is a shared cache handler backed by Redis (Upstash / Memorystore) so all N replicas observe the same tag timestamps. 1. Install the shared handler pnpm add @neshca/cache-handler @neshca/json-replacer-reviver2. const { CacheHandler } = require("@neshca/cache-handler");
const createRedisHandler = require("@neshca/cache-handler/redis-strings").default;
const { createClient } = require("redis");
CacheHandler.onCreation(async () => {
const client = createClient({ url: process.env.REDIS_URL });
client.on("error", (e) => console.error("redis cache-handler", e));
await client.connect();
const redisHandler = await createRedisHandler({
client,
keyPrefix: "nx:",
timeoutMs: 5000,
});
return { handlers: [redisHandler] };
});
module.exports = CacheHandler;3. module.exports = {
cacheHandler: require.resolve("./cache-handler.js"),
cacheMaxMemorySize: 0, // disable in-process LRU; Redis is the source of truth
};Now 4. Escape hatch for hot pages that can't tolerate any staleness Skip the Data Cache entirely on those routes: export const dynamic = "force-dynamic"; // or: export const revalidate = 0Then let Cloud CDN handle edge caching via 5. Cloud Run gotchas that will bite you
tl;dr — |
Short version:
revalidateTag()/revalidatePath()only invalidate the Next.js cache on the container that received the request. Each Cloud Run replica has its own in-memory Data Cache / Full Route Cache — nothing is broadcast between them by default. What you're seeing is expected behavior of the default cache handler.The clean fix on Cloud Run is a shared cache handler backed by Redis (Upstash / Memorystore) so all N replicas observe the same tag timestamps.
1. Install the shared handler
2.
cache-handler.jsat the repo root: