Understanding locking within transactions (and how it deals with high concurrency)

Hi @Tom_S and welcome to the community!!

Firstly, I would really appreciate you spending time on posts with so much details.

However, to answer your question:

The locks in MongoDB are acquired at the transaction level. However only write operations will lock the associated document. Read operations will not lock them. Hence the locks will be released only when the transaction is completed.
For more reference on how transactions and locks work functions in MongoDB, would recommend following documentations for:

  1. Transactions
  2. FAQ: Concurrency
  3. How to Use MongoDB Transactions in Node.js

In terms of your concern about latency, I think it depends on the size of the workload you’re expecting, and the hardware that executes this workload. I agree though that using transactions will definitely have a performance impact, but depending on your workflow, it might be unavoidable (especially if you’re depending on mutating the state of more than one documents where the all mutations must succeed to be considered successful). In general, though, MongoDB works fastest with operations involving single documents.

Best Regards
Aasawari

1 Like