liveBook · Manning

Two-phase commit

As explained in Microservices Patterns, Second Edition

Two-phase commit is a distributed transaction protocol that coordinates participants through a prepare phase and a commit or rollback phase.

Two-phase commit (2PC) coordinates a distributed transaction through a prepare phase followed by a commit or rollback phase. It is a traditional way to manage operations spanning services, but its runtime coupling can create availability and latency problems in microservice architectures. section 7.5.1 section 9.1

For operations that cross service boundaries:

  • Avoid treating 2PC as the default: its complexity and runtime coupling make it a poor fit for modern microservices, and many modern databases do not support it. section 7.5.1 section 9.4
  • Prefer a Saga when implementing distributed system commands; it coordinates local ACID transactions without 2PC’s runtime coupling. section 9.4

Creating an order

A createOrder operation can span the Order and Consumer services: the Order Service persists an order, while the Consumer Service reserves the customer’s credit. This illustrates why a distributed operation crosses independent transaction boundaries. section 7.5.1

Figure 7.8 Services are process and transaction boundaries, so implementing a distributed operation is much more complicated than a local operation that simply spans different classes in different packages.
services are boundaries

Use the diagram to see why crossing service and transaction boundaries makes a distributed operation more complicated than a local one. section 7.5.1

How Microservices Patterns, Second Edition covers it

How deep it goes
Defines the protocol’s role and trade-offs, but does not detail its phase mechanics.
Examples
Uses order creation across Order and Consumer services, including customer credit reservation, to illustrate distributed transaction boundaries.
What you can do after
Evaluate classic distributed transactions against Saga for commands spanning microservices.
How it connects
Contrasts with ACID transactions, eventual consistency, and the Saga pattern in microservice architectures.

Also explained in

Compare how 5 books explain two-phase commit →

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.