event sourcing · event sourcing lars vonk & bob forma in production systems tuesday, april 9,...
TRANSCRIPT
Event sourcingLars vonk & Bob Forma
in production systems
Tuesday, April 9, 13
Let’s take a look how wecurrently develop software
Tuesday, April 9, 13
Tuesday, April 9, 13
Tuesday, April 9, 13
One model to rule them all
Repor&ng
Mix of concerns
QueryingTransac&ons (commands)
Tuesday, April 9, 13
Tuesday, April 9, 13
How did this Invoice get to status :sent?
Tuesday, April 9, 13
How many items are deleted from the shopping cart just before an order is placed?
How did this Invoice get to status :sent?
Tuesday, April 9, 13
Information is lost
How many items are deleted from the shopping cart just before an order is placed?
How did this Invoice get to status :sent?
Tuesday, April 9, 13
Tuesday, April 9, 13
Tuesday, April 9, 13
Event SourcingCapture all changes to an application state as a sequence of events
martinfowler.com/eeaDev/EventSourcing.html - Martin Fowler
Tuesday, April 9, 13
Or maybe not so different...
Tuesday, April 9, 13
Trivial exampleCurrent state
id amount status recipiented3456f 121 sent [email protected]
Tuesday, April 9, 13
Trivial exampleCurrent state
id amount status recipiented3456f 121 sent [email protected]
Event sourced
id event_type event_jsoned3456f InvoiceCreatedEvent {id:“ed3456f”}ed3456f LineItemAddedEvent {id:“ed3456f”, line_item: {id: 1, amount: 100, vat: 21}}ed3456f InvoiceSentEvent {id:“ed3456f”, to: “[email protected]”}
Tuesday, April 9, 13
Event sourced
id event_type event_jsoned3456f InvoiceCreatedEvent {id:“ed3456f”}ed3456f LineItemAddedEvent {id:“ed3456f”, line_item: {id: 1, amount: 100, vat: 21}}ed3456f LineItemRemovedEvent {id:“ed3456f”, line_item_id: 1}ed3456f LineItemAddedEvent {id:“ed3456f”, line_item: {id: 2, amount: 150, vat: 21}}ed3456f InvoiceSentEvent {id:“ed3456f”, to: “[email protected]”}
How many line items are deleted just before...
Tuesday, April 9, 13
So how do you query stuff?
The One Model To Rule Them All
CommandModel
Query Model
View
write
read
Tuesday, April 9, 13
So how do you query stuff?
CommandModel
Query Model
View
Tuesday, April 9, 13
So how do you query stuff?
CommandModel
Query Model
View
writere
ad
Tuesday, April 9, 13
So how do you query stuff?
CommandModel
Query Model
View
writere
ad
CQRS
Tuesday, April 9, 13
So how do you query stuff?
CommandModel
Query Model
View
write
‘generates using events’
read
CQRS
Tuesday, April 9, 13
Concrete example...
Tuesday, April 9, 13
User
Concrete example...
Tuesday, April 9, 13
User
POST /invoices/:id/send
Controller
Concrete example...
Tuesday, April 9, 13
User
POST /invoices/:id/send
Controller
SendInvoiceCommand
CommandHandler
Concrete example...
Tuesday, April 9, 13
User
POST /invoices/:id/send
Controller
SendInvoiceCommand
CommandHandler load_aggregate
Eventstore
Concrete example...
Tuesday, April 9, 13
User
POST /invoices/:id/send
Controller
SendInvoiceCommand
CommandHandler load_aggregate
Invoice
InvoiceCreatedEvent
LineItemAddedEventEventstore
load_from_history
Concrete example...
events are in past tense
Tuesday, April 9, 13
User
POST /invoices/:id/send
Controller
SendInvoiceCommand
CommandHandler load_aggregate
Invoice
InvoiceCreatedEvent
LineItemAddedEvent
send
Eventstore
load_from_history
InvoiceSentEvent
Concrete example...
events are in past tense
Tuesday, April 9, 13
User
POST /invoices/:id/send
Controller
SendInvoiceCommand
CommandHandler load_aggregate
Invoice
InvoiceCreatedEvent
LineItemAddedEvent
send
Eventstore
store
load_from_history
InvoiceSentEvent
Concrete example...
events are in past tense
Tuesday, April 9, 13
User
POST /invoices/:id/send
Controller
SendInvoiceCommand
CommandHandler load_aggregate
Invoice
InvoiceCreatedEvent
LineItemAddedEvent
send
Eventstore
store
load_from_history
publish_eventsEventHandler
InvoiceSentEvent
Concrete example...
events are in past tense
Tuesday, April 9, 13
User
POST /invoices/:id/send
Controller
SendInvoiceCommand
CommandHandler load_aggregate
Invoice
InvoiceCreatedEvent
LineItemAddedEvent
send
Eventstore
store
load_from_history
publish_eventsEventHandlerView
update
InvoiceSentEvent
Concrete example...
events are in past tense
Tuesday, April 9, 13
Part 1: For the business
Why Event Sourcing
Tuesday, April 9, 13
To capture user intent
Part 1: For the business
Why Event Sourcing
Tuesday, April 9, 13
To capture user intentHistoric informa&on
Part 1: For the business
Why Event Sourcing
Tuesday, April 9, 13
To capture user intentHistoric informa&onStorage decoupling
Part 1: For the business
Why Event Sourcing
Tuesday, April 9, 13
To capture user intentHistoric informa&onStorage decoupling
Part 1: For the business
Why Event Sourcing
Defer decisions
Tuesday, April 9, 13
Part 2: For us nerds
Why Event Sourcing
Tuesday, April 9, 13
Tell, don’t ask
Part 2: For us nerds
Why Event Sourcing
Tuesday, April 9, 13
Tell, don’t askIsolated domain tests
Part 2: For us nerds
Why Event Sourcing
Tuesday, April 9, 13
Tell, don’t askIsolated domain tests
Part 2: For us nerds
Why Event Sourcing
Tuesday, April 9, 13
Debug informa&on
Tell, don’t askIsolated domain tests
Part 2: For us nerds
Why Event Sourcing
Tuesday, April 9, 13
Debug informa&on
Tell, don’t askIsolated domain tests
Part 2: For us nerds
Conflict resolu&on
Why Event Sourcing
Tuesday, April 9, 13
It is not a Silver bullet
Tuesday, April 9, 13
Not for CRUD
It is not a Silver bullet
Tuesday, April 9, 13
Learning curve
Not for CRUD
It is not a Silver bullet
Tuesday, April 9, 13
Learning curve
Not for CRUD
More work to do simple stuff
It is not a Silver bullet
Tuesday, April 9, 13
Financial administra&on systemIntegrates with various systemsWeb-‐based
Case study Freemle.com
Event source approved
Tuesday, April 9, 13
THE INTERNALS
Tuesday, April 9, 13
Stack
Tuesday, April 9, 13
Domain
Invoice
Organization
ExpenseCustomer
Bank
Invoice numbers
Ledger
Tuesday, April 9, 13
User
POST /invoices/:id/send
Controller
SendInvoiceCommand
CommandHandler load_aggregate
Invoice
InvoiceCreatedEvent
LineItemAddedEvent
send
Eventstore
store
load_from_history
publish_eventsEventHandlerView
update
InvoiceSentEvent
events are in past tense
Remember this picture?
Tuesday, April 9, 13
User wants to mark an invoice as paid
Tuesday, April 9, 13
Request is translated to specific command
Tuesday, April 9, 13
Propagates to correct Domain object
Tuesday, April 9, 13
Retrieves all events for Aggregate
Tuesday, April 9, 13
What does an event look like?
Tuesday, April 9, 13
Rebuild yourself from history
Tuesday, April 9, 13
Events are applied
Tuesday, April 9, 13
Propagates to correct Domain object
Tuesday, April 9, 13
Tell the invoice it has been paid
Tuesday, April 9, 13
Apply events on yourself
Tuesday, April 9, 13
Update your state
Tuesday, April 9, 13
Commit the events in the EventStore
Tuesday, April 9, 13
Publish the events to registered Handlers
Tuesday, April 9, 13
Update view model
Tuesday, April 9, 13
Render view
Tuesday, April 9, 13
User
POST /invoices/:id/send
Controller
SendInvoiceCommand
CommandHandler load_aggregate
Invoice
InvoiceCreatedEvent
LineItemAddedEvent
send
Eventstore
store
load_from_history
publish_eventsEventHandlerView
update
InvoiceSentEvent
events are in past tense
Summary
Tuesday, April 9, 13
EXAMPLE HISTORIC INTEL
Tuesday, April 9, 13
Introducing the timeline
Tuesday, April 9, 13
What was easy?
Tuesday, April 9, 13
Querying (separate model)
What was easy?
Tuesday, April 9, 13
Querying (separate model)Implemen&ng business logic
What was easy?
Tuesday, April 9, 13
Querying (separate model)Implemen&ng business logicMeasuring feature usage
What was easy?
Tuesday, April 9, 13
Querying (separate model)Implemen&ng business logicMeasuring feature usage
What was easy?
Read performance (op&mized view model)
Tuesday, April 9, 13
Querying (separate model)Implemen&ng business logicMeasuring feature usage
What was easy?
Read performance (op&mized view model)No BDUF for the data model
Tuesday, April 9, 13
What was hard?
Tuesday, April 9, 13
What was hard?Mental model change (thinking in events)
Tuesday, April 9, 13
Event upcas&ng
What was hard?Mental model change (thinking in events)
Tuesday, April 9, 13
Defining events
Event upcas&ng
What was hard?Mental model change (thinking in events)
Tuesday, April 9, 13
Open challenges
Tuesday, April 9, 13
Open challenges
Replay all events performance
Tuesday, April 9, 13
Open challenges
Replay all events performanceReplay all events performance
Tuesday, April 9, 13
Asynchronous view model update
Open challenges
Replay all events performanceReplay all events performance
Tuesday, April 9, 13
Zero down&me deployments
Asynchronous view model update
Open challenges
Replay all events performanceReplay all events performance
Tuesday, April 9, 13
Zero down&me deployments
Scaling
Asynchronous view model update
Open challenges
Replay all events performanceReplay all events performance
Tuesday, April 9, 13
Questions?
Tuesday, April 9, 13