Skip to content
fa
Search Frontend Atlas
Search questions and concepts across the handbook.
Frontend System Designadvanced13 min read

How would you structure data fetching across a large frontend?

Define ownership, cache keys, invalidation, deduplication, mutation semantics, and server-state boundaries.

A component can call fetch directly and display the result. That is fine until several screens need the same data, requests race, caching matters or errors need consistent handling. Data-fetching architecture is about deciding where requests live and how loading, freshness, cancellation and failure move through the application.

A component can call fetch directly and display the result. That is fine until several screens need the same data, requests race, caching matters or errors need consistent handling. Data-fetching architecture is about deciding where requests live and how loading, freshness, cancellation and failure move through the application.

The quick version

Treat server state as a cache of remote authority, not as ordinary local UI state. Centralize request identity, caching, deduplication, invalidation, cancellation, and mutation reconciliation in a mature data-fetching library.

Build the mental model

A cache key must include every input that changes the response, including tenant/user scope where relevant. Invalidation should be precise enough to avoid stale reads but broad enough to preserve correctness. Avoid writing a custom query cache unless the product has a compelling reason.

See it in code

const queryKey = ['projects', { organizationId, status, page }];

Where it gets tricky

Cache keys must include all variables that affect the server response and must be scoped to the active identity or tenant. On logout or account switch, remove or isolate private cached data. Decide whether mutations update a cache entry, invalidate related queries, or require a full refetch; these are consistency decisions, not merely library configuration.

A common misunderstanding

A successful HTTP response does not mean every component should independently own a copy forever.

What this changes in an application

Define stale-time and retry policy, auth transitions, optimistic update behavior, and cache isolation between identities.

Questions worth exploring next

  • How do you prevent cross-user cache leakage?
  • When should a mutation invalidate versus update a query?

References and next steps

  • TanStack Query overview — primary or authoritative documentation for the underlying behavior.
  • MDN Web Docs — use the relevant API reference for adjacent browser details.