Cache Lifetime and Refetching

Every mounted Hook is a subscriber to one cache entry, and RTK Query counts them. The first subscriber triggers the request; later ones get cached data with no request; and when the count drops to zero the entry survives for keepUnusedDataFor seconds — 60 by default — before it is deleted, so a remount inside that window renders the old data immediately.

The life of one cache entry, from first subscriber to eviction
The life of one cache entry, from first subscriber to eviction

Cached does not mean fresh, so RTK Query offers several ways to refetch. refetchOnMountOrArgChange: true refetches whenever a new subscriber mounts, or takes a number of seconds meaning "only if the entry is older than this". refetchOnFocus and refetchOnReconnect react to focus and to the network coming back; both require setupListeners(store.dispatch). pollingInterval: 30000 turns a query into a live view, the result's refetch() forces one immediately, and dispatch(api.util.resetApiState()) wipes everything on logout.

Global cache and refetch policyShell
export const catalogApi = createApi({
  baseQuery: fetchBaseQuery({ baseUrl: 'https://api.example.com/' }),
  keepUnusedDataFor: 300,            // five minutes for this API
  refetchOnMountOrArgChange: 30,     // stale after 30 s
  refetchOnFocus: true,              // needs setupListeners(store.dispatch)
  endpoints: (build) => ({           // per-endpoint values override the API's
    getRates: build.query({ query: () => 'rates', keepUnusedDataFor: 5 }),
  }),
});

Tune the numbers to how fast the data actually changes: a currency rate is stale in seconds, a product catalog in hours, a country list effectively never.