In Event Store / CQRS architecture, why are events stored instead of commands?

StackOverflow https://stackoverflow.com/questions/14656338

  •  06-03-2022
  •  | 
  •  

Pergunta

Presumably we could resurrect state by applying the same set of commands, so why not simply store commands rather than events?

Foi útil?

Solução

Events, communicate "this happened in our system". Events occur when a command has been accepted and processed. No one can reject or change the fact that it happened. It's the only authoritative source of changes in the system

Commands are just a way for a part of the system (like a UI) to tell the component in charge of making changes to the system (the "command handler") what it wants done. However, the command handler can choose not to process the command for various reasons. The UI could have stale information and processing the command wouldn't make business sense or the user could have not had the privileges to perform that action. Either way, the command is really just a request & have no bearing on the state of a system

.

Outras dicas

"Stream of Commands" can be processed by your app to produce "Stream of Events", which can produce "Current data".

It certainly is possible to store commands, but the question is why would you do that?

Some possible answers include, the command itself is useful data. You might want to know how many times user tried to add items to their cart (command), even though no more items are added (events) after the cart is full. Granted, this can be solved with just another event, possibly named "Item Added Although Cart Is Full"

In that light, commands are events. Which you process with your business logic to produce another stream of events, lets call them State events. Which you can possibly create another stream, or project onto a relational data structure, or whatever. Adding command could possibly add complexity, at the benefit of more data. Just like how Event Sourcing itself is a step higher than traditional "Current state" data stores.

Edit: The above clears out the terminologies and purpose.

Conclusion: Commands = CommandGivenEvents, "Events" = BusinessLogicProcessedAndStoredEvents. Different purposes. Like how if you change your event handlers, the projected data will be different. If you change business logic, the Commands will produce a different Event Stream. One step higher.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top