Skip to content
← All posts

Nov 19, 2026

You saved the change, but the dashboard still shows the old numbers

The write worked. The read is stale. A cache that never hears about a write becomes a second source of truth that disagrees with the first one.

You change a shop's name in the settings form. The save succeeds — no error, the toast says it worked. You go back to the list and the old name is still there. You refresh. Still the old name. You check the database: the new name is in there. Nothing is broken, and yet the dashboard is lying to you.

The write worked. The read is stale. Somewhere between the database and the screen, a cache is holding on to the old value, and nobody told it the data changed. A cache that never gets told about a write is not a performance optimisation — it is a second source of truth that disagrees with the first one.

The fix is a rule, not a library: every write has to invalidate exactly what it changed, and it has to do it after the write succeeds. Not before, not on a timer — after the database confirms the new value. Invalidate before the write and a failed save still refreshes the cache against the old data. Invalidate on a timer and the user stares at the old value until the timer fires.

On the client, the cache is usually a data-fetching library, and it has a built-in way to do this. In RTK Query, a mutation declares which tags it invalidates, and every query that provides those tags refetches:

updateShop: builder.mutation<Shop, { id: string; name: string }>({
  query: ({ id, ...body }) => ({
    url: `/shops/${id}`,
    method: "PATCH",
    body,
  }),
  invalidatesTags: (_result, _error, { id }) => [
    { type: "Shop", id },
    { type: "Shop", id: "LIST" },
  ],
}),

The list tag matters as much as the detail tag. Invalidate only the single shop and the detail page is fresh while the list still shows the old name — the exact bug you were trying to fix, one screen over. Name the thing that changed and everything that shows it.

With SWR the shape is the same idea with a different name. After the POST succeeds, call mutate on the key that holds the list, and it refetches:

async function renameShop(id: string, name: string) {
  await fetch(`/api/shops/${id}`, {
    method: "PATCH",
    body: JSON.stringify({ name }),
  });
  mutate("/api/shops"); // refetch the list now that the write landed
}

Two details worth knowing. First, the global mutate with only a key does nothing unless a mounted hook is actually using that key — the bound mutate from the hook itself is the safer call when you have it. Second, this is where optimistic updates fit: you can update the cache immediately and let the refetch confirm it, but the refetch after a successful write is not optional. I wrote about the optimistic half last week — this is the part that makes it honest.

On the server, Next.js has the same problem in a different layer. A Server Component that fetched the shop list has its result in the Data Cache, and the rendered page in the Route Cache. A Server Action that updates the shop can clear both with one call after the write:

import { revalidateTag } from "next/cache";

export async function renameShop(id: string, name: string) {
  await db.shop.update({ where: { id }, data: { name } });
  revalidateTag(`shop:${id}`);
  revalidateTag("shop-list");
}

Tag the fetches when you read them, invalidate the same tags when you write. revalidateTag marks the entry stale rather than rebuilding it on the spot — the next request regenerates it — so if the user needs to see their own change in the render the action itself triggers, the Server Action-only updateTag expires it immediately instead. revalidatePath is the blunt version: it clears everything a route rendered, including entries that were still correct. Reach for it when a whole page's worth of data changed; prefer tags when you know exactly which entity moved.

Three honest limits. Invalidation only reaches caches your code controls. Another user with the page already open, or a second tab, keeps showing the old value until something makes them refetch — a focus event, a poll, or a live update. That is a different problem, and no amount of invalidating on your side fixes it. On a multi-instance deployment, an invalidation on one server does not reach the other server's cache unless the cache is shared or the invalidation is broadcast — a shared data store alone does not propagate it. And resist the lazy fix of invalidating everything after every write. revalidatePath("/") after a name change works, and it also throws away every warm cache on the site for a one-field edit. The whole point of a cache is that most of it stays warm.

The rule I use now: a write is not finished when the database says yes. It is finished when every cache that shows that data has been told. Tag the read, invalidate on the write, and do it after the write succeeds — because the dashboard that shows yesterday's numbers after a successful save is not a caching problem. It is a write that never finished its job.