DEV Community

ritaCosta93
ritaCosta93

Posted on

Event Sourcing

Event Sourcing

We get the current state, duplicate it, store it in a "separate storage" and then trigger the changing event to change the duplicate state.

So we keep both, that way we have a log of the event changes, we can check the older and the current state and see what the event actually changed.

The advantage of doing this, is that we can always rebuild the application, going back from the log.

Git is a good example of event sourcing, a version control, we can always go back to an older state of the application.

This approach has the advantage of create a snapshot of your overall application, where you can compile the state of the application into a "recovering point" where you can go back in case something goes wrong.

Advantages of the Event Sourcing

  • Audit
    • You can go back in the system and figure out why a certain event happened or why a certain data was generated
    • Debugging
      • You can go back and trigger events and check for state changes and look for a bug causes
    • Historic State
      • You can go back and look at the changes of your state and figure out when did something happened
    • Memory System
      • I don't need to store the state of all my application in a database, I can have it all in a memory system.
      • With this system, we can have the image in memory and don't need to access a database all the time, that makes the application way faster.
      • This system also alows you to have different systems using the same image, so if one goes down, the other will take over.
      • You can keep all the system in memory, in RAM instead of using a database, of course you have to build all the infrastructure for that, but still it fits in RAM.
      • We still need to have a log system to keep the states, that's what event sourcing is all about.

Disadvantages of the Event Sourcing

  • External systems
    • The communication with external systems is not easy, we still need a kind of replay system to replicate the app state when we call it from an external system
  • Event schema
    • We need an event schema to retrieve the events from the snapshot
  • Identifiers
    • Whenever I have identifiers or constant data, I need to get a way to go back, trigger and replay that date

Which events should we record in the event store

  • Text changes
  • Input events
  • Output events

What not to store

  • Don't have any business logic between the event and the storage

Discussion (0)