API Caching
API Caching covers cache keys, stale versus missing data, ETag and Cache-Control, direct updates versus invalidation, and tenant isolation.
Course: React — State Management Lesson: 10
Learning objectives
Understand cache keys, freshness, stale data, invalidation, refetching, cache scope, and how to avoid incorrect or unsafe reuse of API responses.
Why cache API data?
Caching can reduce network requests, improve perceived speed, and keep previously fetched data available while a refresh occurs. But a cache is only correct when the application knows which response belongs to which request and when that response should be considered fresh.
Cache identity
A cache key must include every input that changes the result. For example, an orders query may depend on tenant, status, sort order, and page.
["orders", tenantId, { status, sort, page }]
If tenantId is omitted, one tenant's cached results could be incorrectly reused in another tenant context. Cache scope is therefore both a correctness and security concern.
Freshness and staleness
A cache entry can exist but be considered stale. A stale entry may be displayed while a background refetch runs. The right freshness duration depends on the business domain: a static country list and a live inventory count do not have the same freshness needs.
Invalidation
After a mutation, related queries may need invalidation. Creating an order may affect the order list and dashboard summary. Updating a product may affect product detail, catalogue, and inventory views.
Plan invalidation around domain relationships rather than invalidating the entire cache after every action.
HTTP caching and client caching
Browser and intermediary HTTP caching use HTTP headers such as Cache-Control, ETag, and conditional requests. A client query library has its own in-memory cache and freshness policy. These layers can work together, but they are not the same mechanism.
User and tenant transitions
When users sign out or switch workspace/tenant, clear or isolate data that should not cross that boundary. Include identity scope in keys when appropriate, and do not rely only on a component being unmounted to protect cached data.
Cache consistency choices
- Refetch after mutation: straightforward and often reliable.
- Update cache directly: fast, but requires accurate knowledge of the response shape and affected entries.
- Optimistic update: immediate UI response with rollback if the mutation fails.
Choose based on data complexity and user experience requirements.
Common mistakes
- Incomplete cache keys.
- No invalidation after mutations.
- Overly long freshness for volatile data.
- Clearing all cache unnecessarily after every small update.
- Reusing user-specific cache after logout or tenant switch.
- Confusing HTTP cache headers with a query library's cache configuration.
Best practices
Define cache keys centrally, document freshness by domain, invalidate related data after writes, isolate user/tenant scope, and monitor cache behavior in production.
Interview questions & answers
Interview preparation
5 questions grouped by difficulty — expand any card for a model answer and follow-up probe.
Beginner
1What makes a cache key correct?
BeginnerModel answer
It includes every factor that changes the result—resource, filters, pagination, user, and tenant—so entries cannot collide.
2When would you update a cache directly instead of invalidating?
BeginnerModel answer
When the mutation response already contains the exact next cache value and rewriting is safer/faster than a full refetch.
Intermediate
3What is the difference between stale and missing data?
IntermediateModel answer
Missing means no cache entry.
Stale means an entry exists but may be outdated and eligible for background refresh.
4Why is cache isolation important in a multi-tenant SaaS?
IntermediateModel answer
Without tenant/user scope in keys and logout cleanup, one tenant can see another's cached data.
Advanced
5How do ETag and Cache-Control relate to client caching?
AdvancedModel answer
They are HTTP freshness validators/directives that help clients and gateways reuse or revalidate responses efficiently.
Practice
Design cache keys and invalidation rules for customers, orders, inventory, and dashboard summaries in a multi-tenant application.