When and why you should not use event sourcing
TL;DR; Event sourcing is amazing when only you want to track the state changes of an application, and in order to do that, you have to do some extra work, try to keep your stack simple, ask your self twice and trice before adopting it.
As Martin define how its working:
The fundamental idea of Event Sourcing is that of ensuring every change to the state of an application is captured in an event object, and that these event objects are themselves stored in the sequence they were applied for the same lifetime as the application state itself.
Click here for his full article explaining what is event sourcing, the scope of this article is not intended for explaining that.
So shall we avoid using event sourcing at all?
Having event sourcing is more complex to manage and store compared to not using it, instead of simply overriding your application values if changes, you have to:
- Identify your application changes.
- Create events matching those changes.
- Store those events in order.
- Every time you need your application values, you need to retrivere the series of events in order and you need to reconstitutes your application as ‘aggregation’.
So, I would say only use it when you really need it
Shall I always have event sourcing whenever I have CQRS?
It depends on the case you are tring to solve, even CQRS and event sourcing are playing well together and you will see a lot of frameworks provide both CQRS and eventsourcing togather but not necessary to couple them.
Examples when you can consider having event sourcing:
- State machine models, when you have state transitions and you need to track those transitions like order operations and shipment tracking systems.
- Event based systems, like financial events and workflows.
- Activity tracking systems, like tracking users in websites
Examples when you should avoid event sourcing:
- Shopping cart changes.
- Draft and qutation changes.
- Product and services, unless there is you would like to track like price drops
- Form data.
In the end, I have seen a lot of misusage of event sourcing, the misusage cost tech teams time and frustration try to take mindful decicion before adopting event sourcing
Create your free account to unlock your custom reading experience.