liveBook · Manning

Two-phase commit

As explained in Kafka for Architects

Two-phase commit is a distributed transaction protocol in which a coordinator first prepares all participants and then directs them to commit or abort as a group.

Two-phase commit (2PC) coordinates a distributed transaction across independent systems so every participant commits, or all participants abort. section 7.4.3

The protocol divides coordination into two stages:

  • Prepare: the transaction manager asks each participant to complete its work and report either readiness or failure. section 7.4.3
  • Commit or abort: if every participant is ready, the manager commits the transaction; otherwise, it instructs all participants to roll back. section 7.4.3

Profile update and the dual-write problem

A ProfileService updates its transactional database and publishes a profile-change event to Kafka. If either action occurs first and the second fails, the systems diverge. Kafka does not currently support 2PC, so the transactional outbox pattern stores the event with the database update; a separate process later publishes it to Kafka. section 7.4.3

How Kafka for Architects covers it

How deep it goes
Defines prepare, commit, and abort steps, then stops at Kafka’s lack of current 2PC support and the outbox workaround.
Examples
Uses a profile database update paired with a Kafka notification to expose dual-write inconsistency.
What it assumes
Assumes familiarity with independent systems, transactional databases, Kafka events, and consistency across distributed operations.
What you can do after
Evaluate when transactional outbox is preferable to direct cross-system writes involving Kafka.
How it connects
Connects distributed transactions, transaction managers, Kafka, transactional outbox, dual writes, and exactly-once semantics.

Also explained in

Compare how 4 books explain two-phase commit →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka for Architects. The section links point to the parts of the book it draws on.