Introduction to Event Sourcing
This article is a good start to gain basic knowledge about Event Sourcing and CQRS.
Join the DZone community and get the full member experience.Join For Free
Have you ever come across the terms Event Sourcing and CQRS? This article is a good start to gain basic knowledge about the two. Here, we will learn what Event Sourcing is and what the benefits are of having an event sourced application. CQRS will be covered in the next article.
What Is Event Sourcing?
Event sourcing is the practice of capturing all changes as domain events, which are immutable facts of things that have happened.
Okay, this definition may sound a bit vague. Let's consider a simple example to understand what it actually is.
Suppose you have an application that offers CRUD facilities for some user accounts. How are you going to manage the state of a user entity at any instant?
In a general scenario, to handle the user state, you will have a database that will be updated for each request that comes to the service.
Refer to the below diagram to understand the same.
After these 4 requests have been handled by the service, your database will contain the following information:
User 1, Neha Dua User 2, Prince User 3, Ashu
So, only the latest state of the business entity is available to us in this basic scenario, but the information of how we have reached to that state is lost.
This is where Event sourcing comes into the picture. The idea behind Event Sourcing is to ensure that every change to the state of an application is captured in the form of events and that these event objects are themselves stored in the sequence.
Introducing Event Sourcing in the service adds a step to the process of managing data persistence. Now the service creates an event object to record the change and then processes it to update the user state.
Look at the given diagram to observe what will happen when the Event Sourcing is added to the service.
If you would have noticed, not just the user state is maintained, there is also an event log that stores the changes happening in the state.
With the basic service, just the final state is captured in the database. However, with Event Sourcing, we also capture each event. We have a log of all the changes that have happened so far. This is the key benefit of having an event sourced application.
Event sourcing persists the state of a business entity such as a User, Order or a Customer as a sequence of state-changing events. Whenever the state of a business entity changes, a new event is appended to the list of events, and nothing is ever mutated.
Applications persist events in an event store or log, which is a database of events.
Advantages of Event Sourcing
- 100% audit logging: With event sourcing, each state change corresponds to one or more events, providing 100% accurate audit logging.
- Temporal Query: We can determine the application state at any point in time. Implementing temporal queries and reconstructing the historical state of an entity is straightforward. This is done by starting with a blank state and rerunning the events up to a particular time.
- Replay Events: In the case of application failure, we can reconstruct the state of the entity by replaying all the events, because event sourcing maintains the complete history of each business object.
Version Control System (VCS) is one of the applications which uses Event Sourcing.
If your service is running for years, there will be a large number of events in the datastore and replaying each of those events will take a lot of time.
In order to optimize the loading of a business entity, an application can periodically save a snapshot of an entity's current state.
And whenever we need to rebuild the current state, the application finds the most recent snapshot and the events that have occurred after that snapshot. As a result, there are fewer events to replay.
Snapshots are automatically saved after a configured number of persisted events.
So until now, we have discussed what event sourcing is, why it is required to manage data persistence and how it reduces the chances of data loss.
Published at DZone with permission of Divya Dua, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.