liveBook · Manning

Distributed transaction

As explained in Microservices Patterns, Second Edition

A transaction identified and coordinated across multiple participating resource managers.

A distributed ACID transaction coordinates multiple services and their resource managers under one global transaction, extending ACID consistency across service boundaries. XA is the de facto standard for managing these transactions. section 9.1.2

An XA-based implementation requires coordinated participation:

  • Use XA-compliant databases or message brokers, with drivers and APIs that can join a global transaction. section 9.1.2
  • Propagate a global transaction identifier to participants. A transaction coordinator uses two-phase commit: participants vote during preparation, then commit or roll back. section 9.1.2
  • In microservices, prefer local ACID transactions coordinated through a Saga; classic distributed transactions add complexity and runtime coupling. section 9.1 section 9.4

Creating an order

A createOrder command spanning the Order Service and Customer Service would require both services and their database servers to participate in the same transaction. section 9.1.2

Figure 9.1 A hypothetical implementation of XA/2PC in a microservice architecture. The services register as participants with the Transaction Coordinator. They enlist their databases in the distributed transaction. The Transaction Coordinator commits the transaction in two phases. In the prepare phases, participants vote on whether they can commit. In the commit phase, the transaction coordinator instructs all participants to either commit or roll back based on the collected votes.
microservices 2pc

The diagram traces service enlistment and the coordinator’s prepare-and-commit flow. section 9.1.2

How Microservices Patterns, Second Edition covers it

How deep it goes
Details XA participation and two-phase commit, then focuses on why the approach poorly fits microservices.
Examples
Uses a createOrder command spanning the Order Service and Customer Service to illustrate shared transaction participation.
What you can do after
Evaluate why classic distributed transactions are a poor fit for microservices and why Sagas are preferred.
How it connects
Connects distributed transactions to XA, two-phase commit, ACID transactions, and the Saga pattern.

Also explained in

Compare how 5 books explain distributed transaction →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Microservices Patterns, Second Edition. The section links point to the parts of the book it draws on.