Top Banner
A visual introduction to Event Sourcing and CQRS 1 @nicusX https://opencredo.com/author/lorenzo/ Lorenzo Nicora Senior Consultant @ OpenCredo
41

A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

Apr 16, 2017

Download

Technology

opencredo
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

A visual introduction to

Event Sourcing and CQRS

1

@nicusX https://opencredo.com/author/lorenzo/

Lorenzo Nicora Senior Consultant @ OpenCredo

Page 2: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

2

A couple of concepts from DDD

Aggregate

(Current) State of the Aggregate

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 3: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

3

Once upon a time…

Everything is synchronous

Request - ResponseLorenzo Nicora Intro to Event Sourcing and CQRS

Page 4: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

4

Scaling up…

Updates —> Locks —> Contention!<— Block <—

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 5: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

5

Let’s go Asynchronous

Pwd —> “secret” Pwd —> “12345” Pwd —> “54321”

Pwd —> “secret” Pwd —> “54321” Pwd —> “12345”

===> Out of Order

Asynchronous, Message-drivenRequest/Response ACID Transaction

Distributed, Message-based —> No order guaranteed

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 6: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

6

Command Sourcing💡

• Append Only —> No Contention • Build State from Command history

Write fast, Think later

K/V Store Distributed

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 7: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

Command “Submit Order!” —> A request (imperative sentence) —> May fail —> May affect multiple Aggregates

7

Commands vs Events

Rebuild Aggregate State from Commands

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 8: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

8

Event “Order submitted” —> Statement of facts (past tense) —> Never fails —> May affect a single Aggregate

Rebuild Aggregate State from Events

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 9: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

9

Commands to Events

(DDD patterns: Aggregate / Process Manager)

X

Y

Z

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 10: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

10

Command > Event Sourcing💡

Think a little, Write, Think later

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 11: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

11

Event Sourcing

In many domains Commands ≃ Events

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 12: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

12

Additional Benefits

EasyEventual Business Consistency

—> Corrective Events

Robust to data corruption (bugs, fat fingers…)

—> Rebuild state ignoring wrong events

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 13: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

13

Additional Benefits

History (for free)

Rebuild State at a point in Time

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 14: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

14

Main Benefit

Scalable —> Append only

-> Fits distributed k/v stores

—> Low-latency writes

—> Allows asynchronous processing

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 15: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

What about reads?

15Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 16: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

16

Retrieving the State

How do I retrieve the State? “Get details of Order ‘AB123’”

not very efficient, but… …may work

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 17: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

17

Querying (Searching) the State

❓How do query the State?

“Get all Orders delivered to ‘SE1 0NZ’”

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 18: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

18

CQRS

Command Query Responsibility Segregation

💡Separate • Code • muService • Datastore

-> Update -—> Retrieve “Query”

datastore is downstream

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 19: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

19

Not a new idea

Specialised Downstream

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 20: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

20

CQRS and Event Sourcing

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 21: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

21

Materialised Views (of current State)

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 22: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

22

Materialised Views (of State)

Latest (known) State

(Persistent)Rebuildable from Events

In MemoryK/V Store Graph

… RDBMS

Delayed

💡

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 23: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

23

Materialised View of State

Query a RDBMS?!?

Wasn’t it the old way?

RDBMS is just one of our options: easy to use, easily become a bottleneck

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 24: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

24

Materialised Views of State

* Views are optimised for specific query use cases

—> multiple Views from same Events

* Updated asynchronously, delayed —> to Scale -> may reorder Events

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 25: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

25

Materialised Views of State

* Views can be rebuilt from Events

Event Log is our Source of Truth

* Easy to evolve or fix

—> change or fix logic; rebuild view from events

(not the View)

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 26: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

26

Indexes

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 27: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

27

Indexes

Search EnginesK/V Stores

• Optimised for querying (less for retrieving) • Latest State; rebuild on the fly

💡

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 28: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

28

Hybrid solutions

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 29: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

29

Hybrid solutions: e.g. Snapshots

• Speed up rebuilding the current State • Use recent Events to rebuild up-to-date

💡Long delayed

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 30: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

30

Eventual (Business) Consistency

Guess —> Compensate —> Apologies

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 31: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

31

Eventual Consistency: Sagas

StatefulOut of band

Corrective Command or Event

Saga

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 32: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

32

Lessonfrom the Trenches

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 33: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

33

Lesson Learned #1

If you put data in…

…you will eventuallyhave to get them out!

The “Query” side is not secondary

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 34: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

34

Lessons Learned #2In old days:

normalising one DB to support as many queries as possible

With CQRS: multiple denormalised “data stores”

optimised for different queries

No single “Q” implementation for all your queries

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 35: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

35

Lessons Learned #3

A central, shared Event Storemay not be the best option

No Event-sourced Monolith

Prefer persistence per Bounded-Context

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 36: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

+++ Summing up +++

36Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 37: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

37

ES/CQRS Optimal Use Cases

High Volume Low Latency writes

(big data)

Event Sourcing + CQRS

😋

Out-of-order Commands/Events (IoT)

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 38: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

38

ES/CQRS Drawbacks

x No “One-Size-Fits-All”

—> Multiple “Q” implementations

x Delayed reads

x No ACID Transactions

x Additional complexity (!)

🙁

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 39: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

39

ES/CQRS Benefits

+ No “One-Size-Fits-All” —> “Q” are optimised for use cases

+ Eventual (Business) Consistency

+ History, Temporal queries

+ Robust to data corruption

😀

Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 40: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

That’s all, Folks!

40Lorenzo Nicora Intro to Event Sourcing and CQRS

Page 41: A Visual Introduction to Event Sourcing and CQRS by Lorenzo Nicora

??? Questions ???

41

Thanks.

Lorenzo Nicora Intro to Event Sourcing and CQRS