NoSQL · Data models beyond tables
How do transactions work in NoSQL databases?
Short answer
Many NoSQL stores guarantee atomicity per document or item and add multi-document transactions with limits. The trade-off is coordination cost, contention and often a maximum scope or duration.
A single-document update is usually atomic because the document is one storage unit. Multi-document transactions require coordination across partitions and are therefore more expensive. Some systems support them fully, some only within a partition, and some offer compare-and-set instead.
Mechanisms you will meet
- MongoDB multi-document transactions with sessions and snapshot isolation
- DynamoDB TransactWriteItems and TransactGetItems for all-or-nothing operations across items
- Cassandra lightweight transactions using Paxos for compare-and-set on a partition
- Conditional writes, which are often enough to prevent lost updates without a full transaction
const session = client.startSession();
try {
await session.withTransaction(async () => {
await accounts.updateOne(
{ _id: "acct_a", balance: { $gte: 100 } },
{ $inc: { balance: -100 } },
{ session }
);
await accounts.updateOne(
{ _id: "acct_b" },
{ $inc: { balance: 100 } },
{ session }
);
});
} finally {
await session.endSession();
}Transactions are not free. They hold locks or reservations, increase latency and can fail under contention, so retries must be idempotent. Keep them short and touch as few items as possible. If a workflow spans minutes or external systems, a saga or outbox is usually a better fit than a long transaction.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1When would you use a saga instead of a transaction?
- 2What isolation level do NoSQL transactions usually provide?
- 3How do conditional writes prevent lost updates?
- 4Why are cross-partition transactions expensive?
Say it in the interview
Most NoSQL stores give atomicity within one document or item, and some add multi-document transactions. Those transactions coordinate across partitions, so they cost more, hold resources and can fail under contention. I keep them short, make retries idempotent and check the engine real limits on item count and duration. For workflows that span services or minutes, I prefer a saga or an outbox pattern, because a long distributed transaction is usually the wrong tool.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live NoSQL mock interview and get scored feedback on depth, clarity and confidence.