What is a compensating transaction in the saga pattern?
A compensating transaction is a new, forward-moving action, run against a service whose earlier step in a saga already committed successfully, that reverses the business effect of that earlier step. It is not a database rollback and doesn't erase the fact that the earlier step happened — it adds a new fact on top that cancels or undoes the effect. For example, if creating a loan record succeeds but the subsequent step of depositing the loan amount fails, the compensating transaction explicitly cancels the loan record that was already created, since a loan with no matching deposit is no longer valid. Every step in a saga that could need to be undone requires its own explicit, deliberately designed compensating action, since there's no generic, automatic way to reverse an already-committed local transaction on an independent service.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response