liveBook · Manning

Compensating transaction

As explained in Grokking Software Architecture

A transaction that applies a corrective action to counteract the effects of an earlier completed transaction.

A compensating transaction is a corrective action that counteracts an earlier transaction that has already committed. In a distributed workflow, there may be no shared rollback button: each service has its own database, so undoing an earlier step requires explicit code. section 12.6.1

Putting the laptop back

Inventory deducts a laptop and publishes an InventoryDeducted event. Billing listens and tries to process the payment. If billing fails, it publishes BillingFailed, prompting Inventory to run a compensating transaction that puts the laptop back on the shelf. section 12.6.1

Figure 12.5 The Saga Failure Path. Because independent Microservices cannot share a single ACID "rollback" button, they must clean up after each other. When the downstream payment fails, it triggers a Compensating Transaction, forcing the upstream Inventory service to manually reverse its work.

Follow the failure path from a failed payment to the inventory correction. section 12.6.1

A Saga coordinates a sequence of local transactions and supplies compensating actions for failures later in the workflow. This avoids locking the entire database while the process runs, but correction must be designed explicitly; it is not an automatic rollback. section 12.6.1

Compensation can also be part of recovery from a network partition. A system may accept writes while disconnected, tolerate temporary inconsistency, and use a Saga to correct mistakes after the partition heals. If you accept that tradeoff, plan the corrective transactions before the network fails. section 7.3.5

How Grokking Software Architecture covers it

How deep it goes
Explains the failure-and-correction sequence and its rationale, without providing an implementation.
Examples
Illustrates compensation with inventory restored after billing fails and discusses correction after a network partition.
What you can do after
Evaluate/understand when committed work needs explicit correction rather than a shared rollback.
How it connects
Connects compensation to the Saga pattern, microservices, network partitions, and eventual consistency.

Also explained in

Compare how 3 books explain compensating transaction →

Related concepts

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