Cqrs race conditions_and_sagas_ohmy
-
Upload
skills-matter -
Category
Technology
-
view
5.230 -
download
0
Transcript of Cqrs race conditions_and_sagas_ohmy
![Page 1: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/1.jpg)
CQRS, Race Conditions, and Sagas – Oh My!
Udi Dahan – The Software SimplistEnterprise Development Expert & SOA Specialist
![Page 2: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/2.jpg)
Agenda
CQRS Races Sagas Domain Model
![Page 3: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/3.jpg)
CQRS
![Page 4: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/4.jpg)
For collaborative domains
Get dataGet data
Change dataUser is looking at stale data
![Page 5: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/5.jpg)
Be upfront about staleness
Data correct as of 10 minutes ago
List of customers
![Page 6: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/6.jpg)
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
![Page 7: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/7.jpg)
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
![Page 8: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/8.jpg)
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
![Page 9: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/9.jpg)
All together
Queries
CommandsDB
Service Layer
Input
Validation Validation
Rules
QueriesView Model
View ModelUpdater
Publish
ClientQ
![Page 10: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/10.jpg)
Race Conditions
![Page 11: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/11.jpg)
Rules which cause commands to fail
Cancel OrderShip Order
Rules:
1. Cannot cancel shipped orders
2. Don’t ship cancelled orders
![Page 12: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/12.jpg)
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 }}
![Page 13: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/13.jpg)
Intensive Collaborative Domains
![Page 14: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/14.jpg)
Looks like we’re stuck
![Page 15: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/15.jpg)
Find the underlying business objective
The customer doesn’t want the products
No point in arguing
![Page 16: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/16.jpg)
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
![Page 17: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/17.jpg)
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
![Page 18: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/18.jpg)
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
![Page 19: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/19.jpg)
Watch for the business reaction
![Page 20: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/20.jpg)
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
![Page 21: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/21.jpg)
Enter The Saga
![Page 22: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/22.jpg)
What is a saga?
![Page 23: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/23.jpg)
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
![Page 24: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/24.jpg)
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
![Page 25: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/25.jpg)
Cancel OrderIs a pure command
Publishes the Order Cancelled event
No reason for it to fail
![Page 26: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/26.jpg)
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
![Page 28: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/28.jpg)
Domain Models
![Page 29: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/29.jpg)
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
![Page 30: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/30.jpg)
Sagas *ARE* your domain model
![Page 31: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/31.jpg)
All together
Queries
CommandsDB
Service Layer
Input
Validation Validation
Rules
QueriesView Model
View ModelUpdater
Publish
ClientQ
![Page 32: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/32.jpg)
In Closing
CQRS Races Sagas Domain Model
![Page 33: Cqrs race conditions_and_sagas_ohmy](https://reader035.fdocuments.us/reader035/viewer/2022062513/554c8f93b4c905df3c8b5b65/html5/thumbnails/33.jpg)
Thank youUdi Dahan – The Software SimplistEnterprise Development Expert & SOA Specialist