If events are kept in order and as when using CQRS / event sourcing

I recently took a CQRS workshop which made me wonder how events are stored when using event sources.

I think that after every event, the whole application should be in a valid state (otherwise the replay function is useless). This means that the events must be stored exactly in the order they were in. I also believe that the order of events for all aggregates is important. For example, a user can create a product and an order (a product and an order are both separate aggregates and a product of orders via an ID). This means that "create product event" must be saved before "adding product to event". Otherwise, replay may result in an invalid state where an order referencing a product exists before the product exists.

How is this situation handled? Should you always send events to the database using synchronous methods such as locking the database? Is this solution scalable? Or should you store events for each aggregate in a different table? But how do you secure the order? Another option is to save time for each event and sort it. Is the accuracy of the PC timers high enough to be able to do this?

+3


source to share


2 answers


You can look at NEventStore for a source that does pretty much what you want. All events are stored in the order in which they are committed, the event store doesn't really care about your specific business objects, it just atomizes the collection of events. It's for your application to generate them in the correct order.

Also, keep in mind that your business object must be a proper aggregated root, which means that, at best, it only has a different AR ID. One AR doesn't care about the other, but the business process will tell you in which order to do things, for example: OrderCreated => the order has been saved => Create invoice (referring to the order) => Created invoice.

For example, a user can make a product and an order (product and order are both separate aggregates and order products via an identifier).



No, I couldn't. He needs product information before he can create an order. Customers don't add invisible products to their shopping cart. And customers don't create products either.

There is a lot to talk about it here, but for everything to work properly, you really need well-designed domain objects and understand what an event source (ES) really is (hint: it expresses the state of an object as a stream of events).

Btw, when using ES, you should focus on correct object level implementation, you shouldn't create your own event store.

+3


source


For example, a user can create a product and an order (product and order both individual aggregates and products order ID).

CQRS, Command Request Responsibility Segmentation, means that you are using two different models, one for the write model, one for the read model. Therefore, if you want to add a product to the cart (for example AddProductToCart

), the product must be in the readable model.



The point is ultimately consistency. For example, a user cannot see a product that you added to stock a few seconds ago because the read model and write model are not yet in sync. They will eventually sync (and when they are your users, you will see the added product).

0


source







All Articles