Distributed Transaction Distributed Transactions

Distributed Transaction

In this article

  1. What is Distributed Transaction
  2. Difficulties distributed transaction
  3. Common solutions
  4. Explain to resolve distributed transactions through reliable sources

What is a distributed transaction?

There is such a demand:

Jamie has two accounts, which are located A, B two databases, Xiao Ming will need funds to B of A in.

How do we achieve?

See if there is a problem to achieve the following manner.

  1. Database connection A, acquires connection connA
  2. connA open transactions
  3. A library funds decreased by 100
  4. Link Library B, acquire connB connection
  5. connB open transactions
  6. B 100 increase library funding
  7. connA.commit()
  8. connB.commit()

The above operation, normally there is no problem.

Consider the following:

After step 7 is successful, network problems, step 8 fails to submit the results at this time are: A library funds decreased 100, B library funding has not increased; it is a network problem led to the failure of our business , network factors are uncontrollable factors program, as well as others such as running after to 7, system suddenly loses power, the same result may occur. Resulting in data errors on the business impact is relatively large.

Distributed transactions can be understood: a business operation, will contain many sub-operations, each sub-business is a separate transaction, we need to consider is how to ensure that these operations are successful child, or have failed.

Difficulties distributed transaction

  1. Distributed transaction, the branch may be a variety of, there may be a variety of unusual circumstances led to some success, but failed some of these cases we need to be able to handle the program to ensure that all branches are either succeed or fail, can not appear partially successful and some fails.
  2. Distributed transaction, it is difficult to ensure the success of multiple branches at the same time. Each branch may have been providing remote interface calls, there is a problem of network failure between the branches in front of the call was successful, but the other branches due to the uncontrollable factors such as the network and the call is unsuccessful, then the data is hard to do At the same time consistency.
  3. Real-time consistency is not guaranteed. Then we can achieve eventual consistency is also possible.

What is the ultimate consistency?

Take the above transfer, a capital A library is reduced, due to network problems, Procedure B connB disconnect the library, resulting in no increase in capital library B; network problem can be restored, if the network is restored, the system can B plus to the capital, so the final data is correct; this time the middle section of the library funds is inconsistent AB ( a decrease of 100 libraries, 100 B library should be increased without increasing the data is inconsistent ), but in the end some point in time the data becomes consistent. Be able to minimize the time inconsistency problem is the system needs to be considered .

Distributed transaction, we can accept inconsistent data within a certain time period, but at some point the final data is consistent.

Common Solutions

  1. Reliable message mode
  2. TCC model to achieve

Distributed Transaction family mainly speaks of these two programs, the two schemes can basically solve the most common problems of distributed transactions, so we got to get this win in two ways.

Here we explain how to use reliable sources?

To achieve reliable message transfer mode operation

Here Insert Picture Description
Two micro Service
Service A: A library for operating the account
service B: Procedure B for the account in the database

Two services are independent of the database link, relying on functionality provided by the database, to ensure their own affairs.

For the user as follows:

  1. Call the service A, 100 debit
  2. Send a message to the debit success message service
  3. Returns the user transfer has been accepted

Adhesive

  1. Service B, the pull message transfer
  2. B library to the account +100
  3. The call message service message deletion
  4. B service consumption process, such as the emergence of network, restart the machine and other reasons, led to the failure of consumption, such as machine after recovery, you can consume this message again, retry several times eventually succeed

The entire transfer process above there are several points we need to consider:

  1. How to ensure success after A service charge, will be able to send messages successfully; if the message sending failed and lost, the business will not be back. How this relates to send to reliable sources, before the message has introduced series of articles, we can look at: talk about ways business systems deliver a message to the mq
  2. Our services are generally clustered fashion, news consumption, it may appear the situation complicated by a message consumer, when concurrent happens, how to ensure that the consumer can only be consumed once successfully. If a message was successfully transfer the consumer twice in the final accounts increased 200 B, resulting in service error. This can refer to how to ensure messages consumption idempotency, also talked about this before, we can also look at: explore ways to achieve the idempotency

Rely on message mode to achieve distributed things, consumers will be more suitable for processing a successful scenario. Such as user registration send mail, send text messages, send and other points.

to sum up

    1. This article describes what distributed transactions, some of the difficulties
    2. The most common use of the solution: a distributed asynchronous message handling things, tcc mode
    3. tcc model we introduced in a later article, currently implemented in our own system of universal tcc, has been running on the line, run relatively stable
    4. Interested in a distributed transaction, or have questions, you can add my micro letter itsoku exchange
    5. Please pay attention to the public number javacode2018 , receive a free annual salary of 400,000 information more promptly pushed to your good text

Guess you like

Origin www.cnblogs.com/Leo_wl/p/10992877.html