Having carefully reading the online documentation, I still have a lot of questions about two phase commit in MongoDB.
In the section Recovering from Failure Scenarios, why are there only two classes of failures? In my thinking failure can happen in any of these step so there should be a lot more than two classes here. For example what if, (in Apply Transaction to Both Accounts section), after updating account A the database server failed. That means account A lost some money without anything happen on account B. And we would have inconsistent transaction?
A two-phase commit is a standardized protocol that ensures that a database commit is implementing in the situation where a commit operation must be broken into two separate parts. In database management, saving data changes is known as a commit and undoing changes is known as a rollback.
Two-phase commit (2PC) is a host server-installed protocol that ensures that updates to multiple instances of a database on a network either succeed or fail in their entirety. Host Integration Server supports 2PC over TCP/IP, enabling you to gain the security of a 2PC connection over the Internet.
A two-phase commit is an algorithm used to ensure the integrity of a committing transaction.
When the application or database suddenly crashes between applying the transaction to A and applying the transaction to B, there will still be a transaction with state:"pending"
in the global transaction collection. Your recovery script which you run after a crash should notice this, check the two accounts, and see that there is a pending transaction in one, but not the other account. It now knows everything it needs to know to either rollback the transaction or try to complete it.
Yes, writing a recovery script which is that smart isn't easy. But transactions in a database system not designed for them is always hard. Sometimes you can work around requiring transactions in MongoDB by designing your documents in a way that fields which need to be updated together are always in the same document, but there isn't always a sane way to do this. When your use-case absolutely needs transactions, protect your sanity and use a relational database.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With