Saga rollback/compensating transaction example

Hi,

Is there an example about how can we handle SAGA rollback/compensating transaction using Axon. I am tired of searching but didn’t get one.

Hi Manish,

A compensating action in Axon is also done with a command/event. If we take the example of a bank transfer and the amount is deducted from bank account A but cannot be added to bank account B a compensating command should be sent to bank account A to add the amount again.

I hope this answers your question

Yvonne

So does that mean, compensating actions should be manually implemented using command/events to revert each process that was executed on the way? Basically compensating transaction is nothing but the reverse execution of same steps using command/events?

Yes, for the processes that need a rollback that is correct. It is recommended to be explicit about that these are compensating actions and use different commands and events.

2 Likes

Sorry for old thread, Please tell me what Unit of Work do in this situation for rollback flow? I read many post but still cannot understand what Unit of Work using for?

It’s multiple things, but mainly helpful with making sure when there are multiple actions, either all succeed, or all fail. It depends on how you use compensating actions. For example with a saga, you would likely have something like a TransferFailedEvent. That could be handled in a Saga, which would be in it’s own UnitOfWork.