Post on 08-May-2015
transcript
CQRS, Race Conditions, and Sagas – Oh My!
Udi Dahan – The Software SimplistEnterprise Development Expert & SOA Specialist
Agenda
CQRS Races Sagas Domain Model
CQRS
For collaborative domains
Get dataGet data
Change dataUser is looking at stale data
Be upfront about staleness
Data correct as of 10 minutes ago
List of customers
Data Duplicated, No Relationships, Data Pre-Calculated
List of customers
Customer Service Rep view
ID Name Phone
List of customers
Supervisor view
ID Name Phone Lifetime value
Rep_Customers_TableID Name Phone
Supervisor_Customers_TableID Name Phone Lifetime Value
Use for preliminary validationBefore going to submit data, check if it already exists in the persistent view model
UniquenessCan expose to user (Yahoo user signup)
Related Entity ExistenceAddress validation – existence of street name
Results in less commands being rejected
Good commandsThe kind you can reply with:
“Thank you. Your confirmation email will arrive shortly”
Or just “fake it”
Inherently asynchronousNot really related to an entity
All together
Queries
CommandsDB
Service Layer
Input
Validation Validation
Rules
QueriesView Model
View ModelUpdater
Publish
ClientQ
Race Conditions
Rules which cause commands to fail
Cancel OrderShip Order
Rules:
1. Cannot cancel shipped orders
2. Don’t ship cancelled orders
Even if they’re easy to implement
public class Order{
public void Cancel() { if (status != OrderStatusEnum.Shipped)
//cancel }
public void Ship() { if (status != OrderStatusEnum.Cancelled)
//ship }}
Intensive Collaborative Domains
Looks like we’re stuck
Find the underlying business objective
The customer doesn’t want the products
No point in arguing
Find the underlying business objective
Rules:
1. Cannot cancel shipped orders
Because shipping costs money
That money would be lost if the customer cancelled
Because we refund the customers money
2. Don’t ship cancelled orders
Why?
So?
Why?
Refund Policies
AnalyzeWhen an order is cancelled,does the refund need to be given immediately?
Can we give a partial refund?
Would it be alright to check for cancellationmultiple times in the long-running shipping process?
No
Yes
Yes
Ask the hard questionsFor cancellation at each of the various stages in the shipping process,how much of a refund should the customer get?
Pick list generatedProducts collectedProducts packagedPackage moved to loading dockPackage loadedTransport dispatched
Watch for the business reaction
And then the business insightMost orders are cancelled within an hour of being submitted
After that, shipping starts – refund only when customer returns products for the cancelled order (less the shipping costs)
Full Refund
Partial Refund
Enter The Saga
What is a saga?
What is a saga?A pattern for implementing long-running processes
Similar to the state-machine pattern
Triggers are the arrival of messages
Time can be managed by an endpointSends messages when time is up
Order Acceptance SagaSubmit Order message triggers the creation of a saga
Saga requests to be “woken up” in an hour
If Cancel Order message arrives before that, saga completesDoes not publish an Order Accepted event
Otherwise, publishes the Order Accepted eventAnd then the saga completes
Cancel OrderIs a pure command
Publishes the Order Cancelled event
No reason for it to fail
Refund SagaTriggered by the Order Cancelled event
Correlated with the Products Returned event
Also subscribed to Products Shipped event
Decides whether to send:Full Refund messageRefund Less Shipping Costs message
Domain Models
The Domain Model Pattern
“If you have complicated and ever-changing business rules…”
“If you have simple not-null checks and a couple of sums to calculate, a Transaction Script is a better bet”
-- Martin Fowler, p119 Patterns of Enterprise Application Architecture
Sagas *ARE* your domain model
All together
Queries
CommandsDB
Service Layer
Input
Validation Validation
Rules
QueriesView Model
View ModelUpdater
Publish
ClientQ
In Closing
CQRS Races Sagas Domain Model
Thank youUdi Dahan – The Software SimplistEnterprise Development Expert & SOA Specialist
email@UdiDahan.comwww.UdiDahan.com