Skip to content

engine: less work per GraphQL request before it reaches dagql - #15000

Closed
sipsma wants to merge 6 commits into
mainfrom
sipsma/graphql-request-fixed-cost
Closed

sipsma wants to merge 6 commits into
mainfrom
sipsma/graphql-request-fixed-cost

Conversation

@sipsma

@sipsma sipsma commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Every Dagger client talks to the engine through GraphQL over HTTP, one request per query. Before a request reaches dagql, the engine routes it, admits its client, sets up its loggers and builds a GraphQL handler. Several of those steps redo, for every request, work whose result never changes. That is cheap once, but a build that makes thousands of cached calls pays it thousands of times. Building dagger's own CLI with dagger/go is an example: an edit sends ~3,000 requests.

This PR removes that repeated work. It cuts the engine's CPU by ~0.2–0.3 s per edit build of dagger's CLI with dagger/go, about 4–5% of the engine's CPU for that build. On its own, no build's wall time changes, because on an edit another step ties with the request walk on the critical path. With in-flight changes that shorten that other step, edits are ~0.13–0.20 s faster.

What changes

Each commit stands on its own:

  1. Serve the session's endpoints without building a mux per request. serveHTTPToClient built an http.ServeMux for every request and registered the session's four endpoints on it. Those are exact paths, so they're now served directly. The mux is still built for telemetry subscriptions, and for every request once the session has an endpoint mixed in through MuxEndpoint, so a mixed-in pattern keeps its precedence.
  2. Build a request's log fields in one step. The request logger got its five fields from chained WithField calls, each copying logrus's field map; it now gets them in one WithFields call.
  3. Merge a known client's metadata without cloning it. mergeClientMetadataLocked made a JSON round-trip clone of the incoming metadata on every request, under the session's scope lock, only to find nothing to store. It now compares first and clones only a field it stores. getOrInitClient's own clone stays: it also normalizes the metadata (an empty map becomes nil), and the comparison relies on that.
  4. Look up an existing session under the read lock. Every request took the engine-wide daggerSessionsMu exclusively to find its session. Now only creating a session takes the write lock, and the shutdown check stays under it. Execution traces of an edit build showed requests waiting up to 0.2 s in all on that lock.
  5. Build a schema's GraphQL handler once. serveQuery built a new gqlgen server per request: five transports, two extensions and two LRU caches, all thrown away afterwards. A dagql.Server now builds its handler on first use and every request to that schema shares it.
    • The handler is gqlgen's default server written out, because handler.NewDefaultServer is deprecated. It has no parsed-query cache: that cache never hit when it lived for one request, and shared, it would mostly hold queries that never come again, since the SDKs write arguments, IDs included, into the query text.
    • One visible difference: the automatic persisted queries cache is now shared by the schema's requests, so a client can send a known query by its hash alone. Before, the cache lasted one request, so that never worked.
  6. Log each HTTP request at trace level. The "handling http request" entry was added to debug 🐞 transport not supported #7592, which has since closed. The engine logs at debug level by default, so every engine wrote this line for every request: about 10,000 lines per run of the builds below. It's now logged at trace level.

Measurements

Building dagger's CLI with dagger/go on one c8gd.4xlarge (16 Graviton4 cores), comparing an engine with and without this change, both built from the same base. Every run started a fresh engine, with alternating arms. There were three series on one machine: 4 runs per arm (without the last commit), then 8, then 6 with the engine's wall-clock profiler recording in both arms. The base was a branch of main with other performance changes on it; this series applies to main with an identical diff.

Engine CPU, s per build, change minus base, with 95% intervals:

Build 4 runs 8 runs 6 runs, recording
Edit of a package inside dagger −0.23 [−0.42, −0.02] −0.17 [−0.31, −0.03] −0.34 [−0.47, −0.19]
Edit of the CLI's own package −0.19 [−0.41, +0.06] −0.29 [−0.42, −0.16] −0.18 [−0.35, −0.02]
Cold build +0.10 [−0.71, +0.98] −0.15 [−0.33, +0.03] −0.21 [−0.52, +0.11]

Wall time, all 18 runs per arm pooled, change minus base:

Build Base Change minus base
Cold build 22.60 s +0.08 [−0.12, +0.29]
Edit inside dagger 6.89 s 0.00 [−0.06, +0.08]
Edit of the CLI's package 5.43 s −0.02 [−0.08, +0.04]
Unchanged rebuild 0.86 s 0.00 [−0.02, +0.02]

A fourth series put both arms on top of in-flight changes that take the source's content hash off the edit's critical path, 8 runs per arm. On that line, edits were 0.13–0.20 s faster (medians), with intervals of −0.30 to −0.05 s and −0.25 to −0.06 s.

CPU profiles of one run per arm show the targeted steps shrink. Building a mux per request went from 0.08–0.10 CPU-s per build to nothing, building gqlgen's handler from 0.03–0.04 to nothing, and client admission from 0.07 to 0.03 per edit.

Validation

  • dagger api call engine-dev test --pkg ./engine/server --race: 497 passed.
  • dagger api call engine-dev test --pkg ./dagql --race: 1,248 passed.
  • No new tests. The change keeps behavior the same, apart from persisted queries now working across requests. Both packages run in CI's test-split/test-packages check.

Every request built an http.ServeMux, registered the session's four
endpoints on it and dispatched through it, and built the fields of a Debug
log entry whether or not debug logging was on. The session's endpoints are
exact paths, so while the session has no endpoint mixed in through
MuxEndpoint, a request for one of them is now served directly. Any other
request goes through the mux with every endpoint registered, as before, so a
mixed-in pattern that matches a built-in path, such as "GET /query", keeps
its precedence. The debug entry is built only when debug logging is enabled.

Signed-off-by: Erik Sipsma <[email protected]>
Every request chained five logrus WithField calls, each copying the entry's
growing field map, and formatted its trace and span IDs four times over. The
fields are now set in one WithFields call, from IDs formatted once.

Signed-off-by: Erik Sipsma <[email protected]>
Every request of a client runs mergeClientMetadataLocked, which cloned the
incoming metadata through a JSON round trip before comparing it with the
stored one, field by field. For a known client the two almost always match
and nothing is stored, so the clone is now made only once a field must be
completed from the incoming metadata: the record still never shares a value
with the caller. getOrInitClient, the only caller outside tests, passes
metadata it has already cloned the same way, so the comparisons are those
of a cloned value as before.

Signed-off-by: Erik Sipsma <[email protected]>
Every request of a session took the engine's sessions lock exclusively, only
to find its session in the map. With a dozen requests of a build in flight,
they queued on it: in execution traces of two edit builds of dagger's CLI,
requests waited on it 15 ms and 0.2 s in all. An existing session is now
found under the read lock; only a request that creates a session takes the
write lock, and checks for shutdown under it as before.

Signed-off-by: Erik Sipsma <[email protected]>
…request

Every query built a new gqlgen server for its schema: five transports, the
introspection and persisted-query extensions, a cache of parsed queries and a
cache of persisted queries, all dropped once the request was served. A
dagql.Server now builds its handler on first use and serves every request to
its schema with it.

The handler is gqlgen's default server, written out instead of taken from
the deprecated handler.NewDefaultServer, without its cache of parsed
queries. That cache never hit before, since each lived for one request, and
shared it would mostly hold queries that never come again: clients write
their arguments, IDs included, into the query text. The persisted-query
cache is now shared by the schema's requests, so a client using automatic
persisted queries can send a known query by its hash alone, as that
extension intends.

Signed-off-by: Erik Sipsma <[email protected]>
serveHTTPToClient logged a debug entry for every request, added to help
debug a transport error (#7592, closed since). The engine logs at debug
level by default, so every engine formatted and wrote that line for every
GraphQL request: in an edit build of dagger's CLI, a few thousand lines and
0.03 to 0.06 CPU-s. The entry is now logged at trace level, where it's still
there to debug with.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma

sipsma commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Folded into #15007, which carries these six commits unchanged.

@sipsma sipsma closed this Oct 30, 2026
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