Batch MongoDB Atlas upsert writes
MongoDBAtlasVectorStore currently saves each document individually during upsert, following the same sequence as add.
For larger batches, bulk writes could reduce database requests and improve ingestion throughput.
Investigate using ordered bulk replacements while preserving the current behavior:
- Replace the whole document by ID, including removing omitted metadata keys.
- Validate every vector’s dimensions before writing.
- Preserve custom collection names, nested embedding paths, ID mapping, and input order when IDs repeat.
- Honor the template’s configured write concern and custom
WriteConcernResolver. - Keep the existing partial-write and retry semantics.
Use bounded batches to avoid excessive memory use.
Add tests for batch boundaries, repeated IDs, replacement, and failure handling, and compare performance with the current implementation.
Spring Data’s bulk operations do not invoke the custom WriteConcernResolver in the same way as save, so that compatibility needs particular attention.
Follow-up to #7272
See this comment for more details: https://github.com/spring-projects/spring-ai/pull/7272#discussion_r4424746687
I’m interested in contributing a focused follow-up here. I read #7272 and the linked review comment. The compatibility requirements look more important than simply replacing the save loop with bulkOps.
Before choosing an implementation, would you prefer retaining the existing per-document save path whenever a custom WriteConcernResolver is configured? That would preserve the resolver behavior while allowing a bounded ordered-bulk path where write-concern compatibility can be established, rather than silently bypassing it or grouping writes in a way that reorders repeated IDs.
I’d start with regression coverage for whole-input dimension validation before the first write (including an invalid vector beyond a batch boundary), full replacement removing omitted metadata, repeated IDs across batch boundaries, custom collection/nested vector paths, and a failure in the middle of a batch with assertions about which writes actually persisted. I’d compare the old/new paths under the same fixture before reporting throughput numbers.
I have not implemented or benchmarked this yet. Is that compatibility-first scope useful, and is anyone already preparing the batching follow-up? AI assistance: Codex helped review the issue discussion and draft this proposal.
@Ayush0612005 Thanks for looking into this! Please go ahead and contribute a fix.
Just a heads up on the WriteConcernResolver idea: MongoTemplate doesn't give us a way to read the resolver back, there's only a setter. So we can't really tell whether someone has set a custom one, short of reflection, and I don't want to go there.
What I'd do instead is add bulk as a builder option, with ordered bulk replaces in bounded batches, and keep the per-document save as the default for now. That way nothing changes for people relying on a custom resolver, and the docs can say the bulk path doesn't go through it.
Your test list looks good. Please include some before/after numbers in the PR too, on the Atlas local container is fine.
Thanks, @sobychacko — that clears up the resolver question. I'll follow your suggested approach: an opt-in builder option for bounded, ordered bulk replacements, with per-document saves staying the default. I'll document that the bulk path bypasses the custom resolver and include the regression cases and before/after measurements using the Atlas local container. I'll share the implementation in a PR once those checks are ready.
@sobychacko the PR is up: #7350. It follows your suggestion (opt-in builder option, ordered bulk replaces in bounded batches, per-document save stays the default) and includes the regression cases and before/after numbers on the Atlas local container.