A name that’s almost synonymous with Event Sourcing is Greg Young. I suggest spending some time watching his many YouTube videos to get a grasp of what event sourcing is. He is the main developer of EventStore, an event storage engine that has fast become my first choice for event sourcing.
What is Event Sourcing?
I’m going to give you a generic definition first.
An event sourcing pattern is defined by: the use of an append-only store to record the full series of events that describe actions taken on data in a domain, rather than storing just the current state.
Good question. Consider the following scenario: You look at your bank balance and don’t agree with what is shown. You ring them to complain that the balance isn’t correct. If the bank didn’t use event sourcing they would only have the current state (balance) of your account. So, it would be your word against theirs.
In its most simple form, event sourcing is nothing but the storage of all the domain events so that your state changes are represented as a series of actions. We treat state changes as a first-class concept.
The reasons for using the event sourcing pattern are countless and include:
The ability to track all changes to the domain
This is perhaps the core foundation on which an event sourcing system is built. Since we will store all domain events completed to aggregates (I’ll cover Aggregates in part 2) there is an audit trail of how the current state of an aggregate came to be. This is reason that your company accountant uses what’s called a ledger.
To rebuild your state to a point in time
With the stored series of events we can quickly replay them to construct a “World View” of the system at a specific time. But why is this useful?
Have you ever tried to report on a newly introduced metric but the customer wanted this information for the previous 12 months? E.g. What percentage of customers removed an item from the shopping cart before they checked out? If you are using a relational database you will pull your hair out at this point, as if you were using event sourcing you would already have this information. You would simply have to replay events for the past 12 months and report based on a “shopping cart item removed” domain event. The ability to go back and construct the state gives a huge advantage to the business because they can analyse the past better, and can gain access to a free audit trail.
Less coupling between the representation of current state in the domain and storage (Ideal for CQRS)
When you store state, the natural inclination is to map your state to columns or fields in storage. This increases the coupling between your representation of state and how you decide to store them.
With event sourcing, you are only concerned about storing events. For the read side we often use a thin data layer which is highly optimized for reads. We can even use a snapshot strategy to recreate the read model faster, but that’s outside the context of this post. This is why event sourcing plays so nicely with command query responsibility segregation (CQRS).
Ability to have many world views
We can have one or many read-models for the use of case specific interpretations of current state. This is powerful because this gives us the ability to present the same information in multiple ways depending on how the information gets consumed. Imagine you have World View ABC and Word View XYZ subscribing to the same events (from the store / publisher) and updating their state/schema accordingly. ABC and XYZ might have different ideas of what each event means to them but the important concept here is that they both consume the SAME event.
Impedance mismatch is reduced between your domain and a relational database
In object oriented development we are faced with a challenge when persisting objects or aggregates to storage. We often go from a “graph” type relationship to a “tabular” form of storage. It’s even harder to convert a tabular form of data back to a graph form.
This is what’s called impedance mismatch. ORMs have been trying to minimize impedance mismatch for decades but ORM’s are a leaky abstraction. Don’t get me wrong. I love EF and NHibernate but you have to account for impedance mismatch when you choose to use an ORM. An ORM is not a magic bullet.
Event sourcing reduces this by not storing state. An event is immutable and easily serializable to a tabular form. Since we are only storing events most concerns of Impedance Mismatch go away.
Easier to debug (some times)
You can copy the events from the production environment to a dev environment and replay them to build the current state. This gives you a chance to add extra logging or allows you to step through certain parts of the code to dig deeper when something isn’t working as expected. This is basically like using the remote to pause/play/rewind to look at a sports highlight. You can go one step further by storing commands (when using CQRS) and replaying them.
So Why Not Use Event Sourcing All the Time?
Short answer. There is a time and place. Implementing event sourcing is hard and time consuming. I wouldn’t recommend it unless the advantages described really add business value.
Be careful when you select the event sourcing pattern. Read up on the pitfalls and anti-patterns like “command sourcing”.
Architecturing is hard. There are pros and cons to every choice you make. You make your life easier when you make those decisions based on requirements rather than just “resume driven development”
Something to keep in mind
ES + CQRS (Event Sourcing & Command Query Responsibility Segregation) is not a top-level architecture pattern. CQRS will complement a top level design pattern like SOA. CQRS will work well for a bounded context in DDD. Some may even go further to say CQRS fits into a business component within a bounded context.
If you aren’t sure whether your bounded context requires ES + CQRS the chances are that it doesn’t. CQRS is only needed for very specific use cases (scalability, complex business logic and large teams, etc). So choose wisely as there is a lot of complexity that comes with it. You have been warned.
Some Common Pitfalls
It’s still an unfamiliar concept to many
Despite event sourcing being a concept that has been around for a long time in software development (Since ancient times in many other fields), it is still not something that’s used often enough. As a result, you aren’t going to find a lot of people in your team familiar with the concept. Even though the pattern isn’t hard to understand; it forces people to change their way of thinking. Initially you will find it hard to grasp, especially when implementing. But once setup it’s straightforward to add new functionality because it will be a simple process of repetition.
External systems or dependencies
If our system depends on external systems then there is a risk that: we can’t replicate information gathered from an external system at a specific point in time to replay events. There are ways to get around this by storing the information gathered from the external system in the event. Be aware of this if dealing with external systems.
Just because we are building a CQRS system doesn’t mean the commands need to be asynchronous. There are times when this is not required. (Ex. Your source control system uses event sourcing but the COMMIT is not asynchronous). Just keep in mind that whilst asynchronous processing will scale better it’s not a must to implement event sourcing.
Eventual Consistency (Not really a con)
Event sourcing introduces eventual consistency to the system. While this is not a bad thing (Hint: It’s how the real world works) this makes things a hell of a lot harder to implement properly, especially the UI.
Event schema changes
Overtime your event schema will change. Events are immutable so you need to handle versioning of your events. There are many strategies for this. Pick the one that’s right for the context.
If your internal event handlers generate ID’s then they can’t be random. The idea is you can replay your events many times and the resulting state must be the same. If the system generates a random identifier while replaying, a future event might fail because it now can’t correlate to the new ID.
So… What’s Next?
In the next post I will look at some of the core concepts when trying to implement Event Sourcing. I will be using .NET and C# for code demonstrations and will introduce you to my Event Sourcing framework NEventLite.