sendong wu
sendong wu

Reputation: 11

Distributed Transaction in MongoDB

So Exciting that MongoDB 4.0 support Multi-Document ACID Transactions!! But I wondering will the new feature solve my problem?

Background: i have several microservice, they share the same mongdb, so we have to face the transactions problem between them.

example: A start the transaction then call B service and C service via http, and B will call D. if one of B,C,D failed, we need to rollback. it's hard to reach this goal in this distributed system, so I wondering whether mongodb 4.0 will have the easy way to solve the problem. A / \ B C \ D I've readed the specifications from here: https://github.com/mongodb/specifications/blob/master/source/transactions/transactions.rst, i found that mongo use lsid, txnNumber to identify a transaction. so, is that mean if I develop a special Mongo-Driver that accept the lsid, txnNumber as parameters to build the special command and this will reach my goal: if one step raise error, rollback everything!

Upvotes: 1

Views: 2330

Answers (1)

Wan B.
Wan B.

Reputation: 18845

if one of B,C,D failed, we need to rollback

There are alternatives way to do this without multi-document transactions. You can have a document that gets updated by the processes A, B, C, and D. For example, D marks the document to completed.

MongoDB multi-document transactions is designed for situations that require atomicity for updates to multiple documents or consistency between reads to multiple documents. So, multiple database operations and/or multiple documents as a single transaction. From your description, your use case is more about multiple stages that can progress through or not, this can be handled using appropriate data modeling.

In addition, please note that there is also a limit for the lifetime of a transaction. The default is 60 seconds. See also transactionLifetimeLimitSeconds.

Upvotes: 0

Related Questions