Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Know the flow! Events, commands and long-running services.

Know the flow! Events, commands and long-running services.

@martinschimak -> Sep 13th, 2018 -> ExploreDDD Denver

Martin Schimak

September 13, 2018
Tweet

More Decks by Martin Schimak

Other Decks in Programming

Transcript

  1. Event
    Command
    Sep 13th, 2018 -> Denver -> Explore DDD Conference
    Know
    the flow!
    Events, commands and long-running services
    @martinschimak

    View full-size slide

  2. Apples Oranges
    @martinschimak
    vs

    View full-size slide

  3. Commands Events
    @martinschimak
    vs
    Retrieve
    payment
    Payment
    retrieved

    View full-size slide

  4. vs
    Orchestration Choreography
    Long-running
    services

    View full-size slide

  5. Events look into the past
    „An event is something that
    has happened in the past.“
    - Greg Young
    Payment
    retrieved
    @martinschimak

    View full-size slide

  6. Commands look into the future
    A command specifies something
    that should happen in the future.
    Retrieve
    payment
    @martinschimak

    View full-size slide

  7. Commands and events. And intent.
    „Commands are all about
    intent.
    - Jonas Bonér
    @martinschimak
    Events are intentless.“
    Command
    Retrieve
    payment
    Payment
    retrieved
    Event

    View full-size slide

  8. Commands and events. Like two sides of a coin.
    Payment
    retrieved
    Payment
    retrieval
    Command Event
    Execution
    Something should happen Something happens Something happened
    Retrieve
    payment
    @martinschimak

    View full-size slide

  9. Commands and events. Flipping the coin ...
    Payment
    retrieved
    Order
    fulfillment
    process
    Event Execution
    Something happens
    Something happened
    @martinschimak
    Command
    Something should happen
    Fetch
    goods

    View full-size slide

  10. Domain events are more explicitly discussed than commands
    @martinschimak

    View full-size slide

  11. Worker
    Manager
    @martinschimak
    Why?

    View full-size slide

  12. Events are really simple to grasp ...
    Goods
    shipped
    Order
    placed
    Goods
    fetched
    Payment
    retrieved
    ... EventStorming!
    @martinschimak

    View full-size slide

  13. EventStorming! When it starts to get interesting! :-)
    @martinschimak

    View full-size slide

  14. Events to separate write and read models ...
    Order
    summary
    Payment
    retrieved
    Payment UI
    raises
    projected
    onto
    ... CQRS!
    @martinschimak

    View full-size slide

  15. Events to temporally decouple services ...
    Order
    summary
    Payment
    retrieved
    Payment UI
    raises
    projected
    onto
    Payment Service
    Order Service
    ... CQRS with „foreign“ events!
    @martinschimak

    View full-size slide

  16. Events to represent history and state of systems ...
    Payment
    received
    Payment
    Payment
    received
    Payment
    received
    Payment
    received
    Payment
    retrieved
    ... Event Sourcing!
    @martinschimak

    View full-size slide

  17. Events to enable whole business processes ...
    ... Event
    choreographies!
    Payment
    received
    Inform
    customer
    listened to by
    Payment Service
    Notification Service
    @martinschimak

    View full-size slide

  18. Let‘s just rely on events in between services!
    @martinschimak

    View full-size slide

  19. Payment Service Credit Card
    Gateway
    @martinschimak

    View full-size slide

  20. Item
    ordered
    My context of sophisticated fulfillments
    Energy
    traded
    Order
    fulfillment
    Energy trade
    execution
    Coverage
    extension
    requested
    Policy
    issuance
    @martinschimak

    View full-size slide

  21. plexiti.com
    @martinschimak

    View full-size slide

  22. New pair
    of shoes
    ordered
    End-to-end business processes and their phases
    New pair
    of shoes
    ordered
    Old pair
    of shoes
    worn out
    Order new
    pair of
    shoes
    New pair
    of shoes
    delivered
    Job Execution
    History
    Customer-driven Supplier-driven
    Supplier-exploring
    Human to Machine
    Supporting Service
    Customer-determined
    Machine to Machine
    Automated Service
    @martinschimak

    View full-size slide

  23. A company‘s „long-running“ order fulfillment service
    Order new
    pair of
    shoes
    New pair
    of shoes
    delivered
    Command Event
    “Long-running”
    service
    Order
    fulfillment
    service
    Supplier-driven
    Customer-determined
    Machine to Machine
    Automated Service
    @martinschimak

    View full-size slide

  24. „Long-running“ services have long-running contracts
    Order new
    pair of
    shoes
    New pair
    of shoes
    delivered
    Command Event
    Order
    fulfillment
    service
    “Long-running”
    service
    “Long-running”
    asynchronous contract
    Hours to weeks
    @martinschimak

    View full-size slide

  25. „Long-running“ services may have internal customers
    Retrieve
    payment
    Payment
    retrieved
    Payment
    service
    “Long-running”
    service
    Command Event
    Seconds to weeks
    @martinschimak
    “Long-running”
    asynchronous contract

    View full-size slide

  26. „Long-running“ services with fine-grained contracts
    Charge
    credit card
    Credit card
    charged
    Credit card
    gateway
    “Long-running”
    service
    Command Event
    1 to 20 seconds
    @martinschimak
    “Long-running”
    asynchronous contract

    View full-size slide

  27. Order Service
    Order
    placed
    Payment Service
    Payment
    retrieved
    Inventory Service
    Goods
    fetched
    Shipment Service
    Goods
    shipped
    The choreography principle
    @martinschimak

    View full-size slide

  28. Payment Service Inventory Service Shipment Service
    The orchestration principle
    Order Service
    Fetch
    goods
    Retrieve
    payment
    Ship
    goods

    View full-size slide

  29. This is how the story is often presented …
    Choreo-
    graphy
    Orches-
    tration
    Process logic Process control
    explicit central
    implicit decentral
    Service coupling
    high
    low
    @martinschimak

    View full-size slide

  30. Order Service
    Order
    placed
    Payment Service
    Payment
    retrieved
    Inventory Service
    Goods
    fetched
    Shipment Service
    Goods
    shipped
    Choreographies and their implicit process logic ...
    @martinschimak

    View full-size slide

  31. Order Service Payment Service Inventory Service Shipment Service
    Order
    placed
    Goods
    shipped
    ... can lead to a hidden form of accidental coupling
    Payment Service
    Inventory Service
    Goods
    fetched
    Payment
    retrieved
    ... to change implicit process logic ...
    Change
    listener 1 ...
    Change
    listener 2 ...
    Change
    listener 3 ...
    @martinschimak

    View full-size slide

  32. Payment Service Inventory Service Shipment Service
    Orchestration and its explicit process logic ...
    Order Service
    Fetch
    goods
    Retrieve
    payment
    Ship
    goods
    ... does not display THIS form of
    hidden accidental coupling...

    View full-size slide

  33. Who made the decision to use events only?
    @martinschimak

    View full-size slide

  34. This is how the story develops …
    Choreo-
    graphy
    Orches-
    tration
    Process logic Process control
    explicit central
    implicit decentral
    Service coupling
    high
    low
    Not
    necessarily
    Not
    necessarily
    @martinschimak

    View full-size slide

  35. Let‘s dig a bit deeper and look into this ...
    Choreo-
    graphy
    Orches-
    tration
    Process logic Process control
    explicit central
    implicit decentral
    Service coupling
    high
    low
    Not
    necessarily
    Not
    necessarily
    @martinschimak

    View full-size slide

  36. Unhealthy apples vs. fruity and ripe oranges
    š Maybe Synchronous
    š Request and Response Style
    š Sender/Receiver Exchange
    š Probably Transient
    š Asynchronous by Nature
    š Fire and Forget Style
    š Publish/Subscribe Exchange
    š Probably Persistent
    Command Event
    @martinschimak

    View full-size slide

  37. Payment Service Inventory Service Shipment Service
    Why not asynchronous commands? „Doable“ ...
    Order Service
    Fetch
    goods
    Retrieve
    payment
    Ship
    goods
    Request
    Async Response
    Request Async
    Response
    Async Response
    Request

    View full-size slide

  38. How unhealthy is this apple ...
    š Maybe Synchronous
    š Request and Response Style
    š Sender/Receiver Exchange
    š Probably Transient
    š Asynchronous by Nature
    š Fire and Forget Style
    š Publish/Subscribe Exchange
    š Probably Persistent
    Command Event
    Or Asynchronous!
    @martinschimak

    View full-size slide

  39. ?
    ? ?
    Payment Service Inventory Service Shipment Service
    Why not fire the command? And listen to an event?
    Order Service
    Fetch
    goods
    Retrieve
    payment
    Ship
    goods
    Payment
    retrieved
    Goods
    fetched
    Goods
    shipped
    Order
    fulfilled
    Fire
    Fire Fire
    Listen
    Listen Listen

    View full-size slide

  40. How unhealthy is this apple ...
    š Maybe Synchronous
    š Request and Response Style
    š Sender/Receiver Exchange
    š Probably Transient
    š Asynchronous by Nature
    š Fire and Forget Style
    š Publish/Subscribe Exchange
    š Probably Persistent
    Command Event
    Or Asynchronous!
    Like Fire and Listen?
    @martinschimak

    View full-size slide

  41. ?
    Payment Service
    Why not publish the command?
    Order Service
    Retrieve
    payment
    Payment
    retrieved
    Publish
    @martinschimak

    View full-size slide

  42. ?
    Payment Service
    ... we get location transparency inside a context ...
    Order Service
    Retrieve
    payment
    Payment
    retrieved
    Publish
    Subscribe
    Financial Services
    Bounded Context
    @martinschimak

    View full-size slide

  43. ?
    Payment Service
    ... and the ability to subscribe to „everything“.
    Order Service
    Retrieve
    payment
    Payment
    retrieved
    Publish
    Subscribe
    Financial Services
    Bounded Context
    Subscribe
    Subscribe
    Business Monitoring
    Bounded Context
    „Order Context wanted
    payment to happen!“
    „Financial
    Context retrieved
    payment!“

    View full-size slide

  44. The apple doesn’t look so unhealthy anymore ...
    š Maybe Synchronous
    š Request and Response Style
    š Sender/Receiver Exchange
    š Probably Transient
    š Asynchronous by Nature
    š Fire and Forget Style
    š Publish/Subscribe Exchange
    š Potentially Persistent
    Command Event
    Or Asynchronous!
    Like Fire and Listen?
    Like Publish and
    Promise to Subscribe?
    @martinschimak

    View full-size slide

  45. vs
    Apples Oranges
    @martinschimak

    View full-size slide

  46. Hey DDD! Do you like purely semantical differences? :-)
    š We specified an intended job
    š This just models our “state of mind”
    š We use imperative tense to express intent
    š Defined by supplier bounded context,
    created by customer system
    š We created a relevant change
    š This models everybody’s “state of world”
    š We use past tense to express change
    š Defined and created by emitter bounded
    context and system
    Command Event
    @martinschimak

    View full-size slide

  47. ?
    Payment Service
    Related semantics are both defined in supplier context
    Retrieve
    payment
    Payment
    retrieved
    Financial Services
    Bounded Context
    „When YOU publish
    this WE promise to
    subscribe and act!“
    „When WE publish
    this YOU decide to
    subscribe and act!“
    Command Event
    @martinschimak
    Make the
    contract explicit!

    View full-size slide

  48. ?
    Payment Service
    Mix and match decentral orchestration and choreography
    Retrieve
    payment
    Payment
    retrieved
    Financial
    Context
    Command
    Event
    Order Service
    Fulfillment
    Context
    Payment
    retrieval
    failed
    Event
    Marketing
    Context
    Customer
    promoted
    Promotions
    service
    Event
    @martinschimak

    View full-size slide

  49. New pair
    of shoes
    ordered
    It‘s a supplier‘s responsibility to act on customer intent
    New pair
    of shoes
    ordered
    Retrieve
    payment
    Payment
    retrieved
    Command Event(s)
    Events
    Customer Supplier
    Payment Service
    @martinschimak

    View full-size slide

  50. New pair
    of shoes
    ordered
    It‘s a customer‘s responsibility to specify intent
    New pair
    of shoes
    ordered
    Retrieve
    payment
    Command Event(s)
    Events
    Customer Supplier
    Order
    placed
    Order Service
    @martinschimak

    View full-size slide

  51. New pair
    of shoes
    ordered
    Some responsibilities span both phases, but not all
    New pair
    of shoes
    ordered
    Promote
    customer
    Command Event(s)
    Events
    Payment
    retrieved
    Promotions
    service
    Customer
    promoted
    Promotions
    service
    @martinschimak

    View full-size slide

  52. Wait. It really depends ... on semantics?
    @martinschimak

    View full-size slide

  53. Orchestration and choreography... „it really depends“.
    Choreo-
    graphy
    Orches-
    tration
    Process logic Process control
    explicit central
    implicit decentral
    Service coupling
    high
    low
    Not
    necessarily
    Not
    necessarily
    Not
    necessarily
    Not
    necessarily
    @martinschimak

    View full-size slide

  54. Embed and contain state managing business logic!
    Retrieve
    payment
    Payment
    retrieved
    Payment
    service
    “Long-running”
    service
    Seconds to weeks
    Payment
    retrieval
    failed
    Command Event(s)
    @martinschimak

    View full-size slide

  55. A blossoming ecosystem of saga & process managers
    CADENCE
    Baker

    View full-size slide

  56. Don‘t underestimate state managing business logic...
    Payment
    retrieved
    Payment
    service
    Seconds to weeks
    Payment
    retrieval
    canceled
    Command Event(s)
    Timeout
    Retry
    Version
    Compensate
    Parallelise
    Retrieve
    payment
    @martinschimak

    View full-size slide

  57. @martinschimak

    View full-size slide

  58. @martinschimak

    View full-size slide

  59. DangerZone. Experimental. Open
    Thinking. (Apples and Oranges.)

    View full-size slide

  60. Why not ... persisting commands, too?
    š Maybe Synchronous
    š Request and Response Style
    š Sender/Receiver Exchange
    š Probably Transient
    š Asynchronous by Nature
    š Fire and Forget Style
    š Publish/Subscribe Exchange
    š Probably Persistent
    Command Event
    Or Asynchronous!
    Like Fire and Listen?
    Like Publish and
    Promise to Subscribe?
    @martinschimak

    View full-size slide

  61. Payment
    retrieval
    saga
    Payment
    requested
    Order
    fulfillment
    Credit card
    Account
    Withdraw
    amount
    Order placed
    Retrieve
    payment
    Order
    items
    @martinschimak

    View full-size slide

  62. An eventsourced aggregate ... (1)
    public List process(RetrievePaymentCommand cmd) {
    return EventUtil.events(
    new PaymentRequestedEvent(...)
    );
    }
    @martinschimak

    View full-size slide

  63. An eventsourced saga pattern ... (2)
    public List process(RetrievePaymentCommand cmd) {
    return EventUtil.events(
    new PaymentRequestedEvent(...),
    new WithdrawAmountCommand(...)
    );
    }
    @martinschimak

    View full-size slide

  64. Payment
    retrieval
    saga
    Payment
    requested
    Order
    fulfillment
    Credit card
    Account
    Amount
    withdrawn
    Withdraw
    amount
    Payment
    partly
    covered
    Credit Card
    charged
    Charge
    credit card
    Payment
    retrieved
    Order placed
    Retrieve
    payment
    Order
    fulfilled
    Order
    items
    @martinschimak

    View full-size slide

  65. Payment
    retrieval
    Payment
    requested
    Order
    fulfillment
    Credit card
    Account
    Amount
    withdrawn
    Withdraw
    amount
    Payment
    partly
    covered
    Credit Card
    charged
    Charge
    credit card
    Payment
    retrieved
    Order placed
    Retrieve
    payment
    Order
    fulfilled
    Order
    items
    “Fact”sourced sagas
    know the flow of
    events and commands
    created by itself.
    @martinschimak

    View full-size slide

  66. “Fact”sourced sagas leverage the “event nature” of commands.
    Command
    Event
    @martinschimak
    creating!
    intent.

    View full-size slide

  67. Why? Aggregate
    (Worker)
    Process
    Manager
    @martinschimak

    View full-size slide

  68. Why?
    @martinschimak
    Process
    Manager
    Aggregate

    View full-size slide

  69. medium.com/plexiti @martinschimak
    Thank you!

    View full-size slide