Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best event sourcing db strategy

I want to setup a small event sourcing lib. I read a few tutorials online, everything understood so far.

The only problem is, in these different tutorials, there are two different database strategies, but without any comments why they use the one they use.

So, I want to ask for your opinion. And important, why do you prefer the solution you choose.

  1. Solution is the db structure where you create one table for each event.

  2. Solution is the db structure where you create only one generic table, and save the events as serialized string to one column.

In both cases I'm not sure how they handle event changes, maybe they create a whole new one.

Kind regards

like image 789
SharpNoiZy Avatar asked Feb 23 '15 05:02

SharpNoiZy


People also ask

Which database is best for Event Sourcing?

implements such an event store and supports MongoDB and PostgreSQL as databases out of the box. PostgreSQL is the better and more powerful choice. If you take a look at the schema definition of the events table, you will notice that all events can be processed using a single schema: CREATE TABLE IF NOT EXISTS "${this.

What is Event Sourcing in database?

Event sourcing stores the state of a database object as a sequence of events – essentially a new event for each time the object changed state, from the beginning of the object's existence.

What is the Event Sourcing pattern?

Solution. The Event Sourcing pattern defines an approach to handling operations on data that's driven by a sequence of events, each of which is recorded in an append-only store.

What is the difference between event driven and Event Sourcing?

Event Sourcing is keeping a log for your own use so you don't forget. Event Driven Architecture is about communicating what happened to others. Typically, components in EDA can't recover the entirety of their state from the events they've published because not everything that changed their state is worth publishing.


2 Answers

I built my own event sourcing lib and I opted for option 2 and here's why.

  • You query the event stream by aggregate id not event type.
  • Reproducing the events in order would be a pain if they are all in different tables
  • It would make upgrading events a bit of pain

There is an argument to say you can store events on a per aggregate but that depends of the requirements of the project.

I do have some posts about how event streams are used that you may find helpful.

  • 6 Code Smells With Your CQRS Events and How to Avoid Them

  • Aggregate Root – How to Build One for CQRS and Event Sourcing

  • How to Upgrade CQRS Events Without Busting Your Event Stream

like image 138
Codescribler Avatar answered Nov 10 '22 22:11

Codescribler


Solution is the db structure where you create only one generic table, and save the events as serialized string to one column

This is by far the best approach as replaying events is simpler. Now my two cents on event sourcing: It is a great pattern, but you should be careful because not everything is as simple as it seems. In a system I was working on we saved the stream of events per aggregate but we still had a set of normalized tables, because we just could not accept that in order to get the latest state of an object we would have to run all the events (snapshots help but are not a perfect solution). So yes event sourcing is a fine pattern, it gives you a complete versioning of your entities and a full auditing log, and it should be used just for that, not as a replacement of a set of normalized tables, but this is just my two cents.

like image 37
MeTitus Avatar answered Nov 11 '22 00:11

MeTitus