Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event-sourcing and sagas - compensating transactions

First question on SO (really???), so bear with me please :)

We're architecting a solution using event sourcing. Some of our business processes will be long-running, thus we're planning on using sagas to orchestrate commands to several aggregate roots.

In my understanding, if a saga-issued command should fail, the saga would be responsible to issue compensating commands to all the previously invoked aggregate roots.

What should be the course of action if the state of an aggregate root would be mutated externally (i.e. by some other process/user) after it takes part in the saga, but before the saga fails and issues a compensating command to that aggregate root?

In other words, how would one try to compensate for an event that is not the last one in a certain aggregate root's event stream (speaking in EventStore lingo)?

like image 575
tdaliviu Avatar asked Oct 30 '22 18:10

tdaliviu


1 Answers

This is a rather tricky situation since what I am seeing is that you may end up with an invalid AR after your compensating entry, making your compensating action invalid.

You are probably going to have to re-look at the design of the process such that changes to ARs are not made until you are sure that your process manager (saga) will be able to complete. Perhaps temporarily storing the values for later change.

Another approach may be to prevent certain commands on your AR should it be in a certain state that indicates that it may lead to issues for those commands. The user would then not be able to issue those commands. Your process manager would take care of that state and any state expiries/timeouts and so forth.

like image 100
Eben Roux Avatar answered Nov 22 '22 19:11

Eben Roux