Question 41 / 50
Does MongoDB support transactions, and when do you actually need them?
Short answer
Yes — multi-document ACID transactions on replica sets and sharded clusters. You need them far less often than in a relational database, because a single-document update is already atomic and related data is usually embedded.
Why?
Transactions hold locks and keep a snapshot, so they cost throughput and have a time limit. The typical legitimate use is a write that spans documents and must not be seen half-applied, such as moving funds. If most writes sit in a transaction, the schema is probably too normalised.
Interview tip
Saying 'I model so one document write is the common case' is the answer that separates people who have used MongoDB from people who have only read about it.
Common mistake
Wrapping every repository method in a session 'for safety'.
How did you do?
Cheat Sheet