Skip to content

Distributed Transactions

Full Stack edited this page Mar 2, 2025 · 3 revisions

Distributed transaction

Single logical unit of work that spans multiple systems, often involving different databases or microservices.

  • Goal is to ensure that all the operations within the transaction either succeed completely (commit) or fail completely (rollback), maintaining data consistency across the distributed system.

𝐂𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞𝐬 𝐨𝐟 𝐃𝐢𝐬𝐭𝐫𝐢𝐛𝐮𝐭𝐞𝐝 𝐓𝐫𝐚𝐧𝐬𝐚𝐜𝐭𝐢𝐨𝐧𝐬

  • Distributed nature ➟ introduces complexities not present in traditional, single-database transactions.

Network Issues

  • Network latency, partitions and failures can disrupt communication between systems, making it difficult to coordinate transaction completion.

Partial Failures

  • Individual systems within the distributed transaction might fail independently.
  • Ensuring atomicity (all or nothing) becomes challenging.

[3.] Consistency Models ◾ Distributed systems often employ weaker consistency models (e.g., eventual consistency) than the strong consistency typically found in single-database transactions. ◾ This can lead to temporary inconsistencies that need to be managed carefully.

[4.] Concurrency Control ◾ Concurrent access to data across different systems can lead to conflicts and race conditions, requiring sophisticated concurrency control mechanisms.

The ACID properties (Atomicity, Consistency, Isolation, Durability), become more difficult to guarantee in a distributed environment.

𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡𝐞𝐬 𝐭𝐨 𝐃𝐢𝐬𝐭𝐫𝐢𝐛𝐮𝐭𝐞𝐝 𝐓𝐫𝐚𝐧𝐬𝐚𝐜𝐭𝐢𝐨𝐧𝐬

  • Two-Phase Commit (2PC)
  • Phase 1 (Prepare) - Coordinator sends a 'prepare' message to all participants. Each participant checks if it can commit the transaction and responds with a 'yes' (prepared) or 'no' (abort).
  • Phase 2 (Commit/Abort) - If all participants vote 'yes,' the coordinator sends a 'commit' message. If any participant voted 'no' or didn't respond, the coordinator sends an 'abort' message.

Participants execute the corresponding action.

Sagas

  • Orchestration - A central orchestrator coordinates the saga, sending commands to participants and handling responses.
  • Choreography - Participants react to events published by other participants, leading to a decentralized coordination.
  • **Compensating Transactions **- Each step in the saga has a corresponding compensating transaction that can undo its effects in case of failure.

BASE Transactions

  • A relaxed consistency model.
  • BASE stands for - Basically Available, Soft state, Eventual consistency.

TCC (Try-Confirm-Cancel)

  • Try - Reserve resources required for the transaction.
  • Confirm - If all resources are reserved, execute the operations.
  • Cancel - If any resource reservation fails, release all reserved resources.

There are more approaches like Quorum-based Consensus (Paxos, Raft), Distributed Locking, Transactional Outbox Pattern etc.

References

Clone this wiki locally