Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Microservice Patterns and Best Practices

You're reading from   Microservice Patterns and Best Practices Explore patterns like CQRS and event sourcing to create scalable, maintainable, and testable microservices

Arrow left icon
Product type Paperback
Published in Jan 2018
Publisher Packt
ISBN-13 9781788474030
Length 366 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Vinicius Feitosa Pacheco Vinicius Feitosa Pacheco
Author Profile Icon Vinicius Feitosa Pacheco
Vinicius Feitosa Pacheco
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Title Page
Dedication
Packt Upsell
Contributors
Preface
1. Understanding the Microservices Concepts 2. The Microservice Tools FREE CHAPTER 3. Internal Patterns 4. Microservice Ecosystem 5. Shared Data Microservice Design Pattern 6. Aggregator Microservice Design Pattern 7. Proxy Microservice Design Pattern 8. Chained Microservice Design Pattern 9. Branch Microservice Design Pattern 10. Asynchronous Messaging Microservice 11. Microservices Working Together 12. Testing Microservices 13. Monitoring Security and Deployment 1. Other Books You May Enjoy Index

Event sourcing – data integrity


Before going directly onto the theme of event sourcing, it is necessary to understand the functioning of most standard applications.

Whenever we run the command UPDATE in the database, we perform a modification to our current representation in the database. With that, whenever we do a query on the database, we seek the current state of a record. This behavior is called state mutation. Let's use an example to make it clearer.

State mutation

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

ID

user_name

status

user_manager

1

John Doe

admin

Manager1

 

As we look at the preceding table, we see that the user John Doe is an administrator and this status was applied by Manager1. After a period of time, it was found that John Doe could never have been the admin of the application and an UPDATE is performed on this row in the table:

UPDATE status_user set status='normal_user', user_manager='Manage2' WHERE ID=1; 

This change results in some modifications in line with ID = 1:

ID

user_name

status

user_manager

1

John Doe

normal_user

Manager2

 

In this case, Manager2 was responsible for the change in the status of the user. The current status of the user John Doe is normal_user, but what was the previous status? Who was responsible for making John Doe an application administrator for some period of time?

The default behavior adopted to record changes in the database does not allow us to know the trajectory of a record within the application—we always know only the current state of the registry. To supply 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 a 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, we'd have all the changes until you reach the current time of permission for the user.

From the beginning, in the status_user table using the concept of event sourcing, the records would be as follows:

ID

user_name

status

user_manager

1

John Doe

admin

Manager1

1

John Doe

normal_user

Manager2

 

If a new status change is applied to the user John Doe, a new line would be generated by keeping track of the changes. Event sourcing uses the append only model for database records.

Event sourcing has positives and negatives. Among the positive points are the historical maintenance records, while 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, not to encumber the searches in the database due to a large number of the same record.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime
Visually different images