DEV Community

Discussion on: Event Sourcing: What it is and why it's awesome

Collapse
 
mbreveglieri profile image
Marco Breveglieri

I find Event Sourcing really interesting, but there are a lot of questions that come into my mind as a newbie in this world. :)

Using this approach requires you (the developer) to model your database using tables to store "events" instead of "entities". Suppose you want to maintain a long list of customer data and retrieve the full list of customers, is it not too slow scanning the entire event table to rebuild the current status of all the available customers?

I know you can do snapshots, but does this approach require you to take snapshots too much often in order to keep everything fast?

Collapse
 
barryosull profile image
Barry O Sullivan

Hey Marco,

Glad to answer. In Event Sourcing, you wouldn't have tables per model, you would have one table that stores all the events for your system, this would is called the event log.

If you're using mysql, you'd typically encode the event itself as JSON and just store. You'd also have columns for storing the aggregate (root entity) and the aggregate id (root entity id), so you can easily fetch the subset you need when validating business operations.

Now, this is not optimal for your proposed usecase "retrieve the full list of customers". This is where projections come in. You would create a read model (projection) that is built from all the "customer" events. Everytime a "customer" event is fired, this read model is listening and updates itself.

Hope that answers your question.

Collapse
 
brightide profile image
Jarred Filmer

Would I be right in assuming that if you want to do a query on anything that isn't the aggregate id you'd have to project the entire set for the type in question first?

Thread Thread
 
barryosull profile image
Barry O Sullivan

That's exactly how you'd do it. There are lots of strategies for this, it depends on your usecase.

Collapse
 
mbreveglieri profile image
Marco Breveglieri

Thanks for your really clear answers, Barry.

It seems that for any "trouble" you might encounter applying an ES-based model, there is a workaround to balance disadvantages with benefits.

I will deepen the subject to know more, since the greater obstacle (for me) is more a matter of "mind shifting" than technical shortage. :)

Thanks again!

Collapse
 
stephaneeybert profile image
Stephane Eybert

So the aggregate is not the data in the projection ? What is this aggregate and its aggregate id then ?