Request Deduplication
Deduplicate in-flight requests by key so concurrent callers share one fetch, cache the result, and avoid doubling load when components mount together.
On this page
Introduction
This guide covers deduplicating in-flight requests, a recurring concern in the data-fetching scenario. When several components mount at once and ask for the same data, firing one request each doubles the server load for no benefit. Deduplicating by key so concurrent callers share one in-flight promise, then caching the result, cuts the load and makes the shared fetch the default.
Key by URL and params
Build a key from the URL and the serialized params so two calls with different args are not deduped, which would be a silent bug.
The trade-off worth naming is that build a key from the url and the serialized params so two calls with different args are not deduped, which would be a silent bug adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
const inflight = new Map();
function dedupe(url, params) {
const key = url + JSON.stringify(params);
if (inflight.has(key)) return inflight.get(key);
const p = fetch(url, { method: 'POST', body: JSON.stringify(params) }).then(r => r.json());
inflight.set(key, p);
p.finally(() => inflight.delete(key));
return p;
}
Share the in-flight
If a key is already in flight, return the same promise so concurrent callers wait on one request and all receive the same result.
The trade-off worth naming is that if a key is already in flight, return the same promise so concurrent callers wait on one request and all receive the same result adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Cache the result
After the promise resolves, keep the value in a cache for a short window so a call right after also hits the cache rather than refetching.
The trade-off worth naming is that after the promise resolves, keep the value in a cache for a short window so a call right after also hits the cache rather than refetching adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
const cache = new Map();
p.then((v) => { cache.set(key, { v, t: Date.now() }); });
// serve from cache if Date.now() - cache.get(key).t < 2000
Only safe reads
Dedupe only GET or other safe reads; two identical POSTs may have different effects, and deduping would silently drop one, which is dangerous.
The trade-off worth naming is that dedupe only get or other safe reads; two identical posts may have different effects, and deduping would silently drop one, which is dangerous adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Error sharing
All callers sharing the in-flight also share its rejection, so handle the error once and let each caller render its own failure state.
The trade-off worth naming is that all callers sharing the in-flight also share its rejection, so handle the error once and let each caller render its own failure state adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Test concurrency
Fire the same request three times at once and assert one network call and three equal results, so dedupe is real.
The trade-off worth naming is that fire the same request three times at once and assert one network call and three equal results, so dedupe is real adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Sources
Sources: SWR dedup, TanStack Query.
FAQ
- Dedupe for how long?
- For the duration of the in-flight request plus a short dedupe window after, so a second call right after the first resolves still hits the cache rather than refetching.
- Should I dedupe POST too?
- No; dedupe only safe reads, since two identical POSTs may have different intended effects, and deduping them would silently drop one.
Report a problem with this articleOpens a pre-filled issue on GitHub.