Friday, November 11, 2022

Distributed Transaction in Microservices

Distributed Transactions

Unlike single monolithic applications, distributed applications are dealing with multiple services. In such architectures, handling transactions could be a challenge.

Two-Phase Commit (2PC) is one of the distributed transaction strategies that we could apply. 


Two-Phase Commit (2PC) Pattern

The 2PC Pattern is all about updating resources on multiple nodes in a single atomic operation.

In 2PC, it carries out an update in two phases (Figure 02).

  1. Prepare: Each node, participating in the transaction, whether it is able to carry out an update in the second phase. Once each node is able to ensure it, the coordinator will be notified. If any of the nodes are unable to make it, the coordinator is notified to roll back releasing any locks they have with nodes.
  2. Commit: Carrying out the update and completing the transaction.



The Saga Pattern

As explained above, your business logic can use ACID transactions within services. However, it must use Saga Pattern in order to maintain data consistency across services.

Pattern: Maintain data consistency across services using a sequence of local transactions that are coordinated using asynchronous messaging [2].

A Saga is a sequence of local transactions. Each local transaction updates the local database using the familiar ACID transaction frameworks and publishes an event to trigger the next local transaction in the Saga. If a local transaction fails, then the Saga executes a series of compensating transactions that undo the changes, which were completed by the preceding local transactions (Figure 03).

 


The following frameworks are available to implement the Saga orchestration pattern:

  • Camunda is a Java-based framework that supports the Business Process Model and Notation (BPMN) standard for workflow and process automation.
  • Apache Camel provides implementation for the Saga EIP (Enterprise Integration Pattern), a way to define a series of related actions in a Camel route that should be either completed successfully (all of them) or not-executed or compensated.
  • IBM App Connect allows you to draw out a flow using various built-in adapters and configure its properties appropriately to create a Saga flow


No comments:

Post a Comment

Data Engineering and Best practices

Data and types Data at rest (e.g. batch data pipelines / data stored in warehouses or object stores) Data in motion (e.g. streaming pipeline...