educative.io

How does a transaction work flow and work in distibuted system?

please explain how a transaction flows in distributed sys.tem?

How do nodes work on it collaboratively?


Course: https://www.educative.io/collection/10370001/4891237377638400
Lesson: https://www.educative.io/collection/page/10370001/4891237377638400/5919770157776896

Hi @Amit3, Let me explain how a transaction flows in a distributed system and how nodes work collaboratively to ensure transactional integrity:

  1. Transaction Initiation: A transaction typically begins when a user or application initiates a series of data operations. These operations can involve reading data, modifying data, or a combination of both.
  2. Transaction Request: The transaction request is sent to one of the nodes in the distributed system. This node is often referred to as the “coordinator” for the transaction. The coordinator is responsible for overseeing the execution of the transaction.
  3. Transaction Segmentation: In some cases, especially for complex transactions, the coordinator may break down the transaction into smaller sub-transactions or tasks. Each sub-transaction can be assigned to different nodes for execution. This segmentation helps distribute the workload and can improve parallelism.
  4. Local Execution: Each node involved in the transaction executes its part of the transaction. This can involve reading and modifying data as instructed by the transaction.
  5. Logging and Undo Information: To ensure the ability to roll back or recover from failures, each node records a log of the changes made during the transaction. This log includes information on what data was modified and how to undo those changes if needed.
  6. Voting or Two-Phase Commit (2PC): The coordinator collects votes from the participating nodes. In a distributed system, the Two-Phase Commit (2PC) protocol is commonly used for this purpose. In the first phase, the coordinator asks nodes whether they are prepared to commit or abort the transaction. Nodes reply with “prepared” or “abort” messages. If all nodes vote “prepared,” the coordinator proceeds to the second phase. If any node votes “abort,” the coordinator decides to abort the transaction.
  7. Transaction Conclusion: In the second phase of 2PC, the coordinator informs nodes of the final decision: commit or abort. Nodes then perform the actual commit or abort actions as instructed. This ensures that all nodes reach the same conclusion regarding the transaction’s outcome.
  8. Transaction Durability: Once nodes have committed, they mark the transaction as “durable.” This means that the changes are permanently saved and will survive system failures.
  9. Response to Client: After the transaction is completed, the coordinator sends a response back to the client or application that initiated the transaction. This response indicates whether the transaction was successfully committed or aborted.
  10. Recovery and Resynchronization: If a failure occurs at any stage of the transaction (e.g., node crashes, network issues), recovery mechanisms are in place. Nodes can use their logs to restore their state to the last consistent point and resynchronize with other nodes.

In a distributed system, nodes collaborate to ensure that all steps of the transaction are executed correctly. The use of distributed transaction protocols, such as 2PC, helps maintain consistency and coordination among nodes. These mechanisms ensure that even in the presence of failures, the distributed system behaves as if it’s a single, well-coordinated entity regarding data changes.