serverlesstechnology / cqrs

A lightweight, opinionated CQRS and event sourcing framework.
Other
384 stars 42 forks source link

Consistency over mulitple commands #85

Closed paulkre closed 11 months ago

paulkre commented 11 months ago
cqrs_a.execute(id_a, command_a).await?;
cqrs_b.execute(id_b, command_b).await?;

If the server crashes right after executing the first line, only the aggregate of cqrs_a will be in the correct state and the state of cqrs_b's aggregate will be incorrect. Is there some way of ensuring that the resultant events of both commands are either all commited, if both commands succeed, or all dropped, if one of the commands fails?

paulkre commented 11 months ago

Maybe there needs to be a third aggregate containing the shared state between the first two aggregates?

davegarred commented 11 months ago

If I understand correctly, you are trying to modify two aggregates and you'd like to have an atomic commit for both? If this is going to be common with your aggregates, you might want to consider you need to incorporate the logic of both into a larger aggregate.

An aggregate always should be an entity, but not all entities need to be an aggregate. E.g., you could have a Customer and an Account aggregate, but if the business rules lock them together it might be better to have a single CustomerAccount aggregate.

Context might help to understand what the reasoning is here, feel free to start a discussion on this.

paulkre commented 11 months ago

I added some context here: https://github.com/serverlesstechnology/cqrs/discussions/86