How do you implement a compensating transaction in a choreography saga?
Compensation in a choreography saga uses exactly the same mechanism as the happy path: a service adds a plain event listener for the relevant failure event, and reacts by sending a command against its own aggregate to undo the effect of its earlier step. For instance, if Accounts Service fails to deposit funds and publishes DepositFailedEvent, Loans Service listens for that event and sends a CancelLoanCommand against its own LoanAggregate, cancelling the loan it approved earlier. No special saga-aware mode is required — the service that needs to compensate simply adds one more listener alongside whatever others it already has. Because events can be redelivered, the compensating command handler should also be idempotent, so it behaves correctly even if the failure event arrives more than once.
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