Skip to content

Add configurable concurrent resolution of subscription items - #1833

Open
tpham-mysten wants to merge 1 commit into
async-graphql:masterfrom
tpham-mysten:upstream-subscription-concurrency
Open

tpham-mysten wants to merge 1 commit into
async-graphql:masterfrom
tpham-mysten:upstream-subscription-concurrency

Conversation

@tpham-mysten

Copy link
Copy Markdown

Description

Subscription payloads are resolved one at a time: the selection set for each
yielded value is fully resolved before the next value is polled from the stream.
When resolving a payload involves async work (for example, fetching nested
fields from a database or another service), that work is serialized across
payloads, so a fast-producing subscription ends up bottlenecked on resolving a
single payload at a time.

This adds a schema-level option to resolve multiple subscription payloads
concurrently while preserving output order:

let schema = Schema::build(Query, EmptyMutation, Subscription)
    .subscription_resolution_concurrency(16)
    .finish();

The default is 1, which keeps the current behavior of resolving one payload at
a time, so the change is fully backwards compatible.

Internally the subscription resolver switches from StreamExt::then (serial) to
StreamExt::map followed by StreamExt::buffered(n), so up to n payloads
resolve in flight while results are still yielded in stream order. Each in-flight
payload resolves against its own error sink, so concurrent payloads do not drain
one another's errors.

Output ordering is unchanged, and a test covers that N payloads resolve
concurrently and are still delivered in order.

This mirrors amnn/async-graphql#2,
which has already been reviewed; opening here to upstream it.

Subscription payloads are resolved strictly serially (StreamExt::then in the derive), so a
subscription resolving DB-backed fields does one backend round trip per payload with no DataLoader
batching across payloads.

Add a subscription_resolution_concurrency knob to SchemaBuilder (default 1, i.e. serial, so existing
behavior is unchanged). When set > 1, the derive resolves up to N subscription items concurrently with
buffered(N) (output order preserved via FuturesOrdered), so their DataLoader reads coalesce the same
way a list's do within a payload. A server opts in with
Schema::build(...).subscription_resolution_concurrency(n).

The one correctness fix it needs: all items shared a single QueryEnvInner.errors sink drained per-item,
which only works serially. Move errors from Arc<QueryEnvInner> onto QueryEnv and add fork_errors() so
each concurrently-resolved item gets its own sink. Field shadowing keeps every existing errors read
site unchanged, and queries/mutations never fork, so their behavior is untouched.
@tpham-mysten
tpham-mysten force-pushed the upstream-subscription-concurrency branch from baaa022 to 6530206 Compare July 31, 2026 16:26
@tpham-mysten
tpham-mysten marked this pull request as ready for review July 31, 2026 16:27

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant