Saga event Handling and Event Handling

Hi all,

I am new to Microservice Patterns such as EventSourcing and Saga, we are implementing these patterns using Axon Framework. Now from what I understand about Saga from Chris’s blog if we use choreography-based saga on 2 MS such as

  1. Order Service and

  2. Customer Service

Here Order Service receives an compensating event called CreditLimitExceeded from Customer service if the order can not be fulfilled because of Credit Limit in Customer Service, now if I’ve to implement it using Axon Framework I can use Saga where I’ll start a Saga from creation of an order and based on CreditLimitExceeded and I’ll end saga and take any compensating action such as rollback order or inform user about his/her credit limit. This EndSaga can be handled conditionally or by @EndSaga annotation over an SagaEventHandler.

What I need to know is this can also be achieved by normal @EventHandler annotations in Axon then why to use Saga annotations what is the use of Saga store and how is it used? From a previous conversation with Allard on this stackoveflow thread I understood that Saga table is cleared if saga is ended. How is this saga_entry table utilized and what is different in implementing this Saga using simple EventHandlers?

Another thing I would like to know is that does the Saga that we create using @StartSaga and @Saga annotation does it represent Saga Orchestrator and thus it is not an Choreographed Saga instead it is an Orchestrated Saga?

Apologies for the ignorance that I’ve regarding Axon and patterns but insights would be really helpful and welcome.

Thanks and Regards,
Malay M

Hi Malay,

A Saga in Axon terms is coordinating a ‘Complex Business Transaction’ between several Aggregates, in doing so potentially spanning a broad time frame, with the necessity of storing state from several events to perform the ‘Complex Business Transaction’/make it’s decision.

Certain events will instantiate a Saga (through the @StartSaga annotation), some events will update state needed to complete the ‘complex business transaction’ and some events signal the end of that transaction, thus with a @EndSaga or the SagaLifecycle.end() function.

Thus the added benefit of a Saga is that it coordinates actions between several aggregates over a unspecified span of time, and it stores it’s state which it needs to make the right decisions between handling these events.

To be able to handle the right events you’re interested in in your Saga instance, you are inclined to use the @SagaEventHandler. This is a hard requirement to be able to point out which field in a given event associates to which specific Saga instance it has to perform work on.

Your assumption is right, you can perform the same logic in a regular Event Handling Component. But in doing so will require you to keep track of every single ‘complex business transaction’ in transit yourself, and storing it as well as you wont know when new events you’re interested in for a given transaction will pass along.
By using the Saga set up in Axon, the framework abstracts the notion of several of such actions, happening at the same time, from you and keeps you focused on the task at hand: correctly setting up the coordination between these components.

As the Saga is thus some form of Object being alive with a notion of time tied to it as well, you’re thus also inclined to store the Saga.

That’s what the saga_entry is for. That’s also what the association values are for - you can regard those as the identifiers of a saga_entry.

I hope I have giving you enough background to work with Sagas in a pleasant manner Malay.

If not, feel free to post more questions.
Do note that at AxonIQ we also provide a training around the framework. Saga’s are covered in plenty a’ detail in there as well.

Cheers,
Steven

Hi Steven,

Thanks for taking time to write this elaborate explanation, after your answer now the logs produced by sample Saga application make a lot of sense. Also now I understand how to use the association fields with Saga.

Cheers,
Malay

Hi Malay,

I am very please to hear that my explanation helped you out!

Why try to make the usage of the framework as pleasant an experience as possible.

Thus if any issues arise during the implementation phase or any thing is unclear from the Reference Guide, please keep us apprised of that.

Cheers,
Steven