Skip to content

fix: share immutable MongoDB configuration across request clones - #201

Merged
codedmart merged 3 commits into
mainfrom
codedmart/share-immutable-mongo-config
Nov 3, 2026
Merged

codedmart merged 3 commits into
mainfrom
codedmart/share-immutable-mongo-config

Conversation

@codedmart

@codedmart codedmart commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Share immutable Configuration with Arc inside MongoConfiguration instead of deeply cloning the full configuration on router-state and per-query clones.

  • Add MongoConfiguration::new and migrate production/test construction sites.
  • Preserve read-only field access through Arc dereferencing.
  • Add pointer-identity/lifetime regressions for wrapper cloning and static router-config cloning plus collection resolution.
  • Leave the SDK, MongoDB query/response buffering, allocator, and Postgres per-request fetch behavior unchanged. This is not a config-store cache.

Why

With a large static schema, deriving Clone on the owned configuration causes expensive full copies at the SDK state-clone boundary and again during static configuration resolution. Sharing immutable data avoids the transient allocations and their glibc retained-RSS aftermath.

Validation

The memory experiment used an isolated VM, local MongoDB and synthetic data only—not customer configuration or traffic. Matched source builds were made from v2.1.1 (8f5d1314d282355ab91c46259428d0ffc1071a6a) using the same Cargo.lock, Rust 1.94.0 and release profile. Both ran with the original pinned release-image glibc 2.40 loader/libraries and equal container limits. The only production-source difference in that A/B was this patch. The local baseline is not bit-identical to the published release binary.

Large-schema fixture: 12,001 object types / 10,584,163 JSON bytes; tiny /query result of 4,129 bytes; 4 workers × 24 requests × 3 bursts, with fully drained 60-second idle windows. Same schema file, query, MongoDB data and runtime for both builds.

RSS measurement (MiB) Baseline Shared configuration
Before query traffic 681.32 184.90
Observed peak 1,510.65 193.80
After third 60-second idle 1,510.65 188.74
Growth from pre-query to final idle +829.33 +3.84

Final idle RSS was 87.5% lower. All 72 requests per variant succeeded and all response bytes/SHA-256 hashes matched. Pre-query RSS is also lower because configuration clones during server/router setup now share the same allocation. Observed peaks are lower bounds from 100 ms sampling.

Controls:

  • Small-schema/tiny-result final idle: 21.40 → 20.46 MiB.
  • Small-schema/39.29 MiB-result final idle: 306.54 → 250.70 MiB. The large-result allocation/retention remains; this change does not fix response buffering. Single-run control differences are not a general performance claim.
  • Large-schema allocator-counter diagnostic: baseline glibc in-use + mmap remained 512.47 → 512.88 MiB (before traffic → final idle), while free arena grew to 1,027.90 MiB. Manual trim after all idle windows dropped RSS from 1,560.84 to 559.75 MiB. With shared configuration, in-use + mmap was 52.23 → 52.70 MiB, free arena was 119.40 MiB, and pre-trim final idle RSS was 191.30 MiB. These are allocator counters, not complete object-liveness profiling; manual trim is diagnostic, not a production fix.

All eight A/B runs completed: 504/504 requests succeeded, all baseline/shared response sizes and SHA-256 hashes matched, no OOMs, and no active client requests in any idle sample.

cargo test --locked --release --package mongodb-agent-common --package mongodb-connector passed 301 tests on the patched v2.1.1 build, including both new sharing regressions. MongoDB-backed feature-gated relational integration tests were not enabled.

This PR is based on current main (041108675cc096e16d97a94e2ce6a2c80f8f45a0, v2.1.4), not the old release. All 14 touched Rust files are unchanged between the A/B source revision and that base, so the exact tested diff applies without adaptation. main has newer dependencies/toolchain. The same package-test command also passed 301/301 tests on the actual PR commit 5700496 with current-main dependencies and Rust 1.94.1, including both new sharing regressions. This is separate from the v2.1.1 memory measurements. Feature-gated MongoDB-backed integration tests were not enabled locally; the broader GitHub CI suite is still running. No current-main memory A/B is claimed.

Interactive report and raw evidence (HasuraQL access required). Evidence includes source patch, compiler record, RSS/allocator CSVs, full-response hashes and reproduction instructions. No fixture bulk or VM tooling is included in this code PR.

Scope and limitations

This establishes the configuration-copy amplification in the synthetic static-config case. It does not establish the cause of a customer's incident, exclude an independent leak, cover live reload, or recommend malloc_trim in production. No customer/production changes were performed. Human review is requested; no approval or merge has been performed.

@codedmart
codedmart merged commit 6f37ba6 into main Nov 3, 2026
13 of 14 checks passed
@codedmart
codedmart deleted the codedmart/share-immutable-mongo-config branch November 3, 2026 02:59
@codedmart codedmart mentioned this pull request Nov 3, 2026
codedmart added a commit that referenced this pull request Nov 3, 2026
Bumps the workspace to `2.1.5` and adds the changelog entry for #201:

- Share immutable MongoDB configuration across router-state and
per-query clones instead of deeply copying it, reducing memory usage for
large static schemas.

Follows #200 and #196: only `Cargo.toml`, the workspace package versions
in `Cargo.lock`, and `CHANGELOG.md` change. No dependency updates or CI
workaround are included.

After merge: tag `v2.1.5` on the merge commit, check that the draft
release's `connector-definition.tgz` has
`ghcr.io/hasura/ndc-mongodb:v2.1.5` and non-empty `sha256` values, then
publish and update ndc-hub. No tag or release is created by this PR.

Validation: `cargo check --workspace` and `cargo check --workspace
--locked` both passed with Rust 1.94.1. The lockfile diff contains only
the ten workspace-package version bumps; no external dependency or
checksum changes. Hosted checks run normally; the separate MongoDB
8.3.13 / hosted Ubuntu kernel-detection failure has not been bypassed or
changed in this version bump.
codedmart added a commit to hasura/ndc-hub that referenced this pull request Nov 3, 2026
Release [ndc-mongodb
v2.1.5](https://github.com/hasura/ndc-mongodb/releases/tag/v2.1.5):
share immutable configuration across router-state and per-query clones
instead of deeply copying it, reducing memory usage for large static
schemas
([hasura/ndc-mongodb#201](hasura/ndc-mongodb#201)).

- Set MongoDB's latest version to `v2.1.5`.
- Add release packaging with the tagged commit
`74d85cae7b869f71987b3ddfd8551ab76901ad53` and the existing MongoDB e2e
test configuration.
- Use SHA-256
`f0da724690e1a609022b6613d009c4ad14ec38ac4e77fe4d5b1733c72497e58e`,
calculated from the downloaded `connector-definition.tgz` and matched to
GitHub's release-asset digest.

The release workflow completed successfully and the release is
published. Verified that the archive references
`ghcr.io/hasura/ndc-mongodb:v2.1.5` and all five CLI-plugin downloads
match the embedded hashes, release `sha256sum`, and GitHub asset
digests. Only the two registry files change, following #758 and the
registry portion of #764. No workflow, DDN workspace, test, or unrelated
connector changes.

The repository's Go validators passed for MongoDB latest-version
metadata, the downloaded packaging checksum and existing test
configuration, and the downloaded connector metadata spec. Hosted CI/e2e
checks will run normally; those have not been claimed as passing
locally. No hub merge or publication is performed by opening this PR.
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