Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two phase commit in MongoDB

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?

like image 757
Khanh Tran Avatar asked Nov 08 '13 04:11

Khanh Tran


People also ask

What is 2 phase commit in database?

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.

What is 2 phase commit in SQL Server?

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.

What is 2 phase commit in Oracle?

A two-phase commit is an algorithm used to ensure the integrity of a committing transaction.


1 Answers

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.

like image 127
Philipp Avatar answered Sep 28 '22 21:09

Philipp