Quick revision
The short answer
MongoDB multi-document transactions need a replica set or a sharded cluster, never a standalone server, and in Spring we must register a MongoTransactionManager bean before @Transactional does anything for MongoDB. They cost extra latency, hold resources until commit and are cut off after a time limit of 60 seconds by default, so they should be short. Often the better answer is a document model that keeps the invariant inside one document, where every single-document write is already atomic, and when we do use transactions we choose read and write concerns that match the guarantee we need.
What to remember
In this answer, we saw that single-document writes in MongoDB are already atomic, while multi-document transactions need a replica set or sharded cluster and an explicitly configured MongoTransactionManager in Spring. Transactions add commit latency, risk write conflicts and are aborted after 60 seconds by default, so they must be short and retried on transient errors. Frequently, a document model that keeps the invariant in one document with a conditional update avoids the need, and when we do use transactions, majority write concern with snapshot read concern gives the guarantee we expect.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
MongoDB multi-document transactions need a replica set or a sharded cluster, never a standalone server, and in Spring we must register a MongoTransactionManager bean before @Transactional does anything for MongoDB. They cost extra latency, hold resources until commit and are cut off after a time limit of 60 seconds by default, so they should be short. Often the better answer is a document model that keeps the invariant inside one document, where every single-document write is already atomic, and when we do use transactions we choose read and write concerns that match the guarantee we need.
Why this question is asked
Teams coming from relational databases often assume transactions in MongoDB work exactly like in PostgreSQL. Then the first integration test against a local single-node MongoDB fails, or @Transactional silently does nothing. Interviewers ask this scenario to see whether we know the infrastructure requirement, the Spring wiring, the real cost, and above all whether we know when we do not need a transaction at all. In this answer, we will recap atomicity in MongoDB, look at the cluster requirement, wire the transaction manager, discuss costs and limits, see how a better document design avoids the need, and choose read and write concerns.
1A Quick Recap: What Is Already Atomic in MongoDB
Atomic means an operation either happens completely or not at all; nobody sees half of it. In MongoDB, every write to a single document is atomic, even when it changes many fields and nested arrays at once. So if an order and its lines live in one document, saving the order is atomic without any transaction.
A multi-document transaction is needed only when one business operation must change several documents, possibly in several collections, and they must all change together. Moving money between two account documents, or creating an order while decrementing stock held in separate product documents, are the usual examples. MongoDB has supported such transactions on replica sets since version 4.0 and on sharded clusters since 4.2.
2Requirement 1: A Replica Set or Sharded Cluster
A replica set is a group of MongoDB servers holding the same data, with one primary that accepts writes and secondaries that copy from it. Transactions depend on machinery that only exists in replica sets, such as the operation log (the oplog) and logical sessions. A standalone server does not have it, so starting a transaction there fails with an error saying transaction numbers are only allowed on a replica set member or mongos.
This matters most for local development and tests. The fix is cheap: run a single-node replica set. Testcontainers' MongoDB module starts one by default, and with Docker we can start mongod with --replSet rs0 and run rs.initiate() once. Managed offerings such as MongoDB Atlas always run replica sets.
3Requirement 2: Wiring MongoTransactionManager
Spring's @Transactional works through a transaction manager bean. For JDBC and JPA, Spring Boot creates one for us. For MongoDB, Spring Boot 3.x does not auto-configure a MongoTransactionManager, so without one @Transactional on a method that only touches MongoDB does nothing at all, and each save commits on its own. Check your Spring Boot version, but declaring it explicitly is always safe.
8 messages in order. Play them, or step through them one at a time.
If the application also uses JPA, there are now two transaction managers, and we must name the one each @Transactional uses, for example @Transactional("mongoTransactionManager") after giving the bean that name. Otherwise Spring fails with an error about finding more than one candidate, or quietly uses the wrong one if one is marked primary.
4What Transactions Cost
Transactions are not free in MongoDB. Each one holds a session and a snapshot open on the server until it commits, and writes inside it take locks on the documents they touch. Another transaction writing the same document gets a write conflict and must retry. Commit with write concern majority waits for most replica set members to confirm, which adds a network round trip, typically a few milliseconds inside one region and much more across regions.
There is also a hard time limit. By default the server aborts any transaction that runs longer than 60 seconds (the transactionLifetimeLimitSeconds parameter), and MongoDB's guidance is to change well under 1,000 documents per transaction. So we keep transactions short, never call remote services inside them, and write retry logic for transient errors. The MongoDB driver's callback API retries on errors labelled TransientTransactionError; with Spring we usually add a retry around the service method.
5Often We Do Not Need One
Before adding a transaction, we ask whether the invariant can live inside one document. Suppose we must never sell more tickets than seats. If each sale updates a seat counter and inserts a ticket in a different collection, we need a transaction. If instead the event document holds the counter and we update it with a condition, one atomic single-document write enforces the rule.
This is faster, cannot conflict across documents, and works on any deployment. The lesson is that in a document database, the document is the unit of consistency, so we design documents around the invariants we must protect.
6Choosing Read and Write Concerns
A write concern says how many servers must confirm a write before it counts as done. w: 1 means only the primary; w: majority means most members, so the write survives the primary failing. A read concern says what data a read may see. snapshot, used for transactions, gives a consistent view as of one point in time; majority returns only data confirmed by a majority, so it cannot be rolled back.
For a transfer of money, we want writeConcern majority and readConcern snapshot, which is what we configured above. Using w: 1 inside a transaction would mean a commit acknowledged by a primary that fails a moment later could be rolled back, which defeats the point of the transaction.
7Follow-up questions
Can a transaction span MongoDB and PostgreSQL?
No. Each transaction manager handles its own store, and there is no practical two-phase commit between them in Spring today. For changes that must reach both, use the outbox pattern: write the business change and an event in one store's transaction, and let a relay deliver the event to the other store, with idempotent handling.
Why did my test pass on an embedded MongoDB but fail in staging?
Usually because the test ran without a transaction manager, so every write committed alone and nothing was rolled back, or it ran on a standalone server where transactions were never really exercised. Testing against a real single-node replica set in Testcontainers with the transaction manager configured shows the real behaviour.
Do transactions slow down reads that are not in a transaction?
Not directly, but long transactions keep old snapshots in memory on the server and increase cache pressure, and conflicting writes must retry. Short transactions keep that cost small.
8Conclusion
In this answer, we saw that single-document writes in MongoDB are already atomic, while multi-document transactions need a replica set or sharded cluster and an explicitly configured MongoTransactionManager in Spring. Transactions add commit latency, risk write conflicts and are aborted after 60 seconds by default, so they must be short and retried on transient errors. Frequently, a document model that keeps the invariant in one document with a conditional update avoids the need, and when we do use transactions, majority write concern with snapshot read concern gives the guarantee we expect.
Open with
First, the infrastructure: multi-document transactions only work on a replica set or a sharded cluster. A standalone mongod rejects them, so for local work and tests I run a single-node replica set, which Testcontainers does by default.
Then, if they want more
Second, the Spring wiring: @Transactional needs a MongoTransactionManager bean, and Spring Boot 3 does not auto-configure one, so without it every save commits on its own and nothing rolls back. If the app also uses JPA, I name the transaction managers and pick the right one on each annotation.
Third, the cost: a transaction holds a session and snapshot open, writes to the same documents conflict and must retry, a majority commit adds a round trip, and the server aborts anything over 60 seconds by default, so I keep them short, never call other services inside, and retry on TransientTransactionError.
But my first question is whether I need one at all. In MongoDB every single-document write is atomic, so if I can keep the invariant in one document, like a seat counter with a conditional decrement and a push of the booking, I get correctness without a transaction.
When I do need one, for example moving money between two account documents, I configure write concern majority and read concern snapshot, so a commit cannot be lost if the primary fails right after acknowledging it.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A Spring Boot service needs multi-document transactions in MongoDB. What are the...": https://scrivio-interview-library.netlify.app/databases/spring-data-nosql/e334dbed59/