Invental/Review lessons/Client state that goes stale

Client state that goes stale

Clearing a cache does not stop in-flight loads

When a push event invalidates a cache, every request already in flight must be able to tell it is now stale, and something must start the replacement request; deleting the cache entry alone does neither.

01 · Before and afterThe pattern

TypeScript
const gen = new Map<string, number>();
export function invalidate(id: string) { gen.set(id, (gen.get(id) ?? 0) + 1); cache.delete(id); notify(); }

async function load(id: string) {
  const started = gen.get(id) ?? 0;
  try { const data = await fetchAll(id); if ((gen.get(id) ?? 0) === started) commit(id, data); }
  catch (e) { if ((gen.get(id) ?? 0) === started) markFailed(id); }
}
// subscribers: when gen changes and a view is open, call load(id) again

02 · Run it on your appThe check

If a websocket event clears a cached value while a fetch for it is running, can the old response overwrite the new value?

If the honest answer is no or not sure, this is worth a look before it reaches production.

Want this checked on your code?

Tell us what you are building and what worries you. We match a senior reviewer to your code and confirm scope before anything starts.

— Get in touch
hi@invental.co ↗
— Or
— What to include

A link or short description of the code or system, what you want checked, your stack, and when you need the answer. No repository access is needed until scope is agreed.

— Or leave a note
We reply within one business day.