Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Booster profile imageJ2RGEZ
J2RGEZ forBooster

Posted on

     

Event-sourcing and the event-replay mystery

It’s been a while since we first started our way into event-sourcing and, from our experience, we really think that there is still space to dig into what event sourcing is and why it’s useful for modern backend applications. But first...

 What’s event-sourcing?

Event-sourcing is a collection of patterns that introduces a new way of thinking, where domain events are the source of truth (domain referring to domain-driven design). We need to fulfill the following contract:

  1. The event store persists thefull history of domain events. For example:

    UserRegistered
    UserChangedPassword
    UserPublishedAnArticle

  2. Domain events must bechronologically ordered

  3. Domain events areimmutable

Given #1 and #2, we can actuallyreplay domain events. That is, reconstructing the state of your application by running the full history of domain events, which can give you the chance to reconstruct the current state of your system and go back to a specific point in time, allowing the following:

  • Easily findbugs from the system and reconstruct those bugs in different environments.
  • Deploy different versions of your backend for testing purposes.
  • Recover from errors by replaying events from a specific point in your system.

Specifically for microservice architectures,there is one case when this comes in handy: Imagine adding a new service, meaning a new read model (local DB) needs to be created. For this purpose, we need to reconstruct the current state of domain models by replaying domain events from the Event Storage. This operation becomes easier when adding snapshotting to the formula.

Snapshotting for event replaying

Snapshots represent the current state of an entity (oraggregate) at a specific time. Snapshots can be configured to be done whenever you want. For example, every X events, X days, or when something special happens in your system.

Snapshotting

Replaying events then becomes easier, because you can start rebuilding your service from a specific point of your events’ timeline, and then run all further events from that point. Check the example below.

Alt Text

As you can see, you don’t have to go back to the firstUserRegistered event and run all events, but instead, you just take advantage of snapshotting and rollback the User state just before deleting the article.

Not everything is shiny

Definitely, event sourcing advantages were the ones that motivated us to enter this field, but along the way, we found that things weren’t as easy as the theory says:

  • Withhigh traffic, what would happen if your application goes down? How are you going to recover from the previous failed events plus keep receiving the new ones?
  • How do you handleside effects? For example, when replaying events imagine that one of the events calls your email service. In that case, we should check that the email was previously sent, or otherwise while we replay the events, the user could end up with a lot of repeated emails.
  • When should you store a snapshot? Snapshotting depends a lot on the business needs, so we should be open to leave this as configurable as possible, or just generalize it and snapshot every X events.
  • Is it better tostore snapshots synchronously or asynchronously? Synchronous snapshot persistence could happen if you’re not worried about persistence latency.
  • How do you handlenew events coming while replaying events?
  • How are you going to manageGDPR? As events must be immutable, theoretically you can’t delete events even if they contain personal data.

These problems can be probably solved by adding more control parameters to your events, adding additional checks (to avoid side effects for example), or having a high available queue system like Kafka/RabbitMQ in the middle of your store and your database (for events queuing and system recovery).

Although, what we’ve missed these years is an event-sourcing standard implementation, where the developer doesn’t have to think about the big complexity it has to develop the majority of useful use cases. That’s what we’ve been trying to do withBooster Framework, and we are really excited about what we’ve currently achieved.

Feel free to join ourdiscord orslack if you want to know more about it!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Want to contribute?

More fromBooster

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp