Distributed Transactions Concepts and Importance

Introduction

Anyone that has a little bit understand about database will know the concept of a A guide to database transaction. To simply put, it is to make sure that a series of database transaction is commit or rollback together base on the execution status of the function to ensure the ACID properties of the database.

However, when we are building large scale application or adopting a microservices architecture, you will have multiple machines (nodes) running different services and connecting to different database.

Distributed Transactions Concepts and Importance 2023-11-20 11.31.32.excalidraw.png

Sometimes, there needs to be interactions between different services which will carry out a dedicated action in that service's own database. But the problem comes when let say, one of the service failed, it performed a rollback on its own database actions, but the other service that is called using rpc is not aware of this failure and still continues with the database action instead of rollback. Therefore violating the Consistency of the data

The goal of distributed transaction is such that all operation under the same gloabltransaction should be all succeeded or all failed and rollback

BASE Properties

In order to understand distributed transaction, it is important for us to understand some of the basic properties of Consistency Model.

BASE theory is a framework proposed by architects at eBay to address the limitations of achieving strong consistency in distributed systems, as highlighted in the cap theorem. The CAP theorem posits a trade-off between Consistency and Availability in distributed systems.
BASE theory suggests a balanced approach, focusing on achieving eventual consistency tailored to the specific needs of an application.

Basically Available (BA)

This principle implies that a system remains operational even during unforeseen failures, albeit with potential performance or functionality degradation. For instance, a system's response time might increase from 10ms to 50ms but remains functional.

Soft state

BASE allows for a 'soft state' in systems, where data can exist in intermediate states without significantly affecting the system's overall availability. It accepts the fact that data replication across different nodes might have latency.

Eventual Consistency

BASE ensures that, barring any new updates, data across the system will eventually become consistent. This means all clients will ultimately access the most up-to-date data.

Common solutions for distributed transaction

Types of distributed transaction

Common frameworks

X/Open DTP Transaction Model

XA Protocol

Java Transaction API (JTA)

Components of the X/Open DTP Model

Transaction Model in Distributed Systems

XA Protocol

Two-Phase Commit Protocol (2PC)

The XA protocol uses 2PC for coordinating multiple resources in a global transaction, supported by MySQL version 5.5 and above.
Distributed Transactions Concepts and Importance 2023-11-20 20.57.24.excalidraw.png

Preparation Phase

Transaction managers(TM) send a 'Prepared' message to each participant, who then execute the transaction locally and write Undo/Redo logs, without committing the transaction yet.
- Undo Logs: Record pre-modification data for potential rollbacks.
- Redo Logs: Record post-modification data for committing the transaction.

Commit Phase

Characteristics and Limitations

TCC (Try-Confirm-Cancel) Transactions

Divides transaction into serveral stages:

  1. Try (business checks and resource reservation)
  2. Confirm: Commit the changes, by default, confirm will succeed once try succeed since the resources are reserved
  3. Cancel: Business logic fails and perform rollback. Release the resources
    Distributed Transactions Concepts and Importance 2023-11-20 21.06.49.excalidraw.png
Operation Meaning
Try Reserve resources and data verfication
Confirm confirm execution and commit data
cancel roll back data

Advantages

Challenges:

Ensures idempotence for each operation to handle potential retries or network issues.

Transactional Messaging

Uses message queues to provide a mechanism similar to XA for achieving eventual consistency in distributed transactions.
Distributed Transactions Concepts and Importance 2023-11-20 21.43.54.excalidraw.png

Transaction message type

Advantage

Disadvantage

Conclusion

In the realm of distributed systems, there are multiple solutions to handle distributed transactions, including the XA's Two-Phase Commit (2PC), TCC (Try-Confirm-Cancel), and transactional messaging using MQ. Frameworks like Seata provide support for these various modes, offering a range of options for different scenarios.

However, it's crucial to approach the implementation of these solutions with caution and careful consideration. Except for specific scenarios requiring strong data consistency, it's advisable to avoid using distributed transactions whenever possible. This is because, regardless of how performance-optimized these solutions might be, the introduction of distributed transactions into a project can significantly reduce overall efficiency. This drawback becomes particularly apparent in high-concurrency environments.

For operations involving multiple links, alternative strategies or perspectives should be sought to circumvent the need for distributed transactions. Examples include changing the approach to locking stock during order placement or modifying how discount coupons are recorded. These alternatives can often achieve the desired outcome without resorting to the complexities of distributed transactions.

In summary, both distributed transactions and distributed locks should be used sparingly. If there's a need to use them, the preference should be towards flexible (soft) transactions, resorting to rigid (hard) transactions only when absolutely necessary. Similarly, when using distributed locks, it's advisable to minimize the granularity of the locks. This cautious approach ensures that the system remains efficient and scalable while still meeting the essential requirements of data consistency and integrity.