Top Banner
CQRS & EVENT SOURCING I - Event sourcing
30

CQRS & Event Sourcing

Apr 11, 2017

Download

Software

matthiasnoback
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: CQRS & Event Sourcing

CQRS& EVENT

SOURCINGI - Event sourcing

Page 2: CQRS & Event Sourcing

TRADITIONAL PERSISTENCEStore only the current state

Page 3: CQRS & Event Sourcing

TRADITIONAL PERSISTENCEStore only the current state

Page 4: CQRS & Event Sourcing

TRADITIONAL PERSISTENCESnapshots

Page 5: CQRS & Event Sourcing

EVENT SOURCINGStore the events that have led to every state

1. Created

2. Updated foo

3. Updated bar

4. Deleted

Page 6: CQRS & Event Sourcing

EVENT SOURCINGBenefits

Natural audit log

Ability to reconstruct a previous state

Ability to respond to bugs in state

Page 7: CQRS & Event Sourcing

COMMAND-QUERY SEPARATIONCommand function/method

Produces state change

Has observable side-effects

Either succeeds or fails

Has a "void" return type

Throws exceptions

Page 8: CQRS & Event Sourcing

COMMANDProduces a state change and emits an event

Store only the events

Reproduce the state using the events

Page 9: CQRS & Event Sourcing

RECONSTITUTIONAppend new events

event event event

event

command

Page 10: CQRS & Event Sourcing

EVENTS

Type (class)

Object identifier

Timestamp

Any relevant data

(Playhead)

Page 11: CQRS & Event Sourcing

assignments/01.mdEvent sourcing the `Meetup` aggregate

Page 12: CQRS & Event Sourcing

CQRS& EVENT

SOURCINGII - CQRS

Page 13: CQRS & Event Sourcing

EVENT SOURCINGSlow on the "read" side

We need to:

1. Fetch and recreate all event objects

2. Reconstitute the object based on its events

Page 14: CQRS & Event Sourcing

CQRSHelps with scaling on the read side

Page 15: CQRS & Event Sourcing

COMMAND-QUERY SEPARATIONQuery method

Has no observable side-effects

Either succeeds or fails

Has a specific return type

Throws exceptions

Page 16: CQRS & Event Sourcing

COMMAND/QUERY SEPARATION PRINCIPLE

(CQS)

Every method should be either a command method, or a query method

command(): void query(): type

Page 17: CQRS & Event Sourcing

COMMAND/QUERY RESPONSIBILITY SEGREGATION

(CQRS)

Domain objects should have either command methods, or query methods

query(): typecommand(): void

Page 18: CQRS & Event Sourcing

WE WANT TO PERSIST EVENTSBut we need the current state too

Page 19: CQRS & Event Sourcing

IF ONLY WE COULDTail the event log...

event event event

Page 20: CQRS & Event Sourcing

PROJECTIONSUsing events to update read models

event

Page 21: CQRS & Event Sourcing

READ MODELSAre query objects

query(): type query(): type query(): type

Page 22: CQRS & Event Sourcing

READ MODELSAre use-case specific

Page 23: CQRS & Event Sourcing

READ MODELSAre easy to query

SELECT * FROM ... WHERE ...

Page 24: CQRS & Event Sourcing

READ MODELSCan use polyglot persistence

Page 25: CQRS & Event Sourcing

assignments/02.mdApply CQRS to the `Meetup` aggregate

Page 26: CQRS & Event Sourcing

assignments/03.mdTwitsup: Unfollowing

Page 27: CQRS & Event Sourcing

assignments/04.mdTwitsup: Mentions

Page 28: CQRS & Event Sourcing

assignments/05.mdTwitsup: Mentions

Page 29: CQRS & Event Sourcing

CQRS& EVENT

SOURCINGIII - Potential

Page 30: CQRS & Event Sourcing

CQRS & ESVery useful for

Business insights

Scaling

Application integration

Microservices