+ All Categories
Home > Technology > Cqrs race conditions_and_sagas_ohmy

Cqrs race conditions_and_sagas_ohmy

Date post: 08-May-2015
Category:
Upload: skills-matter
View: 5,229 times
Download: 0 times
Share this document with a friend
33
CQRS, Race Conditions, and Sagas – Oh My! Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist
Transcript
Page 1: Cqrs race conditions_and_sagas_ohmy

CQRS, Race Conditions, and Sagas – Oh My!

Udi Dahan – The Software SimplistEnterprise Development Expert & SOA Specialist

Page 2: Cqrs race conditions_and_sagas_ohmy

Agenda

CQRS Races Sagas Domain Model

Page 3: Cqrs race conditions_and_sagas_ohmy

CQRS

Page 4: Cqrs race conditions_and_sagas_ohmy

For collaborative domains

Get dataGet data

Change dataUser is looking at stale data

Page 5: Cqrs race conditions_and_sagas_ohmy

Be upfront about staleness

Data correct as of 10 minutes ago

List of customers

Page 6: Cqrs race conditions_and_sagas_ohmy

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

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

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

All together

Queries

CommandsDB

Service Layer

Input

Validation Validation

Rules

QueriesView Model

View ModelUpdater

Publish

ClientQ

Page 10: Cqrs race conditions_and_sagas_ohmy

Race Conditions

Page 11: Cqrs race conditions_and_sagas_ohmy

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

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

Intensive Collaborative Domains

Page 14: Cqrs race conditions_and_sagas_ohmy

Looks like we’re stuck

Page 15: Cqrs race conditions_and_sagas_ohmy

Find the underlying business objective

The customer doesn’t want the products

No point in arguing

Page 16: Cqrs race conditions_and_sagas_ohmy

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

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

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

Watch for the business reaction

Page 20: Cqrs race conditions_and_sagas_ohmy

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

Enter The Saga

Page 22: Cqrs race conditions_and_sagas_ohmy

What is a saga?

Page 23: Cqrs race conditions_and_sagas_ohmy

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

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

Cancel OrderIs a pure command

Publishes the Order Cancelled event

No reason for it to fail

Page 26: Cqrs race conditions_and_sagas_ohmy

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 27: Cqrs race conditions_and_sagas_ohmy

Open Source .NET Framework

www.NServiceBus.com

Page 28: Cqrs race conditions_and_sagas_ohmy

Domain Models

Page 29: Cqrs race conditions_and_sagas_ohmy

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

Sagas *ARE* your domain model

Page 31: Cqrs race conditions_and_sagas_ohmy

All together

Queries

CommandsDB

Service Layer

Input

Validation Validation

Rules

QueriesView Model

View ModelUpdater

Publish

ClientQ

Page 32: Cqrs race conditions_and_sagas_ohmy

In Closing

CQRS Races Sagas Domain Model

Page 33: Cqrs race conditions_and_sagas_ohmy

Thank youUdi Dahan – The Software SimplistEnterprise Development Expert & SOA Specialist

[email protected]


Recommended