Compensating Transactions

Some business operations have a logical undo. Consider the way banks handle bad checks. When you deposit a bad check at the ATM, the bank adds the amount of the check to your account. When the bank discovers the check is bad, it undoes the deposit by deducting an identical amount from your account and returns the check to you in the mail. This logical undo is called a compensating transaction. Not every transaction has a compensating transaction, but if it does, you should use caution when incorporating compensating transactions into your application. It is very tricky to use compensating transactions without jeopardizing system consistency. For example, imagine that after depositing the check, you apply for a loan. The bank’s loan consultant checks your balance and decides to grant you the loan based on the new increased balance. Once the bank executes the compensating transaction, the system is in an inconsistent state—the account balance is correct, but a loan program is in progress—one that should not have been launched based on the corrected balance. The bank could, of course, perform a compensating transaction for the loan application, except that in the meantime you might have used that loan to start a new business, and so on. As you can see, once the cat is out of the bag, it is difficult to compensate in a comprehensive and consistent manner.

If compensating transactions are bad, why bother with them at all? Compensating transactions are necessary because they enable you to deal efficiently with transactions whose normal execution time is unacceptable. Even though the bad check may bounce after two days, the bank does not expect a customer to wait at the ATM for two days until the check is cleared. Additionally, it is unrealistic to keep a lock on the customer’s account for two days because no other operation on the account can take place until the depositing transaction is done. The bank has to take the chance and use a compensating transaction as a safety net. The bank, in this case, trades transaction throughput for a small, calculated risk in system consistency.

In general, compensating transactions are useful when the transaction for which they compensate is potentially long. Compensating transactions offer a high throughput alternative, allowing you to maintain locks in the resource managers for a minimum amount of time.

Get COM & .NET Component Services now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.