tags: mysql
2PC
For performance reasons, each time you commit a transaction, you only need to place redo and undo to indicate that the transaction has been persisted, and you do not need to wait for the data to be placed. This can already guarantee the roll forward or roll back of the transaction crash. Since the undo information is also written to redo, in fact, we only need to decide whether to redo or rollback the crash recovrey according to whether the redo is placed. As mentioned above, after starting binlog, you also need to consider whether binlog is placed (binlog involves the master-slave data consistency and the site of full recovery). According to whether the transaction is successfully written to binlog to decide whether to redo or rollback the transaction.
2PC is innodb's two-phase commit mechanism for transactions. When mysql starts binlog, there will be an internal XA problem: the order of transactions in the storage engine layer (redo) commit and the order of submission in binlog are inconsistent.
2PC principle
The transaction commit is divided into two stages: prepare and commit:
1. prepare phase: redo persists to disk (redo group commit), and puts the rollback segment into the prepared state, at this time binlog does not operate.

2. Commit phase: innodb releases the lock, releases the rollback segment, sets the commit status, persists the binlog to disk, and then commits to the storage engine layer

After having an understanding of 2PC, we can find that redo and binlog can be group commit in the above picture, then let ’s understand what is group commit
Group Commit
background
As we know, log writing is basically sequential IO. WAL (Write-Ahead-Logging) uses sequential log writing instead of random IO for data to achieve transaction persistence. However, despite this, every transaction commit requires log flushing, which is still limited to disk IO. The emergence of group commit is to merge the action of redo / binlog brushing, thereby improving IO performance
The 2PC mechanism can only solve the problem that the redo / binlog order of a single transaction is consistent. What if it is concurrent? (There may be a case: binlog submission order (T1, T2, T3), innodb commit order (T2, T3, T1)).

As shown above, the transaction starts in T1, T2, and T3 order, writes the binary log (in T1, T2, and T3 order) to the log file system buffer, and calls fsync () to perform a group Commit writes log files to disk permanently, but the order of storage engine submission is T2, T3, and T1. After T2 and T3 commit the transaction, if the online physical backup is used for database recovery to establish replication, because the InnoDB storage engine layer will detect the transaction T3 has completed the transaction submission at both the upper and lower layers, there is no need to restore it at this time The primary and secondary data are inconsistent
Before MySQL version 5.6, the prepare_commit_mutex lock was used to ensure the serial commit order of the upper binary log of the MySQL database and the Innodb storage engine layer in a serial manner, and then the group commit would not take effect

As shown in the figure above, MySQL uses prepare_commit_mutex and sync_log when binary log is turned on to ensure that the order of binary logs and storage engines is consistent. The lock mechanism of prepare_commit_mutex results in high concurrent commit transactions. Nor can group commit.
Redo Group Commit
In the prepare phase of 2PC, a redo operation will be performed on redo (innodb_flush_log_at_trx_commit = 1). At this time, the redo group commit process is as follows:
Not long ago, a newcomer on the team asked me a very important web service interface how to ensure transactions. Because it involves cross-database transactions, I just answered that currently our SOA...
If the operation involves a plurality of distributed nodes, in order to guarantee transaction ACID properties, it is necessary to introduce a "coordinator" unified assembly to perform all th...
The following will end the two-phase submission agreement (2PC) and the three-phase submission agreement (3PC) respectively: As shown in the figure above, TM implements the management of multiple RM t...
table of Contents Two-phase commit (2PC) Preparation Phase: Submission stage: 2PC problems: Three-phase commit (3PC) CanCommit: PreCommit (if all participants in the CanCommit stage return "Yes&q...
transaction Transactions are the foundation to ensure that the database is permanently changed from one consistent state to another consistent state ACID ACID is a basic feature of transactions: A is ...
First, the concept Two-phase commit protocol 2PC (Two phase Commit) refers, in a distributed system, in order to ensure consistency of an algorithm when all nodes during the transaction commits.  ...
concept: 2PC is the two-phase commit protocol. It divides the entire transaction process into two phases, Prepare phase and commit phase. 2 refers to two phases, P refers to the preparation phase, and...
1. 2PC two-phase commit protocol, introduction The following order: involves order service, inventory service, and inventory service execution fails, the coordinator will tell the order service to rol...
2019 Unicorn Enterprise Heavy Gold Recruitment Python Engineer Standard >>> Database connection settings Reprinted at: https://my.oschina.net/u/930279/blog/785945...
When the binlog option is turned on, when the transaction commit command is executed, the two-phase commit mode is entered. The two-phase commit is divided into two phases, the prepare phase and the c...