Compensating Action

A compensating action is a manual or automated process that is executed to reverse the effects of a previously executed operation in a distributed system, without using a two-phase commit protocol. Compensating actions are typically implemented as a set of steps that are designed to undo the changes made by the original operation.

While we discuss compensating actions, it is worth mentioning two related concepts: compensating transactions and compensating workflows. We elaborate on each of these here:

Compensation Transactions

Compensating actions are similar to compensating transactions, but operate at a higher level of abstraction. A compensating transaction is a technique used to maintain consistency across multiple resources in a distributed system, in which an additional transaction is executed to undo the effects of a previous transaction in case of a failure or error. Unlike compensating actions, compensating transactions use a two-phase commit protocol, which is more complex and can lead to performance issues in some cases.

Therefore, the main difference between a compensating action and a compensating transaction is the level of complexity and the use of two-phase commit protocol. Compensating actions are typically simpler and more flexible, but they may require more manual intervention in case of errors, while compensating transactions are more reliable and automated, but they can add more complexity to the system.

Compensating Workflows

As mentioned above, a compensating action is a specific action that is performed to undo the effects of a previously executed action. It is usually a simple, atomic action that can be executed quickly and independently. For example, if an initial action is to debit a user’s account for a purchase, the compensating action would be to credit the user’s account back for that purchase.

On the other hand, a compensating workflow is a series of actions that need to be executed in a specific order to undo the effects of a failed transaction. It is a more complex sequence of actions that may involve multiple services, databases, or other systems. For example, if an order processing workflow involves charging the customer’s credit card, shipping the product, and updating inventory, a compensating workflow would need to reverse all of these actions in the correct order if any one of them fails.

In summary, a compensating action is a simple, atomic action that undoes the effect of a single transaction, while a compensating workflow is a more complex sequence of actions that undoes the effects of a failed business process that may involve multiple transactions.