Distributed transactions in several ways

Business needs to be split according to business needs, such as a large application into the user module, order module, product modules, each module has its own database, when the user purchases the required deductions merchandise inventory module, in orders module to add order data, this time the need to ensure that the two databases is done in the same transaction, so there have been distributed transaction

1. LCN transaction mode
First, the principle introduced:
the LCN mode is achieved by operating the local affairs of the proxy Connection of the way, then by TxManager harmonization of control transactions. When the local transaction commit or rollback operation closes the connection will perform false, the proxy connection will LCN connection pool management.
Second, the characteristic mode:
This mode is a low embedded code.
This mode is limited and there is a connection object in the local transaction module can be controlled by the connection object.
Transaction commit and rollback in this mode is controlled by the local party affairs, the data consistency for higher protection.
The flaw lies in the proxy mode connection with the transaction requires the initiator to release the connection altogether, increasing the time associated with that connection.

2. TCC transaction mode
First, the principle introduced:
the TCC transaction mechanism with respect to conventional transaction mechanism (X / Open XA Two-Phase -Commit), characterized in that it does not rely on the resource manager (RM) XA support, but by scheduling business logic (provided by the service system) to implement a distributed transaction. Mainly by the three-step operation, Try: try to do business, Confirm: Verify that the business, Cancel: to cancel the execution business.
Second, the characteristic pattern:
The pattern of the code embedded high, the write operation requires three kinds of business needs of each step.
The presence or absence of a local transaction control mode can support the use of a wide range.
Data consistency control is almost entirely controlled by the developer, business development requires a high degree of difficulty.

3. TXC transaction mode
First, the principle introduction:
TXC model name comes from Taobao, the principle is to achieve before executing SQL, SQL query first impact data, then save the SQL execution information and the creation of brisk walking lock. When the need to roll back when he uses these data records database rollbacks, the present lock implementations rely redis distributed lock control.
Second, the characteristic mode:
This mode is the same low embedded code.
This mode is limited to module supports SQL mode support.
The model needs to query data due to the impact before each SQL execution, compared LCN mode consumes resources and time to be more.
This mode does not use connection resources database.
----------------

In order to solve the problem of distributed consistency, summed up many of the classic predecessors protocols and algorithms and data consistency in performance trade-off process. Relatively well-known: 2PC, 3PC, TCC, Paxos, Raft, Zab, ISR. In addition to these, the most widely used industry is actually based on MQ implementation.

  2PC (Two Phase Commit) two-phase commit: the general said, is based on the XA two-phase commit protocol. There is also more common JTA agreement.

  XA is a distributed transaction protocol. It is roughly divided into two parts: the local transaction manager and resource manager. Where the local resource managers are often implemented by the database, such as Oracle, DB2 XA interfaces are implemented. MySQL for XA support is not very good. The transaction manager as a global schedule, responsible for the various local resources commit and rollback.

   The advantages of two-phase commit are: the principle is simple, easy to implement. The disadvantage is synchronous blocking, single-point problem, inconsistent data.

   

  3PC (Three Phrase Commit) submitted three stages: divided CanCommit, PreCommit, Do Commit three stages. Phase two is to be divided into two-phase commit 1, when the pre-filed returns if the participant or No timeout interrupt the transaction.

  Three advantages phase commit is to reduce the scope of the participants blocked, and agreed to continue after the single point of failure. The disadvantage is that because preCommit stage, at this stage if a network partition occurs, the coordinator can not, participants will still be submitted by participants in physical communication with the normal, resulting in inconsistent data.

 

  TCC(Try-Confirm-Cancel)

    Try: the completion of all the checks, must reserve resources

    Confirm: Try using the reserved resources for the implementation phase of the business, if you do abnormal, you want to retry

    Cancel: Try to release the reserved resources stage

    TCC is capable of distributed transaction each resource separately locked, submitted separately and released. The same applies to strict, short execution time, high real-time requirements of the scene.

 

  Paxos algorithm: seen before "from Paxos to Zookeeper" the book, I did not understand how. To achieve more complex, Zookeeper distributed consensus is to use this to achieve. Paxos algorithm, Raft protocol and Zab (Zookeeper Atomic Broadcast) protocol is a kind of majority vote by standby to ensure data consistency.

  

  ISR (In-Sync Replicas) Mechanism: Kafka uses this mechanism to ensure data consistency. ISR considered for 2f + 1 copies, the majority voting mechanism requires only allow a maximum of f copy fails, if you want to support fault tolerance two copies, you need to maintain at least five copies.

  

  MQ implementation is essential to ensure an asynchronous type of implementation. The synchronous blocking the transaction becomes asynchronous, avoiding the contention of database transactions.

 

2.X / OpenDTP transaction model
concept: X / Open is an organization that defines a set of distributed transaction standard that defines a standardized API interface roles:

The Application the AP
RM Resouces Manager Explorer
TM transaction manager transaction manager
3.Mysql transaction processing
3.1 redo logs and undo log records to ensure that the log on disk persistent
3.2 update data records
3.3 commit the transaction, redo write commit records

4. 2PC protocol
two-phase commit

Stage a: submit a transaction request
TM to all the AP transmits transaction content, asking whether the execution of a transaction commit, and wait for response of each AP of
the enforcement branch
of each AP node to perform transaction operations, recording undo and redo information into the transaction log, after try to submit during the time-consuming operation and preparation have been completed ahead of time to ensure the follow-up
success rate of transactions submitted
in response to each AP to TM feedback Affairs asked
each AP successful execution of the transaction operation, then back to the TM yes of response; if AP not successfully executing a transaction, the response feedback TM no
stage II: implementation of the transaction commits

Problems of
data consistency problem: When the AP and TM are down, and the new elected TM downtime AP in the end do not know what has been done operation, if the operation of the remaining two survived the AP (commit or abort ) as a judge, it is likely because of operational downtime and survival operations inconsistencies resulting in inconsistent data
synchronization obstruction: a second stage, if all of the AP are down the block waiting for the AP TM can only return
5.3PC agreement
stage a: canCommit: TM commit request AP whether
phase II: precommit
stage three: doCommit
implement a distributed transaction 6.
Atomikos
7.MQ achieve a final consistency
7.1 duplicate message consumer solution
idempotent check: a new terminal in the consumer tables, use a unique index, consumption data access to a data table insert data, if it proves successful insertion is the first time the consumer, if the insertion fails to prove not the first time the consumer
log table to judge or be judged by the state of the lock, the consumer side to create a log table, insert an id and state consumer, post-consumer success to update the status of
eventual consistency model:
query mode
compensation mode: a call B, after the failure of the automatic re Rollback original operation, if it can not be retried automatically notifies operators, labor compensation (such as manual reconciliation), or notification technology, surveillance, early warning
TCC transaction model

8.LCN achieve
service consumer annotated @TxcTransaction (timeout = 1000 * 10)
service provider to obtain global transaction ID, and bound to a context
public int updateStock (OrderDO orderDO) {

// Get the global transaction ID, and bound to a context

String xid = RpcContext.getContext().getAttachment("xid");

TxcContext.bind(xid,null);

// implement their own business logic

int ret = jdbcTemplate.update("update stock set amount = amount - ? where product_id = ?",new Object[]{orderDO.getNumber(), orderDO.getProductId()});

TxcContext.unbind();

Return the right;

}

 

Guess you like

Origin www.cnblogs.com/klb561/p/12031598.html