Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

Cqrs race conditions_and_sagas_ohmy

on

  • 5,658 views

 

Statistics

Views

Total Views
5,658
Views on SlideShare
3,006
Embed Views
2,652

Actions

Likes
5
Downloads
63
Comments
0

17 Embeds 2,652

http://skillsmatter.com 2597
http://realsma.com 31
http://smash 6
https://skillsmatter.com 4
http://192.168.56.101 2
http://85.92.73.37 1
http://www.xpaf.com 1
http://simplysys.com 1
http://smtp.filerepos.com 1
http://www.nambiz.xpaf.com 1
http://mail.xpaf.com 1
http://pop.xpaf.com 1
http://mail.simplysys.com 1
http://www.derickschoonbee.com 1
http://us-w1.rockmelt.com 1
http://translate.googleusercontent.com 1
http://www.filerepos.com 1
More...

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • This is a fact of life – like gravity – you can’t fight it.
  • We see this on Twitter and Facebook – telling us how long ago something happened
  • Don’t need all data in the DB or in the rules – what needs to be in there?
  • If a use clicked a button a microsecond earlier or later, that shouldn’t change fundamental business objectives.
  • Race conditions will cause many commands to fail, undoing much of the foundation of CQRS
  • Which indicates that there’s a bug in the requirements

Cqrs race conditions_and_sagas_ohmy Cqrs race conditions_and_sagas_ohmy Presentation Transcript

  • CQRS, Race Conditions, and Sagas – Oh My!
    Udi Dahan – The Software Simplist
    Enterprise Development Expert & SOA Specialist
  • Agenda
  • CQRS
  • For collaborative domains
    Get data
    Get data
    User is looking at stale data
    Change data
  • Be upfront about staleness
    List of customers
    Data correct as of
    10 minutes ago
  • Data Duplicated, No Relationships, Data Pre-Calculated
    Supervisor view
    Customer Service Rep view
    List of customers
    List of customers
    ID Name Phone Lifetime value
    ID Name Phone
  • Use for preliminary validation
    Before going to submit data, check if it already exists in the persistent view model
    Uniqueness
    Can expose to user (Yahoo user signup)
    Related Entity Existence
    Address validation – existence of street name
    Results in less commands being rejected
  • Good commands
    The kind you can reply with:
    “Thank you.
    Your confirmation email will arrive shortly”
    Or just “fake it”
    Inherently asynchronous
    Not really related to an entity
  • All together
    Queries
    View Model
    Client
    Queries
    Commands
    Publish
    Service Layer
    Input
    Q
    Validation
    Validation
    Rules
    View Model
    Updater
    DB
  • Race Conditions
  • Rules which cause commands to fail
    Rules:
    Cannot cancel shipped orders
    Don’t ship cancelled orders
    Cancel Order
    Ship Order
  • Even if they’re easy to implement
    public class Order
    {
    publicvoid Cancel()
    {
    if (status != OrderStatusEnum.Shipped)
    //cancel
    }
    publicvoid 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:
    Cannot cancel shipped orders
    Because shipping costs money
    That money would be lost if the customer cancelled
    Because we refund the customers money
    Don’t ship cancelled orders
    Why?
    So?
    Why?
    Refund Policies
  • Analyze
    When 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 cancellation
    multiple times in the long-running shipping process?
    No
    Yes
    Yes
  • Ask the hard questions
    For cancellation at each of the various stages in the shipping process,
    how much of a refund should the customer get?
    Pick list generated
    Products collected
    Products packaged
    Package moved to loading dock
    Package loaded
    Transport dispatched
  • Watch for the business reaction
  • And then the business insight
    Most 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 endpoint
    Sends messages when time is up
  • Order Acceptance Saga
    Submit 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 completes
    Does not publish an Order Accepted event
    Otherwise, publishes the Order Accepted event
    And then the saga completes
  • Cancel Order
    Is a pure command
    Publishes the Order Cancelled event
    No reason for it to fail
  • Refund Saga
    Triggered by the Order Cancelled event
    Correlated with the Products Returned event
    Also subscribed to Products Shipped event
    Decides whether to send:
    Full Refund message
    Refund Less Shipping Costs message
  • Open Source .NET Framework
    www.NServiceBus.com
  • 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
    View Model
    Client
    Queries
    Commands
    Publish
    Service Layer
    Input
    Q
    Validation
    Validation
    Rules
    View Model
    Updater
    DB
  • In Closing
  • Thank you
    Udi Dahan – The Software Simplist
    Enterprise Development Expert & SOA Specialist
    email@UdiDahan.com
    www.UdiDahan.com