0Pricing
MongoDB Academy · Lesson

Transaction Performance Considerations

Learners will measure the overhead of multi-document transactions and design schemas that minimise the need for them in hot paths.

Transactions Have Real Overhead

Multi-document transactions in MongoDB provide powerful ACID guarantees but come with measurable performance overhead. They involve additional network round-trips for session management, hold locks that block concurrent writers, consume oplog space, and require coordination across replica set members for the majority write concern. Understanding this overhead helps you design systems that use transactions only where necessary.

Lock Contention and Write Conflicts

MongoDB transactions use document-level locking with optimistic concurrency control. When a transaction reads a document, it takes a snapshot but does not lock it. At commit time, MongoDB checks if any other writer modified those documents—if so, the transaction aborts with a write conflict. Frequent conflicts indicate that multiple transactions are competing for the same hot documents, causing retries and degraded throughput.

// A 'hot document' that many transactions modify simultaneously
// causes frequent WriteConflict errors and retry storms:
db.counters.updateOne({ _id: 'globalOrderCount' }, { $inc: { value: 1 } }, { session });
// Better: use $inc on individual order documents (sharded by orderId),
// or use a dedicated sequence generator outside the transaction.

All lessons in this course

  1. ACID Guarantees in a Distributed Document Store
  2. Starting a Session and Multi-Document Transaction
  3. Error Handling and Retry Logic
  4. Transaction Performance Considerations
← Back to MongoDB Academy