Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TWJUG-Lite: Introduction to Event Sourcing

Avatar for Eddie Cho Eddie Cho
November 12, 2019

TWJUG-Lite: Introduction to Event Sourcing

Avatar for Eddie Cho

Eddie Cho

November 12, 2019
Tweet

More Decks by Eddie Cho

Other Decks in Programming

Transcript

  1. Why I like to give this talk 1. Interesting 2.

    Issues that have to be overcome a. Fragmented study time b. The gap between concept and practices c. Found examples with unfamiliar languages/frameworks d. Stander implementation for Java + Spring + RDMBS Best Practices
  2. Agenda 1. Core Concepts of Event Sourcing 2. Event Sourcing

    in Practice 3. Event Sourcing + Microservice
  3. Core Concepts of Event Sourcing: Event Event: • An event

    is something that has happened in the past. • Conceptually, it should be Immutable. Event Sourcing is about: • Storing facts. • Reading information from the facts for different perceptions.
  4. Core Concepts of Event Sourcing CRUD Table of Data Model

    (e.g. BankAccount) Id Owner Amount 9527 Arthur Morgan 500 W hy?
  5. Core Concepts of Event Sourcing: Event Stream Reference: Event Sourcing

    in Practice - Benjamin Reitzammer & Johannes Seitz BankAccountCreated: {id: 9527, owner: Arthur Morgan} DepositPerformed: {accountId: 9527, amount: 1500} WithdrawalPerformed: {accountId: 9527, amount: 1000} DepositPerformed: {accountId: 9527, amount: 500}
  6. Core Concepts of Event Sourcing: Event Store Type Content Aggregate

    Version BankAccountCreated {id: 9527, owner: Arthur Morgan} 9527 1 DepositPerformed {accountId: 9527, amount: 1500} 9527 2 WithdrawalPerformed {accountId: 9527, amount: 1000} 9527 3
  7. Core Concepts of Event Sourcing: Projection Refernece: Event Sourcing in

    Practice - Benjamin Reitzammer & Johannes Seitz BankAccountCreated: {id: 9527, owner: Arthur Morgan} DepositPerformed: {accountId: 9527, amount: 1500} WithdrawalPerformed: {accountId: 9527, amount: 1000} BankAccount BankAccount {id: 9527, owner: Arthur Morgan} BankAccount {id: 9527, owner: Arthur Morgan amount: 1500} BankAccount {id: 9527, owner: Arthur Morgan amount: 500}
  8. Core Concepts of Event Sourcing: Projection BankAccountCreated: {id: 9527, owner:

    Arthur Morgan} DepositPerformed: {accountId: 9527, amount: 1500} WithdrawalPerformed: {accountId: 9527, amount: 1000} BankAccountReport BankAccountReport {id: 9527} BankAccountReport {id: 9527, { totoalDepositAmount: 1500, totalWithdrwalAmount: 0, depositTimesPerMonth: 1 ….} BankAccountReport {id: 9527, { totoalDepositAmount: 1500, totalWithdrwalAmount: 1000, depositTimesPerMonth: 1 ….}
  9. Core Concepts of Event Sourcing: Overview Refernece: Event Sourcing You

    are doing it wrong by David Schmitz Aggregate Events EventStream EventStore Projection Read Model
  10. Core Concepts of Event Sourcing: Overview Issues could be solved

    by Event Sourcing 1. No audit log issue, Information would not be lost, Single source of truth 2. Structure changes more often than behavior => different read model 3. Allows you to time travel (Historic states) 4. Easier to anylize and fix issues. 5. Data analysis
  11. Event Sourcing in Practice: Event Store Depends on your requirements,

    the event store could be... 1. eventstore.org (open source project) 2. NoSQL Database 3. Message Queue 4. RDBMS (?) 5. other options
  12. Event Sourcing in Practice: Event Store RDBMS Event Store Design

    1: OP lock with version Aggregate Table Event Table Reference https://cqrs.wordpress.com/documents/building-event-storage/ Column Name Column Type Id (PK) bigint Type varchar Version bigint CreateDate datetime Column Name Column Type Aggregste (FK) bigint Content blob(json) Version bigint CreateDate datetime
  13. Event Sourcing in Practice: Event Store RDBMS Event Store Design

    2: Event Table, ( id + parent should be unique) RDBMS Event Store Design 3: Use normal columns to replace context(blob) column Column Name Column Type Id (PK) bigint Content blob(json) Parent (FK) bigint CreateDate datetime
  14. Event Sourcing in Practice: Snapshot Event 2 Event 1 ...

    Event 50 Event 51 Event 52 Snapshot 1 ~ 50
  15. 1. Snapshots should not be places in line in the

    Event Log 2. Snapshots should have their own table 3. Memento pattern better insulates the domain over time as the structure of the domain objects change 4. Frequency of creating snapshots. <= Depends on your requirements Event Sourcing in Practice: Snapshot
  16. Event Sourcing in Practice: Snapshot RDBMS Snapshot Table Design Reference

    https://cqrs.wordpress.com/documents/building-event-storage/ Column Name Column Type Aggregste (FK) bigint content blob Version bigint CreateDate datetime
  17. Event Sourcing in Practice: CQRS CQS Command: void return type

    method, it is allowed to mutate state Query: non-void return type. it is not allowed to mutate state CQRS Command Component Query Component
  18. Event Sourcing in Practice: Sample Project Event Sourcing + Spring

    Boot + MySQL https://github.com/EddieChoCho/event-sourcing-account-management Event Sourcing + Spring Boot + In Memory Event Store https://github.com/Pragmatists/eventsourcing-java-example/tree/projections
  19. Event Sourcing + Microservice 1. Event-Driven Async Communication 2. CQRS

    3. Choreography-based Saga Pattern Caution! These patterns are fit to be implemented with event sourcing. But they also could be implemented isolatedly.
  20. Event Sourcing + Microservice: Async Communication Issue: communications with services

    through http request. 1. Tight coupling 2. Resilience Issue, Multiple points of failures. 3. Response time Order Service CoffeeBean Service CoffeeMaker Service Client
  21. Event Sourcing + Microservice: Async Communication Solution: Event-Driven + Asynchronous

    Communication + Server Push Eventual Consistency Order Service CoffeeBean Service CoffeeMaker Service Message Queue Order Topic CoffeeBeen Topic CoffeeMaker Topic Client Command Query
  22. Event Sourcing + Microservice: CQRS Reference: When Microservices Meet Event

    Sourcing Client Command Service Query Service Event Store Message queue Query DB (Read Models)
  23. Event Sourcing + Microservice: CQRS Reference: Command Query Responsibility Segregation

    (CQRS) Order Service Query Service CoffeeBean Service CoffeeMaker Service Message Queue Order Topic CoffeeBeen Topic CoffeeMaker Topic
  24. Event Sourcing + Microservice: Transaction Monolithic Service + RDBMS, ACID

    start transaction create order reserve credit commit
  25. Event Sourcing + Microservice: Transaction Distribute transaction, Two-phase commit protocol

    1. Synchronous communication 2. Resilience issue, Multiple points of failures. 3. Response time issue
  26. Event Sourcing + Microservice: Transaction Choreography-based saga pattern: local transactions

    + publish messages (events) Order Service CoffeeBean Service CoffeeMaker Service Message Queue T1 T2 T3 T4
  27. Event Sourcing + Microservice: Transaction Order Service CoffeeBean Service CoffeeMaker

    Service T1 C1 T2 Order Service CoffeeBean Service CoffeeMaker Service T1 C1 T2 T3 C2
  28. Microservice + Event Sourcing: Sample Project Sebastian Daschner: Video Course — Event

    Sourcing, Distributed Systems & CQRS Event Sourcing + Microserives + Message Queue (Kafka) https://github.com/EddieChoCho/scalable-coffee-shop/tree/master/coffee-shop-blueprint
  29. References Greg Young - CQRS and Event Sourcing The Many

    Meanings of Event-Driven Architecture • Martin Fowler Event Sourcing You are doing it wrong by David Schmitz Building an Event Storage - CQRS When Microservices Meet Event Sourcing Sebastian Daschner: Video Course — Event Sourcing, Distributed Systems & CQRS Event Sourcing in Practice - Benjamin Reitzammer & Johannes Seitz Microservice Patterns - Saga Pattern by Chris Richardson Greg Young — A Decade of DDD, CQRS, Event Sourcing
  30. Further Readings Microsoft: CQRS Journey Patterns & Practices Symposium Online

    2012 A Journey into CQRS CQRS Tutorial: http://www.cqrs.nu/tutorial/cs/01-design Leave feedbacks twjug slack: CCho twitter: @ChoCho008 Email: [email protected] To Be Continued