Microservices

Event Sourcing and CQRS

Chandan Jhunjhunwal

Tech Architect @ Faodail Technology

https://github.com/indyarocks

       @ChandanJ

What is Event-Store?

  • It is a very old concept. Many domains in real world works like that. For example: Ledger, Banking, Databases
  • Only Write and Read. No Update/Delete like CRUD.
  • Choreography vs Orchestration

Why Event-Store?

 

  • We can go to any state of an object/aggregate for any moment in time by replaying events.
  • No coupling between current state in the domain and in storage.
  • Read side optimised for fast queries and write side highly optimised for writes .
  • Fool proof Audit Log.
  • No impedance mismatch between object oriented and relational world like RDBMS.
  • Easier to undo things.

When to Event-Source?

  • Accountability/debugability is critical
  • you're building rich object-oriented domain models (e.g. DDD)
  • you have a lot of behaviour that isn't CRUD
  • you need version control/undo for data (e. G. Wikis, Google Docs)
  • your business derives value or competitive advantage from event data
  • your domain is inherently event driven (e. G. basketball game tracking)
  • you're building a scaleable system based on the CQRS pattern

What is CQRS?

Every method should either be a command that performs an action, or a query that returns data to the caller, but not both. In other words, asking a question should not change the answer.

Bertrand Meyer

  • CQRS stands for Command and Query Responsibility Segregation
  • CQRS is architectural style.
IMAGE FROM MARTIN FOWLER BLOG

Standard Application DB Design

IMAGE FROM MARTIN FOWLER BLOG

CQRS

Event-Sourcing and CQRS

 

You can use CQRS without Event Sourcing, but with Event Sourcing you must use CQRS.

Greg Young

How?

Technology Set

  • Event Store
    - Open Source database for Event Sourcing (geteventstore.com )
  • rails_event_store gem

 

Domain Events

IMAGE FROM MARTIN FOWLER BLOG
IMAGE FROM MARTIN FOWLER BLOG

Normal Flow

IMAGE FROM MARTIN FOWLER BLOG

With Event-Sourcing

IMAGE FROM MARTIN FOWLER BLOG

Data with Event-Sourcing

IMAGE FROM MARTIN FOWLER BLOG

Domain Event Design

 

  • Greg Young's Talk: https://goo.gl/WCvPAf
  • Open-source, functional database: https://geteventstore.com/
  • Rails Event Store: http://goo.gl/uwyu1C
  • Rails Event Store Gem: https://github.com/arkency/rails_event_store
  • Martin Fowler's Blog: 
    - CQRS (http://martinfowler.com/bliki/CQRS.html)
    - Event Sourcing (http://www.martinfowler.com/eaaDev/EventSourcing.html
    )
    - Domain Events (
    http://www.martinfowler.com/eaaDev/DomainEvent.html)

REFERENCES

THANK YOU!

Event-Sourcing and CQRS Introduction

By Chandan Jhunjhunwal

Event-Sourcing and CQRS Introduction

A detailed comparison between Monolith architecture and Microservices. The factors for choosing one architecture over other. The key concepts in Microservice architecture.

  • 1,320