DEV Community

ritaCosta93
ritaCosta93

Posted on

What is Event Sourcing and CQRS?

Event Sourcing

  • Store the state changes using events
  • Store event log that is persisted in the order that it appears
  • By reading the event log we can re-create the state of our application
  • We can get the current state of the state anytime, just by reading the event log

Advantages

  • Complete Rebuild
    • We can clear the state any time we want
  • Temporal Query
    • We can see how was the state before
  • Debugging and Data duplication
    • We can replay the event log somewhere else
  • Snapshot
    • Create a snapshot of the current state
    • Continue adding more events from there

When to use it

  • When you need to have an audit log
  • Paralell processing, we can apply the information in different systems
  • When you have more readers than writer
  • The event log is immutable
  • You can read from the event log an re-create another state based on it's in the event log

Eventual Consistency

  • Not all the services will read at the same time from the event log
  • There will be a delay between readings

CQRS - Command Query of Responsability Segregation pattern

  • The way you write the data is different from the way that you retrieve it
  • We will have the same database, but we will do different kind of requests (CRUD)
  • When you have more reads or more writes, you can split the responsability
  • The writting and reading views are different
  • The CQRS manages how you write and how you query the data

Benefits

  • Split the reads and the writes in different systems
  • The server are independent
  • Different logic by server
  • Works very well with event driven architecture and event sources
  • Used for balance the system
  • When the latency between reading and writting is a problem

Cons

  • Too complex to use in a CRUD opperation

Discussion (0)