Using request ownership, single-flight synchronization, and freshness windows to reduce avoidable network work.
Many performance problems begin in the client rather than the database.
A screen mounts and requests its data. It regains focus and requests it again. A component rerenders and triggers another request. A global event fires and reloads configuration. Several components initialize together and request the same resource independently.
Each request may be fast in isolation. Together they create unnecessary server traffic, mobile data usage, battery consumption, rate-limit pressure, and visual instability.
Separate rendering from freshness
A render should never automatically mean that remote data must be fetched again. The application needs explicit answers to three questions:
- Who owns refreshing this resource?
- What event makes the existing value stale?
- What should the interface show while a refresh is in progress or fails?
For shared resources, a single-flight pattern prevents concurrent callers from duplicating the same request:
const inFlight = new Map<string, Promise<void>>();
function syncResource(key: string) {
const existing = inFlight.get(key);
if (existing) return existing;
const request = fetchResource(key).finally(() => {
inFlight.delete(key);
});
inFlight.set(key, request);
return request;
}
If five components request the same resource simultaneously, they share one network operation.
Guard against event-driven request loops
Global state systems can accidentally produce a cycle:
event → fetch → store update → event → fetch
Translation loading is a useful example. A language event starts a remote synchronization. Installing the downloaded resource updates the translation store, which emits another event and begins the cycle again.
The fix is to synchronize only during explicit lifecycle events, such as application initialization or a real user language selection. A short freshness window prevents repeated successful synchronization when nearby events request the same language.
Preserve the last valid value
A failed refresh should not erase usable cached data. Keep the last valid value, record its freshness, and expose the failure separately. This is especially valuable for reference data and configuration that changes infrequently.
Balances and transaction statuses may need focus refreshes. Static configuration, bank directories, and translations usually do not need to reload every time a user revisits a screen.
Caching is not merely storing data. It defines ownership, freshness, concurrency, failure behavior, and invalidation. The fastest request is often the one the application correctly decides not to send.
Implementation checklist
Inventory every trigger for important requests. Give each resource one refresh owner. Deduplicate concurrent calls by resource key. Define a bounded freshness period. Retain the last successful value after errors. Cancel obsolete requests where appropriate. Verify that store updates cannot retrigger their own synchronization path.
Top comments (0)