Skip to the page

Reliable requests · mid

Request de-duplication and client-side caching (stale-while-revalidate)

When several components need the same data at the same time, share one in-flight promise per key instead of firing duplicate requests, and keep results in a client cache that shows cached data at once and refreshes it in the background (the pattern TanStack Query, SWR and RTK Query implement).

The notes

What you must know

  • De-duplication: a Map from key (URL + params) to the in-flight promise; later callers get the same promise; remove the entry when it settles (also on error, or one failure is cached forever).
  • Client cache states: fresh (use it), stale (show it and refetch in the background), garbage-collected after it has been unused for a while.
  • Invalidate after mutations: refetch or update the affected keys (invalidateQueries, RTK Query tags), or apply the server response directly; optimistic updates need rollback on error.
  • Normalised caches (Redux entity adapters, Apollo/Relay) store each entity once by id so one update appears on every screen.
  • On the server side, React's cache() and framework request memoisation de-duplicate the same fetch within one render.
  • Refetch triggers: window focus, reconnect, interval; turn them off for expensive or rarely changing data.

The red pen

Common traps: mistake → fix

  • Mistake: "Cache the promise forever"

    Fix: a rejected promise or stale data stays cached; clear on settle and expire entries.

  • Mistake: "Every component should fetch its own data in useEffect"

    Fix: it creates duplicate requests and waterfalls; use a shared cache or fetch higher up.

  • Mistake: "Client cache replaces HTTP caching"

    Fix: they are different layers; HTTP caching still saves network for everyone.

Easy to hard

Questions interviewers ask, easy to hard

  1. Three components on the page all call /api/me. How do you avoid three requests?
  2. What does stale-while-revalidate mean in a client data library?
  3. A user edits a product name. How do the list, the cart and the search results all update?
  4. RTK Query vs TanStack Query vs fetch in useEffect: how do you choose?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview