Select Page
“Event sourcing” a briljant way to “go back to the future”

“Event sourcing” a briljant way to “go back to the future”

What is event sourcing?

Instead of storing just the current state of the data in a domain, use an append-only store to record the full series of actions taken on that data. The store acts as the system of record and can be used to materialize the domain objects. This can simplify tasks in complex domains, by avoiding the need to synchronize the data model and the business domain, while improving performance, scalability, and responsiveness. It can also give consistency for transactional data, and support full audit trails and history that can enable compensating actions. “Source: Microsoft docs”

Difficult words, isn’t it ? So how do i understand that as a junior. Event sourcing is recording the state from an object, the state in the universe as u can understand. So what can you do with that recording, its like what happened is happened, it’s recorded and can be found back in the history. A blockchain in cryptocurrency is an example ( Read my next post about cryptocurrency). Actually every accountancy is an example, also a shopingcart (digital) like showed in the image bellow.

u see what happened? I’ll explain, if you have a shopping cart see it as a stream. So we are gonna add some events on the stream. Like “Added 2 socks” this is an event that will be added to the stream. On that stream u will have consumers, consumers that are gonna read every event on the stream. If the event does have an effect on his job or task, then it will trigger a handler. Like the stock manager knows now that there are going to be 2 socks sold. Idem about the next event that has “Added 4 shirts” now he know’s that there are 4 shirts to be sold. The next event will remove 2 socks that you added earlier, you don’t delete the event cause the event has happened. You just counter it, the next event is “shipping information added” here will the shipping manager know where to send the bought stuff to. Like i told earlier is that you will have consumers. Let’s talk a bit more about that.

Consumers are reading the stream. What can an example be like here above u have like the stock manager and the shipping manager. Every consumer has a handler on a specific event. Like the stock manager only want’s to know what happens with his stock and he doesn’t care about the “shipping info”, the shipping manager doesn’t care what the customer does with the cart but he wants to know where to send it to and what to send.

Lets use an example in coding. So you make a producer and a consumer, the producer will add events on an eventstream and a consumer will read it. Don’t you get it ? Let me show it.

As you can see there are consumers and producers. What i didn’t add to this picture is the possibility that a consumer also can be a producer. Cause everyone that want’s to write an event on the stream can do that as a producer.

event sourcing is mostly used in CQRS desing pattern.

Why can it be handy?

Let’s take the shopping cart example back. If a costumer leaves his shopping cart but did not finish it. You can recover he’s cart the next moment that he visits the shop again. Another example if you work in an accountancy you can follow perfectly what happened and react to any mistakes that his happened along the stream. Another example, what if your database drops or you make a huge mistake and your back-ups are gone? Then can event sourcing be your solution, cause u can replay every message on your streams from the begin of the stream to the end of your stream and it will trigger all your actions again, so it will rebuild your database again. You can debug in time, you even can go back in time of the event stream of course. You can query over time and see events or replaying them, it can make you a time manager.

Why can it be a pain in the ass ?

It’s not easy to implement and control, you need to change your mind of thinking to the right solution. You don’t have 1 big flow. consistency is hard to hold.


What are great event sourcing tools?


Apache Kafka

event hubs

AggregateSource (on github)