$30 off During Our Annual Pro Sale. View Details »

DDD Exchange London 2018: Know the flow! Events, commands and long-running services.

DDD Exchange London 2018: Know the flow! Events, commands and long-running services.

@martinschimak => April 26th, 2018 => London => DDD Exchange

Martin Schimak

April 26, 2018
Tweet

More Decks by Martin Schimak

Other Decks in Programming

Transcript

  1. Events, commands and long-running services
    @martinschimak -> April 26th, 2018 -> London -> DDD eXchange
    Know the flow!

    View Slide

  2. Read
    Model
    Domain
    Event
    Aggregate
    Policy
    Command
    External System
    UI
    Command
    Invoked on raises
    raises
    Invoked on
    projected
    onto
    listened to by triggers
    triggers
    Inspired by the picture that explains (almost) everything :-) *
    *) © The ingenious Alberto Brandolini

    View Slide

  3. Domain events are more prominently discussed than commands

    View Slide

  4. The cool thing about domain events? They are facts.
    It already happened.
    Events let us look into the past.
    Payment
    received

    View Slide

  5. Events create trust in a world of eventual consistency.
    Strong consistency
    Payment
    received
    Payment

    View Slide

  6. We are dedicated to events. For good reasons. CQRS!
    Order
    summary
    Payment
    received
    Payment UI
    raises
    projected
    onto

    View Slide

  7. Need more good reasons? Event Sourcing!
    Payment
    received
    Payment
    Payment
    received
    Payment
    received
    Payment
    received
    Payment
    received

    View Slide

  8. Events are easy to grasp. Event Storming!
    Goods
    shipped
    Order
    placed
    Goods
    fetched
    Payment
    received
    „We need to achieve some facts to create value for customers!“
    Events are a concept easy to grasp for everybody in the room.

    View Slide

  9. Events are relaxed! Just feel fine. Don‘t want to achieve anything.

    View Slide

  10. Events help to decouple services.
    Order
    summary
    Payment
    received
    Payment UI
    raises
    projected
    onto
    Payment Service
    Order Service

    View Slide

  11. Events help with cross-cutting concerns
    Payment
    received
    Customer
    Notification
    Policy
    Inform
    customer
    listened to by triggers
    Payment Service
    Notification Service

    View Slide

  12. Let‘s just rely on events in between services!

    View Slide

  13. Relying on events only leads to suboptimal coupling
    Order
    placed
    Payment Service
    Payment
    received
    Inventory Service
    Goods
    fetched
    Shipment Service
    Goods
    shipped

    View Slide

  14. Shipment Service
    Autonomous services can be deployed independently.
    To change the order of two services, we touch 4 sticky notes… but wait, all three services!
    Goods
    shipped
    Order
    placed
    Inventory Service Payment Service
    Payment
    received
    Goods
    fetched

    View Slide

  15. Who made the decision to use events only?

    View Slide

  16. Our policies decide to act on facts – with commands
    Domain
    Event
    Policy
    Command
    listened to by triggers

    View Slide

  17. Who owns the policy in a pure event choreography?
    Payment
    received
    Start
    Fetching
    Policy
    Fetch
    goods
    listened to by triggers
    Payment Service
    Inventory Service

    View Slide

  18. Who owns the policy in a commanding approach?
    Payment
    received
    Start
    Fetching
    Policy
    Fetch
    goods
    listened to by triggers
    Payment Service
    Inventory Service

    View Slide

  19. Neither of both truly „owns“ that policy?
    Payment
    received
    Fetch
    goods
    listened to by triggers
    Payment Service
    Inventory Service
    Start
    Fetching
    Policy

    View Slide

  20. Extracting the policy into a mediator service
    Payment
    received
    Start
    Fetching
    Policy
    Fetch
    goods
    listened to by triggers
    Payment Service
    Inventory Service
    Order Service

    View Slide

  21. The order as the companies core business
    Place
    order
    Order
    placed
    Request
    payment
    Order Service
    Payment
    received
    Fetch
    goods
    Goods
    fetched
    Ship
    goods
    Goods
    shipped
    “When the
    order is
    placed…”
    “When the
    payment is
    received…”
    “When the
    goods are
    fetched…”
    “Then we
    are done!”
    Mark
    order as
    fulfilled
    Order
    fulfilled
    Payment Service Inventory Service Shipment Service

    View Slide

  22. Double check: what happens when we charge later?
    Place
    order
    Order
    placed
    Request
    payment
    Order Service
    Payment
    received
    Fetch
    goods
    Goods
    fetched
    Ship
    goods
    Goods
    shipped
    “When the
    order is
    placed…”
    “When the
    payment is
    received…”
    “When the
    goods are
    fetched…”
    “Then we
    are done!”
    Mark
    order as
    fulfilled
    Order
    fulfilled
    Payment Service
    Inventory Service Shipment Service

    View Slide

  23. Events decrease coupling? Often! Pro Tip: some commands may help, too!-)

    View Slide

  24. Trade offs! Events and commands in between services.
    Payment
    received
    Policy
    Fetch
    goods
    listened to by triggers
    Payment Service
    Inventory Service
    Order Service

    View Slide

  25. Commands are intent
    Nothing really happened so far.
    It‘s in the future.
    Place
    order

    View Slide

  26. „Place order“ seen as an atomic transaction
    Order
    Invoked on
    Order
    Placed
    raises
    Place
    order

    View Slide

  27. When hugging a tree, it is difficult to see the wood!

    View Slide

  28. „Place order“ seen as a long-running service
    Order
    fulfilled
    Order
    canceled
    Payment
    received
    Goods
    shipped
    Place
    order
    Order
    placed

    View Slide

  29. Atomic vs. composite command execution
    Place
    order
    Order
    placed
    Typically we see a „command“ as the
    intent to change a write model...
    … but the customer‘s or service clients
    intent is often targeted at a more
    valuable business result, which needs
    many steps to be achieved.
    Place
    order
    Order
    fulfilled
    Atomic, trans-
    actional execution
    Composite, long-
    running execution

    View Slide

  30. Is that command executable as an atomic step?
    Request
    payment

    View Slide

  31. Let‘s do some event storming…
    Request
    payment
    Payment
    received
    Payment
    requested
    Charge
    credit card
    Depending
    on account
    balance
    Withdraw
    amount from
    account Amount
    Withdrawn
    Credit card
    charged
    Credit card
    failed
    Update
    credit card
    details
    Account
    details
    Credit card
    details
    updated
    Whenever
    card details
    are updated
    After two
    weeks
    Depending
    on the
    amount
    Amount
    credited
    Mark
    payment
    received
    Mark
    payment
    canceled
    Payment
    canceled
    Credit
    amount to
    account
    Whenever
    payment is
    canceled
    Mark
    payment
    received
    Payment
    received

    View Slide

  32. Credit card
    gateway
    Accounting
    system
    Payment
    Request
    payment
    Charge
    credit card
    Credit card
    charged
    Our result seen as a collection of commands
    Withdraw
    amount from
    account
    Amount
    Withdrawn
    Amount
    credited
    Credit
    amount to
    account
    Mark
    payment
    Payment
    received
    Payment
    canceled
    Payment
    requested
    Credit card
    failed
    Update
    credit card
    details
    Credit card
    details
    updated
    Update
    credit card
    details
    Credit card
    details
    updated
    Credit card
    failed

    View Slide

  33. What happens if we expose those atomic commands?
    Charge
    credit card
    Credit card
    charged
    Credit
    card failed
    Payment Service
    Retry with
    new credit
    card details
    Who owns the policy?

    View Slide

  34. Payment seen as a long-running service
    Request
    payment
    Payment
    received
    Payment Service

    View Slide

  35. What happens if we expose troubles with some steps?
    Request
    payment
    Payment
    received
    Credit
    card failed
    Payment Service
    Retry with
    new credit
    card details
    Who owns the policy?

    View Slide

  36. „Delegating our problems to our clients forces them to
    deal with the mitigation. They become god services.“
    Credit card
    failed
    Retry with
    new credit
    card details
    Charge
    credit card
    listened to by
    triggers
    Payment Service
    Order Service

    View Slide

  37. Smart endpoints (with dumb pipes) …
    … don‘t delegate problems they better deal with themselves!

    View Slide

  38. A long-running smart endpoint …
    Request
    payment
    Payment
    received
    Payment
    canceled
    1) creates value – serving a result its client is really interested in
    2) assumes responsibility – instead of delegating troubles with internal steps to its client
    3) may go asynchronous – if needed for composite execution
    Payment Service

    View Slide

  39. Long-running services help to protect bounded contexts
    Order service
    Payment service
    Request
    payment
    Payment
    canceled
    Payment
    received

    View Slide

  40. How to get productive with long-running services?

    View Slide

  41. Heavily inspired by
    Mathias Verraes! Thank you!
    Considering the
    CQRS pattern
    Read Model
    Projections
    Write Model
    Invariant
    Protection
    Client/Human
    Interaction
    Query
    Command
    Domain
    Event

    View Slide

  42. Write Model
    Read Model
    Query
    Domain
    Event
    Command
    Observe Act
    Decide

    View Slide

  43. How do we implement the payment service?
    Request
    payment
    Payment
    received
    Payment
    requested
    Charge
    credit card
    Depending
    on account
    balance
    Withdraw
    amount from
    account Amount
    Withdrawn
    Credit card
    charged
    Credit card
    failed
    Update
    credit card
    details
    Account
    details
    Credit card
    details
    updated
    Whenever
    card details
    are updated
    After two
    weeks
    Depending
    on the
    amount
    Amount
    credited
    Mark
    payment
    received
    Mark
    payment
    canceled
    Payment
    canceled
    Credit
    amount to
    account
    Whenever
    payment is
    canceled
    Mark
    payment
    received
    Payment
    received

    View Slide

  44. Write Model
    Read Model
    Query
    Domain
    Event
    Command
    Decide
    Observe Act

    View Slide

  45. Write Model
    Read Model
    Process Model
    Domain
    Event
    Command
    Query
    Policy
    plexiti
    de|coding domain language
    Observe Act
    Decide

    View Slide

  46. Process Model
    Write Model
    Domain
    Event
    Command
    Process Manager
    Read Model
    Policy
    Observe
    Decide

    View Slide

  47. How to get productive with process managers?
    Handling of
    time & timeouts
    Retries
    Compensation
    Message-Driven

    View Slide

  48. e.g. with Axon Messaging & Saga Management
    @Saga
    class Payment {
    private var paymentAmount = 0F
    @StartSaga
    @SagaEventHandler(associationProperty = "paymentId")
    fun on(event: PaymentRequested) {
    // save state, evaluate policy, create command
    }
    }
    Private, persistent
    process state
    Managed event
    correlation
    Process policy
    and construct
    command
    is cool, but Java works as well! :-)

    View Slide

  49. What about business process execution engines?
    Monitoring
    & Operations
    Handling of
    time & timeouts
    Versatile Retries
    Explicitness &
    Visibility for Flows
    Versioning for
    Flows and Policies
    Transparent
    Compensation
    Parallelisation and
    synchronisation

    View Slide

  50. Payment service as a BPMN model

    View Slide

  51. Payment service as a BPMN model

    View Slide

  52. Make the implicit explicit!

    View Slide

  53. Explicit and simple timer management

    View Slide

  54. Transparent and simple compensation handling

    View Slide

  55. Transparent business process monitoring and operations
    897
    2
    Temporarily
    failing commands
    Executions
    currently waiting
    for events

    View Slide

  56. But are business process engines
    usable in a modern architecture?

    View Slide

  57. Credit amount
    back to account
    Withdraw
    amount from cust.
    account
    Charge amount
    by credit card
    I want that kind of „reactive“ business process execution
    Charge amount
    by credit card
    Credit amount
    back to account
    Update credit card
    details
    Check customer
    account balance
    Payment
    requested
    Depending
    on account
    balance
    Charge
    credit card
    Withdraw
    amount from
    account
    Credit card
    charged
    Whenever
    credit card
    is charged
    Mark
    payment
    received
    Payment
    received
    Amount
    Withdrawn
    Depending
    on the
    amount
    Mark
    payment
    received
    Payment
    received
    Mark paym.
    partly
    covered
    Payment
    partly
    covered
    Whenever
    payment
    p. covered
    Charge
    credit card
    Credit card
    failed
    Whenever
    credit card
    failed
    Update
    credit card
    details
    Credit card
    details
    updated
    Whenever
    card details
    are updated
    Charge
    credit card
    After two
    weeks
    Mark
    payment
    canceled
    Payment
    canceled
    Whenever
    payment is
    canceled
    Credit
    amount to
    account
    Amount
    credited
    Every two
    days
    Remind
    customer
    Customer
    reminded
    Withdraw
    amount from cust.
    account

    View Slide

  58. I want autonomous services - respecting boundaries
    Order service
    Payment service
    Request
    payment
    Payment
    received
    Payment
    canceled
    Request
    payment

    View Slide

  59. I want transparent commanding - atomic or composite
    Order service
    Request
    payment

    View Slide

  60. With this I get flexible and local technology decisions
    Order service
    Payment service
    Request
    payment
    Payment
    received
    Payment
    canceled
    Request
    payment
    Your local implementation reacting to events and commands

    View Slide

  61. Is it feasible? Sure it is.

    View Slide

  62. e.g. with Camunda an open source process engine
    Domain
    Entities, value objects, domain events etc…
    + atomic command behaviour + composite, long-running command behaviour
    Application
    Infrastructure
    Process
    Engine

    View Slide

  63. Reality Check. Order fulfillment process of a very large online retailer.

    View Slide

  64. BDD style business process specification

    View Slide

  65. Charge amount
    by credit card
    BDD style business process test - and visual feedback
    Charge amount
    by credit card
    Update credit card
    details
    Check customer
    account balance

    View Slide

  66. An emerging ecosystem of saga, state & process managers
    CADENCE
    Baker

    View Slide

  67. I like graphical models to know and discuss „what is“…

    View Slide

  68. … and I like sticky notes to discuss „what should be“!
    Request
    payment
    Payment
    received
    Payment
    requested
    Charge
    credit card
    Depending
    on account
    balance
    Withdraw
    amount from
    account Amount
    Withdrawn
    Credit card
    charged
    Credit card
    failed
    Update
    credit card
    details
    Account
    details
    Credit card
    details
    updated
    Whenever
    card details
    are updated
    After two
    weeks
    Depending
    on the
    amount
    Amount
    credited
    Mark
    payment
    received
    Mark
    payment
    canceled
    Payment
    canceled
    Credit
    amount to
    account
    Whenever
    payment is
    canceled
    Mark
    payment
    received
    Payment
    received

    View Slide

  69. Need code? Reactive processes with Axon & Camunda.
    Order service
    Payment service
    Request
    payment
    Payment
    received
    Payment
    canceled
    Request
    payment
    plexiti
    de|coding domain language
    https://github.com/plexiti/axon-camunda-poc
    @martinschimak

    View Slide