Join us
@selcukkose ă» Feb 07,2022 ă» 2 min read ă» 881 views ă» Originally posted on medium.com
In this article I will explain Event Sourcing Pattern which is a widely used pattern for microservice artitechtured applications nowadays. In otherside I think it will change your look to development and give you a new vision. I will endeavor to explain this topic to you in two steps.
What is the problem?đ€š
Every pattern have born from a problem. Actually we use patterns because they are accepted solutions for specific problems and they will help us not to stuck on same problems again when we encounter. Then letâs try to understand what is the problem behind the Event Sourcing Pattern.
Letâs think about you are developing a food ordering app that customers order food from restaurants and rasturants prepare these orders in the kitchen and deliver to the customers.
How you would handle orders? You need to deliver these orders from customer to the restaruantâs kitchen and cookers will see these orders eventually and start making them. On the other hand orders may not stay same like when they first made, customers might add new food or might change ingrediants.
When we look from microservice perspective you would create a OrderService and a RestaurantService. You would create the order in the OrderService and send it to the RestaurantService in some way for example by using event queue. The best way handling changes on the orders would be using events, for example when order created OrderCreated event will be created and architecture would look like below.
Ok, now think that you are creating RestaurantService and you consume events from the OrderService via event queue and you wonder how to keep that data? In the traditional way you would directly create a table in the database and insert an record for each order, right?
But what about previous changes on the order? Because you are saving just current state of the order so you donât know what was the previous state of the order. You can say what I would do with the previous state? Think about this, while you are doing some operation on the order some thing went wrong on the RestaurantService or another service then you need to drawback the order to the previous state. What you would do?
On the other hand you would have concurrency problem because you just updating one record and the order of multiple commands can affect the result of the recordâs data.
Also you need to log the change to see what is happing. I know you can create an log table and write your logs to the table but actually there is better solution with Event Sourcing.
Ok, then letâs see the solution.
What is the solution?đ€
What if I say you donât have to always keep one record for an order in the table? Well I know you need to have an order object to deal with it but actually you donât need keep directly.
Think about what do you get when you consume the event queue? You get events right? Then what about you keep then directly not to keep as single object in the database. So you would have event stack not just an item so you can create your Order object by applying these events to the Order object in the constructor then you will get the Order object in the code.
In the programming perspective you need to write code like below when ever you want to get the Order object.
Isnât this make some performance issues? Because whenever I want to get the Order object I might need to apply too much events to Order object. Of course there is solution for this. You can take snapshots of the events at sometimes then you will not apply the events before than this snapshot.
I hope this article help you to understand this consept and thank you for reading.đ
Join other developers and claim your FAUN account now!
Influence
Total Hits
Posts
Only registered users can post comments. Please, login or signup.