22title : Deduplicate Inflight Requests
33---
44
5- Most of the time, your Hive Gateway will receive multiple requests for the same data. This can
6- happen when multiple clients request the same data, or when a single client sends multiple requests
7- for the same data.
5+ Hive Gateway supports two levels of inflight request deduplication to reduce unnecessary work:
86
9- To reduce the load on your downstream services, you can deduplicate the requests. This means that if
10- multiple requests for the same data are received at the same time, only one request will be sent to
11- the downstream service, and the responses will be shared among the clients.
7+ - ** Outbound deduplication** – when the gateway sends identical requests to downstream subgraph
8+ services
9+ - ** Inbound deduplication** – when multiple identical client requests arrive at the gateway
10+ simultaneously
1211
13- This feature is enabled by default in Hive Gateway! If you would like to disable it, you can disable
14- it like this:
12+ ## Outbound Request Deduplication
13+
14+ When multiple requests for the same upstream resource are in-flight at the same time, the gateway
15+ can deduplicate them so only one request is forwarded to the downstream service. The single response
16+ is then shared among all waiting requests.
17+
18+ This feature is ** enabled by default** . To disable it:
1519
1620``` ts title="gateway.config.ts"
1721import { defineConfig } from " @graphql-hive/gateway" ;
@@ -27,3 +31,90 @@ export const gatewayConfig = defineConfig({
2731 },
2832});
2933```
34+
35+ ## Inbound Request Deduplication
36+
37+ Gateway-level deduplication handles identical requests arriving from clients. When multiple
38+ identical GraphQL requests are received by the gateway while the first one is still being processed,
39+ only the first request is executed and the rest wait for its result, which is then shared among all
40+ identical requests.
41+
42+ This is useful for reducing the load on the gateway and subgraphs under high traffic when many
43+ clients send the same query simultaneously (e.g. popular dashboard queries or trending content).
44+
45+ This feature is ** disabled by default** . To enable it:
46+
47+ ``` ts title="gateway.config.ts"
48+ import { defineConfig } from " @graphql-hive/gateway" ;
49+
50+ export const gatewayConfig = defineConfig ({
51+ inboundInflightRequestDeduplication: true ,
52+ });
53+ ```
54+
55+ ### Deduplication Key
56+
57+ By default, the deduplication key is composed of:
58+
59+ - HTTP Request Method (e.g. ` GET ` , ` POST ` )
60+ - Request URL
61+ - All request headers
62+ - GraphQL Operation document
63+ - GraphQL Operation name
64+ - GraphQL Variable values
65+
66+ ### Filtering Headers
67+
68+ By default, all request headers are included in the deduplication key. You can restrict which
69+ headers are considered by providing a ` shouldIncludeHeader ` function:
70+
71+ ``` ts title="gateway.config.ts"
72+ import { defineConfig } from " @graphql-hive/gateway" ;
73+
74+ export const gatewayConfig = defineConfig ({
75+ inboundInflightRequestDeduplication: {
76+ // Only include the "authorization" header in the deduplication key
77+ shouldIncludeHeader : (headerName ) => headerName === " authorization" ,
78+ },
79+ });
80+ ```
81+
82+ ### Custom Deduplication Keys
83+
84+ For full control over the deduplication key, use ` getDeduplicationKeys ` :
85+
86+ ``` ts title="gateway.config.ts"
87+ import { defineConfig } from " @graphql-hive/gateway" ;
88+
89+ export const gatewayConfig = defineConfig ({
90+ inboundInflightRequestDeduplication: {
91+ getDeduplicationKeys : (_args , request ) => {
92+ // Deduplicate per tenant based on a custom header
93+ const tenantId = request .headers .get (" x-tenant-id" );
94+ return tenantId ? [` tenant:${tenantId } ` ] : [];
95+ },
96+ },
97+ });
98+ ```
99+
100+ ### Conditional Enabling
101+
102+ You can enable or disable deduplication on a per-request basis using the ` enabled ` function:
103+
104+ ``` ts title="gateway.config.ts"
105+ import { defineConfig } from " @graphql-hive/gateway" ;
106+
107+ export const gatewayConfig = defineConfig ({
108+ inboundInflightRequestDeduplication: {
109+ enabled : (_args , request ) =>
110+ request .headers .get (" x-enable-dedupe" ) === " true" ,
111+ },
112+ });
113+ ```
114+
115+ ### Limitations
116+
117+ - ** Only ` query ` operations are deduplicated.** Mutations and subscriptions are never deduplicated
118+ since they may have side effects.
119+ - ** Queries using ` @defer ` or ` @stream ` are not deduplicated** , since these directives produce
120+ multiple incremental responses.
0 commit comments