Introduction to Event Sourcing

Event Sourcing – Data Integrity

Ankit Verma
Latest posts by Ankit Verma (see all)

Before going directly onto the theme of the database, It is necessary to understand the functioning of the most standard applications. Whenever we run the command update in the database, we perform modification in the current representation in our database so what we are actually doing is the state change of the database from one state to another. It brings me to the concept known as STATE MUTATION.

STATE MUTATION

Suppose we have a table responsible for administrating the access level of a user the table is composed as follows.

IDNameStatusManger
1Amit SharmaAdminManager 1

Now we have update the database table

See also  SOUND – S for Secure Programming #2 | OWASP #2 | Broken Authentication
IDNameStatusManager
1Amit Sharmanormal_userManager 2

In this case manager 2 was responsible for the change in the status of the user. The current status of the user Amit Sharma is normal_user, but what was their previous state? Who was responsible for making Amit Sharma an admin for short period of time ?

The default behaviour adopted to record changes in the databases does not allow us to know the trajectory of the record with in the application – we always know only the current state of the registry. To apply to this kind of need, I want to get to know the entire history of a record that enters event sourcing.

Understanding Event Sourcing

The idea of event sourcing is a little different. Each change in the current state of your database would be a new event in the stream that only allows inclusion. Each update on the table would generate a new line, the change of status. If you have changed to an incorrect status, a new line would be generated by correcting this status.

So what we do create the status_user table.

IDNameStatususer_manager
1Amit SharmaadminManager 1
2Amit Sharmanormal_userManager 2

If a new status change will be done than a new record will be added in status_user table.

See also  Interesting Facts about the US Presidents

Event sourcing has positives and negatives. Among the positive points are the historical maintenance records, while the negatives include exponential growth in the database records editing operations. For this reason, it is very common to see event sourcing being used with CQRS precisely. (https://taagung.com/cqrs-intro/), not to encumber the searches in the database due to a large number of the same record.

References

https://axoniq.io/resources/event-sourcing

https://www.packtpub.com/product/microservice-patterns-and-best-practices/9781788474030