分布式事务
Distributed Systems · 13 notes
- 2. Distributed Transactionshistorical
1. What Are Distributed Transactions - Transactions in a distributed environment. - Traditional transactions operate on a single database node, while distributed transactions span multiple database nodes, or even other data sources such as Redis and MongoDB. 2. Why Distributed Transactions Are Needed - Traditional database transactions can only guarantee transactions within a single database and cannot do so across multiple databases.
- 2.1 Distributed Transaction Solution: 2PChistorical
1. What Is 2PC - A distributed transaction solution that guarantees strong consistency. 2. 2PC Process - Divide the transaction into two phases. - Phase 1: the transaction manager sends prepare requests to all databases. - Phase 2 depends on the result of Phase 1.
- 2.2 Distributed Transaction Solution: TCChistorical
1. What Is TCC - A distributed transaction solution that guarantees eventual consistency. 2. TCC Process - TCC: Try, Confirm, Cancel. - Divide the transaction into two phases. - In Phase 1, execute Try to perform business checks and reserve resources. - Phase 2 depends on the result of Phase 1.
- 2.3 Distributed Transaction Solution: Reliable-Message Eventual Consistencyhistorical
1. What Is Reliable-Message Eventual Consistency - A distributed transaction solution that guarantees eventual consistency. 2. Reliable-Message Eventual-Consistency Process. 3. Use Cases - Suitable for businesses that require eventual consistency, have low time sensitivity, and where the distributed transaction can only succeed rather than fail, such as awarding points after registration or coupons after login.
- 2.4 Distributed Transaction Solution: Best-Effort Notificationhistorical
1. What Is Best-Effort Notification - A distributed transaction solution that guarantees eventual consistency. 2. Best-Effort Notification Process. 3. Use Cases - Suitable for businesses that require eventual consistency, have low time sensitivity, allow a small number of distributed transactions to fail, and where the passive side's processing result does not affect the active side's processing result, such as bank notifications and payment-result notifications.
- 2.5 Distributed Transaction Solution: Sagahistorical
1. What Is Saga - A distributed transaction solution that guarantees eventual consistency. 2. Saga Process - There are multiple transaction participants, and each participant has two pieces of logic: a forward operation and a reverse operation. - Divide the transaction into two phases.
- 2.6 Distributed Transaction Solution: 3PChistorical
1. What Is 3PC - A distributed transaction solution that guarantees strong consistency and is an improved version of 2PC. 2. 3PC Process - Split the first phase of 2PC into two steps, so the whole transaction process has three phases: CanCommit, PreCommit, and DoCommit.
- 2.7 Distributed Transaction Solution: Two Phaseshistorical
1. What Are Two Phases - Divide the entire transaction process into two phases: preparation phase; commit phase or rollback phase. 2. Two-Phase Implementations 2.1. Database Layer - Two-Phase Implementation: 2PC. 2.2. Application Layer - Two-Phase Implementation: TCC.
- 2.8 Reliable-Message Eventual-Consistency Implementation: Local Message Tablehistorical
1. What Is a Local Message Table - Use two transactions, placing an order and then adding points, as an example. The ordering service is Service A and the points service is Service B. 1. Service A executes ordering logic. 2. After Service A successfully places the order, it sends a message to MQ. 3. Service B consumes the message and processes its local transaction.
- 2.9 Two-Phase Implementation: 2PChistorical
1. What Is 2PC - 2PC: Two-Phase Commit. - Divide the transaction into two phases. - In the first phase, the transaction manager sends prepare requests to all databases. - If all respond ok, execute commit in the second phase; if any responds fail, execute rollback in the second phase.
- 2.10 Two-Phase Implementation: TCChistorical
1. What Is TCC - 2PC is a two-phase approach at the database layer, while TCC is a two-phase approach at the application layer. - TCC: Try: attempt to execute the transaction; Confirm: confirm execution of the transaction; Cancel: cancel execution of the transaction. - In essence, it is also a two-phase transaction.
- 2.11 Best-Effort Notification Implementation: MQhistorical
1. What It Is - 1. The producer finishes executing its local transaction and sends a message to MQ. 2. MQ sends the message to the consumer. 3. The consumer consumes the message and executes its local transaction; if successful it ack's, and if it fails it nack's and requeues the message. 4. The consumer can actively call the producer's interface to query message status.
- 2.12 RocketMQ Transaction Messageshistorical
1. What Are RocketMQ Transaction Messages - Traditional local message tables depend on a message table in the database. - RocketMQ transactions encapsulate the local-message-table approach by moving the local message table into MQ, solving the atomicity problem between Producer-side message sending and local transaction execution.