Hadi Rifaii
Hadi Rifaii

Reputation: 41

Event Order Handling Challenges in Axon Framework for E-Shop

I am recently working on a small e-shop project, in which I am using axon. I have a product service, an order service, a payment service, and a user service since I am using axon each of these aggregates should have an identifier so I can only work with one product at a time (correct me if I am wrong) and since I have an order and an order could have multiple products I should process each of these products in a command-event manner in the saga.

So what I am doing is as follows: In my order service (where my Saga is also located) I get a post request with an readOrderDto using this dto I send a createOrderCommand this command will cause an OrderCreatedEvent, this event will start the saga, what I am trying to do is to iterate through the product list and send a ReserveProductCommand for each product this will cause a ProductReservedEvent then since I want to calculate the price of the order, I send for each ProductReservedEvent an IncrementOrderPriceCommand which will be handled by the order aggregate to increment the total price of the order by the price of the product. Then when all the products are reserved I assume that the order has the full price saved in the eventstore and then I send a ProcessPaymentEvent in the event created through this command I check the payment details of the user (if he has sufficient balance) and update the balance and then I send another command ApproveOrderCommand to save the order in the database.

The problem I have now is that the events are not getting executed in the same order I wanted so I am not getting the right orderPrice in the database and on an answer posted on stackoverflow to this problem it was mentioned that the axon framework does not handle the events in a specific order it will just apply them on the aggregate in the same order they where saved in the eventstore. here

So I thought it will be better to just query the prices of the products form the product service and use it in the saga. By Doing so I noticed that my saga is (mostly) querying data from other services and not really coordinating activities between Aggregate types because In addition to this I have to query the user information to see if the userId in the order submitted exists in my system and this is also true for the payment details, so what would be a better solution for this problem?

Upvotes: 1

Views: 72

Answers (1)

Gerard Klijs
Gerard Klijs

Reputation: 401

Hard to say what would be better. It sounds a bit like instead of a Saga, it would better fit to have the order as Aggregate. Since by default events for the same aggregate, are part of the same segment, and are thus always handled in order, that would also solve that problem.

Note that you can set a different sequencing policy if so desired. It's also possible to have an event processor with only one segment. In that case all events are handled in exactly the same order they were published.

Upvotes: 2

Related Questions