22title : Deduplicate Inflight Requests
33---
44
5+ import { Callout } from " @hive/design-system/hive-components/callout" ;
6+
57Most of the time, your Hive Gateway will receive multiple requests for the same data. This can
68happen when multiple clients request the same data, or when a single client sends multiple requests
79for the same data.
@@ -10,8 +12,20 @@ To reduce the load on your downstream services, you can deduplicate the requests
1012multiple requests for the same data are received at the same time, only one request will be sent to
1113the downstream service, and the responses will be shared among the clients.
1214
13- This feature is enabled by default in Hive Gateway! If you would like to disable it, you can disable
14- it like this:
15+ Hive Gateway supports two levels of inflight request deduplication to reduce unnecessary work:
16+
17+ - ** Outbound deduplication** – when the gateway sends identical requests to downstream subgraph
18+ services
19+ - ** Inbound deduplication** – when multiple identical client requests arrive at the gateway
20+ simultaneously
21+
22+ ## Outbound Request Deduplication
23+
24+ When multiple identical requests to the same downstream subgraph service are inflight at the same
25+ time, the gateway can deduplicate them so only one request is forwarded to the downstream service.
26+ The single response is then shared among all waiting requests.
27+
28+ This feature is ** enabled by default** . To disable it:
1529
1630``` ts title="gateway.config.ts"
1731import { defineConfig } from " @graphql-hive/gateway" ;
@@ -27,3 +41,105 @@ export const gatewayConfig = defineConfig({
2741 },
2842});
2943```
44+
45+ ## Inbound Request Deduplication
46+
47+ Gateway-level deduplication handles identical requests arriving from clients. When multiple
48+ identical GraphQL requests are received by the gateway while the first one is still being processed,
49+ only the first request is executed and the rest wait for its result, which is then shared among all
50+ identical requests.
51+
52+ This is useful for reducing the load on the gateway and subgraphs under high traffic when many
53+ clients send the same query simultaneously (e.g. popular dashboard queries or trending content).
54+
55+ This feature is ** disabled by default** . To enable it:
56+
57+ ``` ts title="gateway.config.ts"
58+ import { defineConfig } from " @graphql-hive/gateway" ;
59+
60+ export const gatewayConfig = defineConfig ({
61+ inboundInflightRequestDeduplication: true ,
62+ });
63+ ```
64+
65+ ### Deduplication Key
66+
67+ By default, the deduplication key is composed of:
68+
69+ - HTTP Request Method (e.g. ` GET ` , ` POST ` )
70+ - Request URL
71+ - All request headers
72+ - GraphQL Operation document
73+ - GraphQL Operation name
74+ - GraphQL Variable values
75+
76+ ### Filtering Headers
77+
78+ By default, all request headers are included in the deduplication key. You can restrict which
79+ headers are considered by providing a ` shouldIncludeHeader ` function:
80+
81+ <Callout type = " warning" >
82+ Narrowing the deduplication key means requests that differ only by the
83+ excluded headers will be treated as identical. If your responses vary per
84+ user, session, or tenant (e.g. based on ` Authorization ` , cookies, locale, or
85+ feature-flag headers), make sure to keep those headers in the key to avoid
86+ sharing responses across different users and leaking data.
87+ </Callout >
88+
89+ ``` ts title="gateway.config.ts"
90+ import { defineConfig } from " @graphql-hive/gateway" ;
91+
92+ export const gatewayConfig = defineConfig ({
93+ inboundInflightRequestDeduplication: {
94+ // Only include the "authorization" header in the deduplication key
95+ shouldIncludeHeader : (headerName ) => headerName === " authorization" ,
96+ },
97+ });
98+ ```
99+
100+ ### Custom Deduplication Keys
101+
102+ For full control over the deduplication key, use ` getDeduplicationKeys ` :
103+
104+ <Callout type = " warning" >
105+ When building a custom key, ensure it includes all factors that distinguish
106+ user- or session-specific responses (e.g. tenant ID, authorization context).
107+ An overly broad key can cause the gateway to share responses across different
108+ users, leading to data leakage.
109+ </Callout >
110+
111+ ``` ts title="gateway.config.ts"
112+ import { defineConfig } from " @graphql-hive/gateway" ;
113+
114+ export const gatewayConfig = defineConfig ({
115+ inboundInflightRequestDeduplication: {
116+ getDeduplicationKeys : (_args , request ) => {
117+ // Deduplicate per tenant based on a custom header
118+ const tenantId = request .headers .get (" x-tenant-id" );
119+ return tenantId ? [` tenant:${tenantId } ` ] : [];
120+ },
121+ },
122+ });
123+ ```
124+
125+ ### Conditional Enabling
126+
127+ You can enable or disable deduplication on a per-request basis using the ` enabled ` function:
128+
129+ ``` ts title="gateway.config.ts"
130+ import { defineConfig } from " @graphql-hive/gateway" ;
131+
132+ export const gatewayConfig = defineConfig ({
133+ inboundInflightRequestDeduplication: {
134+ enabled : (_args , request ) =>
135+ request .headers .get (" x-enable-dedupe" ) === " true" ,
136+ },
137+ });
138+ ```
139+
140+ ### Limitations
141+
142+ - ** Only ` query ` operations are deduplicated.** Mutations and subscriptions are never deduplicated
143+ since they may have side effects.
144+ - ** Queries using ` @defer ` or ` @stream ` are not deduplicated** , since these directives produce
145+ multiple incremental responses.
0 commit comments