How To Understand Event Sourcing In Microservices Architecture
Omar Ismail
Senior Software Engineer @ Digitinary | Java 8 Certified? | Spring & Spring Boot?????? | AWS? | Microservices ?? | RESTFul Apis & Integrations ?? FinTech ?? | Open Banking ?? | Digital Payments and Transformation??
Event Sourcing
Event Sourcing Example
Let’s review an example.
It is a?CQRS?pattern (details can be found at?How To Understand CQRS In Microservices Architecture).
Imagine that we are only interested in movie rating functionality. A user leaves his opinion about the movie in a form of a rating (e.g. from 0 to 10). As a result:
The problem with this design is in the fact that only the latest rating state is saved. The whole history is gone.
Let’s review an?Event Sourcing?pattern.
It is almost the same as the previous diagram. A?CQRS?pattern is also used here.
At first glance, nothing changed. However, it brought much more flexibility to the system.
领英推荐
The problem with movie ratings lies in the fact that people have different tastes. The general rating value (e.g. 7.35) shows only an average mark that was calculated based on the opinions of multiple people. As a company that provides movie streaming features, we are interested in providing good recommendations to our users. How we can achieve this? In order to simplify the logic, we can do it with a single statement. Recommend those movies that have similar genres to the movies that were highly rated by this user.
As a result, a new service?Movie Recommendation Service?is introduced.
We are extending an existing system. Consequently,?Movie Recommendation Service?should process all previously assigned ratings. With?Event Sourcing,?it is possible because we keep this information in?Kafka?(?Add Rating?8 to movie?The Lord of The Rings). In the first example, it was not possible due to the fact that we only stored the last movie rating value.
Event Sourcing Advantages
Event Sourcing Disadvantages
Summary
In this post, the?Event Sourcing?pattern was reviewed. It s usually used alongside?CQRS. It is worth mentioning, that the necessity in this pattern can occur in complex systems. As a result, it should be chosen carefully in order not to bring unnecessary complexity to your system.