The best practices and limitations of multi-document ACID transactions

There are currently some limitations and best practices when developing using MongoDB transactions in version 4.0.3:

  • The transaction timeout is set to 60 seconds.
  • As a best practice, any transaction should not try to modify more than 1,000 documents. There is no limitation in reading documents during a transaction.
  • The oplog will record a single entry for a transaction, meaning that this is subject to the 16 MB document size limit. This is not such a big problem with transactions that update documents, as only the delta will be recorded in the oplog. It can, however, be an issue when transactions insert new documents, in which case the oplog will record the full contents of the new documents.
  • We should add application logic to cater for failing transactions. These could include using retryable writes, or executing some business logic-driven action when the error cannot be retried or we have exhausted our retries (usually, this means a custom 500 error).
  • DDL operations such as modifying indexes, collections, or databases will get queued up behind active transactions. Transactions trying to access the namespace while a DDL operation is still pending will immediately abort.
  • Transactions only work in replica sets. Starting from MongoDB 4.2, transactions will also be available for sharded clusters.
  • Use sparingly; maybe the most important point to consider when developing using MongoDB transactions is that they are not meant as a replacement for good schema design. They should only be used when there is no other way to model our data without them.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset