cqrs & event sourcing
TRANSCRIPT
CQRS& EVENT
SOURCINGI - Event sourcing
TRADITIONAL PERSISTENCEStore only the current state
TRADITIONAL PERSISTENCEStore only the current state
TRADITIONAL PERSISTENCESnapshots
EVENT SOURCINGStore the events that have led to every state
1. Created
2. Updated foo
3. Updated bar
4. Deleted
EVENT SOURCINGBenefits
Natural audit log
Ability to reconstruct a previous state
Ability to respond to bugs in state
COMMAND-QUERY SEPARATIONCommand function/method
Produces state change
Has observable side-effects
Either succeeds or fails
Has a "void" return type
Throws exceptions
COMMANDProduces a state change and emits an event
Store only the events
Reproduce the state using the events
RECONSTITUTIONAppend new events
event event event
event
command
EVENTS
Type (class)
Object identifier
Timestamp
Any relevant data
(Playhead)
assignments/01.mdEvent sourcing the `Meetup` aggregate
CQRS& EVENT
SOURCINGII - CQRS
EVENT SOURCINGSlow on the "read" side
We need to:
1. Fetch and recreate all event objects
2. Reconstitute the object based on its events
CQRSHelps with scaling on the read side
COMMAND-QUERY SEPARATIONQuery method
Has no observable side-effects
Either succeeds or fails
Has a specific return type
Throws exceptions
COMMAND/QUERY SEPARATION PRINCIPLE
(CQS)
Every method should be either a command method, or a query method
command(): void query(): type
COMMAND/QUERY RESPONSIBILITY SEGREGATION
(CQRS)
Domain objects should have either command methods, or query methods
query(): typecommand(): void
WE WANT TO PERSIST EVENTSBut we need the current state too
IF ONLY WE COULDTail the event log...
event event event
PROJECTIONSUsing events to update read models
event
READ MODELSAre query objects
query(): type query(): type query(): type
READ MODELSAre use-case specific
READ MODELSAre easy to query
SELECT * FROM ... WHERE ...
READ MODELSCan use polyglot persistence
assignments/02.mdApply CQRS to the `Meetup` aggregate
assignments/03.mdTwitsup: Unfollowing
assignments/04.mdTwitsup: Mentions
assignments/05.mdTwitsup: Mentions
CQRS& EVENT
SOURCINGIII - Potential
CQRS & ESVery useful for
Business insights
Scaling
Application integration
Microservices