Theory of CAP
CAP stands for COnsistency Avalilability Partition tolerance
C Consistency
Consistency means that the read operation after the write operation can read the latest data status. If the data is distributed on multiple nodes, the data read from any node is the latest data status
A-Availability
Availability means that any transaction operation can get response results without response timeouts or response errors
P-Partitionon tolerance
Usually, each node of a distributed system is deployed in different subnets, which is called network partition. It is inevitable that communication between nodes will fail due to network problems, but they can still provide services to the outside, which is called partition tolerance
A distributed system can only meet both requirements
AP
Abandon consistency in favor of partition tolerance and availability,
CP
Instead of availability, ZK pursues consistency and fault tolerance of zones. In fact, ZK pursues strong consistency, such as inter-bank transfer, each transfer request is completed only after the entire transaction is completed by the banking systems of both parties
AC
Consistency and availability can be achieved by giving up partition tolerance, that is, not partitioning and not considering problems due to network failure or node failure. The system
Will not be a standard distributed system, our most commonly used relational data will satisfy CA.
Introduction to Base Theory
Short for Basically Acaliable, soft stat, and Eventually consistent, BASE theory is an extension of THE AP in CAP that achieves usability by sacrificing strong consistency, So when you have a guzheng that allows some parts to be unusable just to make sure that the core functions are available, that allows the data to be inconsistent for a period of time, but eventually reach a consistent state, that satisfies the BASE theory of a transaction, we call it a “flexible transaction.”
Distributed transaction Solution 2PC
2PC refers to the transaction is divided into two phases of commit, is the whole transaction process into two phases, the preparation phase, commit phase
Preparation stage: Bank1 will fill in the transfer information such as name and account amount and transfer to Bank2 via Alipay
Submission stage: Bank1 triggers the transfer button to transfer to Bank2
In the computer, some relational databases such as Oracle and MySQL support the two-phase commit protocol, as shown in the following figure:
- Prepare Phase: The transaction manager sends a Prepare message to each participant, and each database participant executes the transaction locally
Log, and write a local Undo/Redo log.
Undo logs record the data before the change and are used for database rollback. Redo logs record the data after the change and are used for the number of writes after the transaction is committed
According to the documents)
- Commit Phase: If the transaction manager receives an execution failure or timeout message from each participant, it sends it directly to each participant
Send a Rollback message. Otherwise, send a Commit message; Participants perform commit or rollback operations according to the transaction manager’s instructions
To release lock resources used during transaction processing. Note: Lock resources must be released at the last stage.
Flexible Transaction – Reliable Message – Final Consistency Scheme (Asynchronous assurance)
Implement business services in business affairs before submit, to the real-time message service request message, real-time message service record message data only, not actual, business processing services in the business transaction commits, the real-time message service delivery confirmation, only after confirmed to send instructions, real-time news will truly sent.
To ensure high concurrency, the inventory service itself can send messages to the inventory service. The inventory service itself can also use automatic unlock mode, message queue
Transaction sponsors (producers) to send a message to the message middleware, the transaction participants receiving messages from the message middleware, the transaction between give up and message middleware, the transaction between the parties (consumer affairs) and message middleware is communication through the network, because of the uncertainty of the network communication cause problems of distributed transactions