DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. Introduction to Event Sourcing

Introduction to Event Sourcing

This article is a good start to gain basic knowledge about Event Sourcing and CQRS.

Divya Dua user avatar by
Divya Dua
·
Jul. 09, 18 · Analysis
Like (7)
Save
Tweet
Share
12.04K Views

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

  1. 100% audit logging: With event sourcing, each state change corresponds to one or more events, providing 100% accurate audit logging.
  2. 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.
  3. 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.

Snapshots

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.

In the next article, we will understand what CQRS is.

Event Database application

Published at DZone with permission of Divya Dua, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Ultimate Guide to FaceIO
  • The Importance of Delegation in Management Teams
  • The Role of Data Governance in Data Strategy: Part II
  • Enabling DB Migrations Using Kubernetes Init

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: