DEV Community

Mahmut Gündüzalp
Mahmut Gündüzalp

Posted on

revalidateTag Only Cleared One of Our Two Sites: Same Next.js Code, Same Database, Separate Caches

We run one Next.js codebase as two separate deployments. Two domains, two processes, one MySQL database. One site is about our news software, the other covers everything else we build. Blog posts live in the same table; a flag decides which site shows a post, and the other site redirects to it.

Publishing works through a scheduled endpoint. A post is inserted with status scheduled and a publish time, and a cron job calls an API route every ten minutes. The route flips due posts to published and clears the cache tag that the list pages use.

This morning I published five posts. Two belonged to the first site and three to the second. The detail pages of all five returned 200 on the right domain. The first site's blog list showed its two new posts. The second site's blog list still ended with yesterday's post.

The route

This is what the endpoint looked like:

import { NextResponse } from "next/server";
import { revalidateTag } from "next/cache";
import { prisma } from "@/lib/prisma";

export async function GET(req: Request) {
  const token = new URL(req.url).searchParams.get("token");
  if (!token || token !== process.env.CRON_TOKEN) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  const result = await prisma.post.updateMany({
    where: { status: "scheduled", publishAt: { lte: new Date() } },
    data: { status: "published" },
  });

  if (result.count > 0) revalidateTag("posts", { expire: 0 });

  return NextResponse.json({ ok: true, published: result.count });
}
Enter fullscreen mode Exit fullscreen mode

And the list query is cached with that tag:

export const getPosts = unstable_cache(
  async () => prisma.post.findMany({ where: { status: "published" }, orderBy: { publishAt: "desc" } }),
  ["posts"],
  { revalidate: 300, tags: ["posts"] }
);
Enter fullscreen mode Exit fullscreen mode

Nothing wrong in either file on its own. The bug is in where they run.

Why only one site was refreshed

revalidateTag does not send anything to a central place. It marks entries as stale in the cache of the Next.js server that executes it. With the default setup, that cache lives inside the deployment: in memory and in that build's own cache directory. Two deployments of the same code have two independent caches, even when they read from the same database.

Our cron line called only the first deployment. So the sequence was:

  1. Cron calls the endpoint on site A.
  2. Site A updates five rows to published. count is 5, so site A clears its posts tag.
  3. Site B never hears about it. Its cached list is still the old one.

Adding a second cron line for site B does not fix it either, and this is the part I want to point out. Whichever deployment runs first flips the rows. When the second one runs, updateMany finds nothing due, count is 0, and the if (result.count > 0) guard skips the revalidation. The guard was written to avoid clearing the cache every ten minutes for nothing. In a two-deployment setup it guarantees that exactly one site gets refreshed.

The revalidate: 300 time limit is supposed to be the safety net. In practice, more than five minutes after publishing, site B's list still did not show the posts. I did not wait to find out how long it would take. A safety net you cannot put a number on is not something to rely on for a publish button.

The quick way out, and why it is not a fix

The immediate workaround was to set one of the new posts back to scheduled and call the endpoint on site B directly. Site B flipped one row, count was 1, its cache was cleared, and the list showed all three posts.

That works once. It also depends on a person remembering the trick every time a post goes out, which is exactly what a scheduled publish is supposed to remove.

The fix: each process tracks what it has seen

The question each deployment should ask is not "did I publish something?" but "has the newest published post changed since I last looked?" The answer to that is in the database, so every deployment can answer it for itself.

let lastSeenPublish: number | null = null;

export async function GET(req: Request) {
  const token = new URL(req.url).searchParams.get("token");
  if (!token || token !== process.env.CRON_TOKEN) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  const now = new Date();
  const result = await prisma.post.updateMany({
    where: { status: "scheduled", publishAt: { lte: now } },
    data: { status: "published" },
  });

  const newest = await prisma.post.aggregate({
    where: { status: "published", publishAt: { lte: now } },
    _max: { publishAt: true },
  });
  const newestTime = newest._max.publishAt ? newest._max.publishAt.getTime() : 0;

  const refreshed = result.count > 0 || newestTime !== lastSeenPublish;
  lastSeenPublish = newestTime;

  if (refreshed) revalidateTag("posts", { expire: 0 });

  return NextResponse.json({ ok: true, published: result.count, cacheRefreshed: refreshed });
}
Enter fullscreen mode Exit fullscreen mode

lastSeenPublish is a module-level variable, so it lives as long as the server process. After a restart it is null, which means the first call always refreshes. That is the safe direction: one extra revalidation after a deploy costs nothing.

Then both deployments get their own cron line, each calling its own process on localhost.

Testing it with the real sequence

I tested the exact scenario that failed, with the calls in the order the cron would make them. Site A is the one that usually wins the race.

1  B first call   {"published":0,"cacheRefreshed":true}    <- fresh process, null watermark
2  B again        {"published":0,"cacheRefreshed":false}
   (one post set back to scheduled)
3  A              {"published":1,"cacheRefreshed":true}    <- A flips the row
4  B              {"published":0,"cacheRefreshed":true}    <- B sees the newer post, clears its own cache
5  B again        {"published":0,"cacheRefreshed":false}   <- no repeated work
6  A again        {"published":0,"cacheRefreshed":false}
Enter fullscreen mode Exit fullscreen mode

Line 4 is the one that matters. Before the change it would have been published: 0 and nothing else. After it, site B's list showed the post on the next request.

The extra cost is one aggregate query every ten minutes per deployment. MAX() on an indexed date column is cheap.

What this does not fix

This change covers the scheduled publish path only. Anything else that changes posts in one deployment still revalidates only that deployment:

  • Editing a post in the admin panel runs a server action on site A. It clears site A's cache. Site B keeps the old title until its time-based revalidation catches up.
  • Deleting or hiding a post, same thing.
  • Raw SQL changes revalidate nothing anywhere. We already had a rule for that: push a post through the scheduled cycle afterwards.

The watermark only looks at the newest publish time, so an edit to an older post does not move it. You could widen the check (for example MAX(updatedAt) instead of MAX(publishAt)), and that would catch edits too, as long as every write path updates that column.

The proper solution for multiple deployments is a shared cache. Next.js lets you plug in a custom cache handler (cacheHandler in next.config), and with a shared store such as Redis, a revalidateTag in one process is visible to all of them. The other common option is a fan-out: the process that changes data calls a small authenticated revalidate endpoint on every deployment. Both are more moving parts than we wanted for a cron route, so the watermark is our fix for publishing, and the shared cache handler is on the list for the admin path.

Takeaways

  • revalidateTag is local to the Next.js server that runs it. Separate deployments mean separate caches, even with the same code and the same database.
  • A guard like if (count > 0) revalidate() is correct for one deployment and quietly wrong for two: only the deployment that wins the race refreshes.
  • Ask the database what changed, not the current request. A per-process watermark of the newest publish time lets every deployment refresh itself exactly once.
  • Return the decision in the response (cacheRefreshed). It made the test above readable and it shows up in cron logs when something is off.
  • Check the list page on every domain after publishing, not only the detail pages. Our detail pages were fine; the list was the part that lied.

We build web software, mobile apps and news software in Turkey: alestasoft.com

Top comments (1)

Collapse
 
sunnatulla_dev profile image
Sunnatulla •

The watermark misses a backdated post. If someone schedules a post with a publishAt older than the newest published one (an imported article keeping its original date), site A flips it and refreshes, but B's MAX(publishAt) doesn't move. I simulated it and B returned cacheRefreshed: false. Using count plus the max as the signature catches it, and the count catches deletes too.