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

Command & [e]Mission Control: Using Command and Event Buses to create a CQRS-friendly application

Command & [e]Mission Control: Using Command and Event Buses to create a CQRS-friendly application

My first talk at the PHP UK Conference 2018 on Command buses and Event Dispatchers.

Barney Hanlon

February 15, 2018
Tweet

Other Decks in Programming

Transcript

  1. Command AND
    [e]Mission Control
    Barney Hanlon
    [Massive Battlestar Galactica Fan]

    View Slide

  2. Firstl
    y…

    View Slide

  3. Celebrating ten years
    at PHP UK Conference!

    View Slide

  4. Thanks to PHP SW for the warm up!
    @phpsw

    View Slide

  5. What do I do?

    View Slide

  6. View Slide

  7. Foreign currency and payment specialists
    60+ IT (developers, DevOps, infrastructure) working
    on our products
    Guess what?

    View Slide

  8. We’re hiring!

    View Slide

  9. View Slide

  10. The Problem(s)
    Huge growth led to a huge monolith
    Finance, especially FinTech, is a rapidly changing market
    New threats mean changes in compliance and security

    View Slide

  11. View Slide

  12. [Some] Solutions
    Domain-Driven Design approach
    Move to micro(ish) services
    TDD and BDD everywhere

    View Slide

  13. Probably nothing new…

    View Slide

  14. Domain logic is changing constantly
    Spotify “model” is improving agility but frameworks/
    libraries diverging across squads
    We have a zillion internal endpoints on our APIs
    (Still Some) Problems

    View Slide

  15. ???

    View Slide

  16. All right stop
    Collaborate and listen

    View Slide

  17. V
    anilla Ice says…
    We’re not going to refactor all these working APIs
    just to use the same framework.
    We do need a common way to handle all these
    changing things though.
    What lets us plug in new functionality really easily?

    View Slide

  18. (Some) B etter Solutions
    Command buses to tell our domain that we want to do
    something.
    Event buses to inform our domain that something has
    been done.
    Action-Domain-Responder pattern to control the
    interaction between the framework and the command
    bus.

    View Slide

  19. B eing Commanding

    View Slide

  20. What’s a “Command”?
    Gang of Four talk about the Command Pattern.
    DDD refers to Commands within business processes.
    CQRS

    View Slide

  21. CQRS

    View Slide

  22. Command
    Query
    Responsibility
    Segregation

    View Slide

  23. –Martin Fowler
    “At its heart is the notion that you can use a
    different model to update information than
    the model you use to read information.”

    View Slide

  24. Command
    Pattern

    View Slide

  25. – Wikipedia
    “In object-oriented programming, the command
    pattern is a behavioral design pattern in which an
    object is used to encapsulate all information
    needed to perform an action or trigger an event
    at a later time.”

    View Slide

  26. – Wikipedia
    “This information includes the
    method name, the object that owns
    the method and values for the
    method parameters.”

    View Slide

  27. In other words…
    A (very) simple Data Transfer Object (DTO)
    Knows nothing about how it will be used
    Only knows enough to complete the command
    Operate as a Value Object in your Domain.

    View Slide

  28. View Slide

  29. View Slide

  30. Command Buses

    View Slide

  31. – The League of Extraordinary Packages
    “The term is mostly used when we combine
    the Command pattern with a service layer.”

    View Slide

  32. – The League of Extraordinary Packages
    “Its job is to take a Command object (which describes what
    the user wants to do) and match it to a Handler (which
    executes it). This can help structure your code neatly.”

    View Slide

  33. Tactician league/tactician
    Broadway broadway/broadway
    Command Bus Libraries

    View Slide

  34. View Slide

  35. View Slide

  36. Event Buses [Emission]

    View Slide

  37. Drupal started with “hooks” - simple named pub/sub
    Zend Framework (0.x - 1.x) had the “Dispatch cycle”
    SPL Observer/Subject for implementing Observer pattern
    Symfony Event Dispatcher (et al)
    Bounce (coming soon)
    The Evolution of Event buses in PHP

    View Slide

  38. View Slide

  39. View Slide

  40. View Slide

  41. Comparing Commands and Events

    View Slide

  42. Commands and Events are both simple DTOs
    Beautifully dumb
    Should be self-validating
    Commands know (or infer) the one to one relationship
    with a single handler
    Comparing Commands and Events

    View Slide

  43. Know nothing about the mechanism
    Only know about their Commands/Events
    Extremely easy to test
    Command bus abstracts the Command-Handler relationship
    Event bus manages the many-to-many Event/Listener
    relationship
    Command Handlers AND Event Listeners

    View Slide

  44. Hexagonal Architecture
    “Ports & Adapters”

    View Slide

  45. – Alistair Cockburn
    “Allow an application to equally be driven by users,
    programs, automated test or batch scripts, and to be
    developed and tested in isolation from its eventual
    run-time devices and databases.”

    View Slide

  46. Credit: Fabricio Epaminondas
    Hexagonal architecture for Java applications

    View Slide

  47. –Alistair Cockburn
    “The application is blissfully ignorant
    of the nature of the input device.”

    View Slide

  48. What is an adapter?
    HTTP
    Command Line
    Queue Listener
    Testing framework

    View Slide

  49. Using the
    Command Pattern

    View Slide

  50. Web HTTP Controller
    HTTP Adapter Domain
    Command Bus
    Using the Command Pattern

    View Slide

  51. Why bother with another
    level of abstraction?

    View Slide

  52. “Take as long as you need to understand your
    domain logic before building a working product. Your
    product can change, your logic shouldn’t.”
    – No CEO ever

    View Slide

  53. Apps without buses

    View Slide

  54. View Slide

  55. View Slide

  56. Downsides
    Code duplication is exponential with the number of
    services involved.
    HTTP Controllers and console are inherently knowing
    more about your domain through endpoint names.
    Infers you know your Domain upfront.

    View Slide

  57. EXAMPLE:Domain
    relativel
    y unexplored

    View Slide

  58. View Slide

  59. Adv
    ant
    ages
    The Controller’s single responsibility is turning a Request
    into a Command
    Command Bus easily mocked to test HTTP adapter.
    Interactions between services within the Domain
    abstracted away.
    Can write the HTTP side of the app without knowing any
    domain services

    View Slide

  60. Application Flow

    View Slide

  61. Do/Done Cycle
    Console/Controller as Adapter
    Create Command - “Do Something”
    Command Bus
    Command Handler
    Create Event - “Something was Done”
    Event Bus

    View Slide

  62. View Slide

  63. View Slide

  64. View Slide

  65. Command Event
    Command
    Command
    Command
    Event
    Event
    Event
    Command
    Command
    Command
    Event
    Event
    Event

    View Slide

  66. Action
    Domain
    Responder

    View Slide

  67. Action-Domain-Responder
    First heard of it via the Slim framework
    documentation
    “Web-specific refinement of the MVC pattern”
    More suited to the closure-based micro frameworks
    https://github.com/pmjones/adr

    View Slide

  68. T
    aking ADR
    Further
    Command/Event buses to perform the
    interactions

    View Slide

  69. Request

    View Slide

  70. Request Action

    View Slide

  71. View Slide

  72. Request Command
    Action

    View Slide

  73. Command
    Bus
    Request Command
    Action

    View Slide

  74. View Slide

  75. Command
    Bus
    Request Command Handler
    Action

    View Slide

  76. Command
    Bus
    Request Command Service
    Handler
    Action

    View Slide

  77. Command
    Bus
    Request Command Service
    Handler
    Service
    Service
    Action

    View Slide

  78. View Slide

  79. Command
    Bus
    Request Command Service
    Handler
    Service
    Service
    Event
    Action

    View Slide

  80. View Slide

  81. Command
    Bus
    Request Command Service
    Handler
    Service
    Service
    Event
    Event Bus
    Action

    View Slide

  82. Command
    Bus
    Request Command Service
    Handler
    Service
    Service
    Event
    Listeners Event Bus
    Action

    View Slide

  83. View Slide

  84. Command
    Bus
    Request Command Service
    Handler
    Service
    Service
    Event
    Listeners Event Bus
    Action
    Responder

    View Slide

  85. Command
    Bus
    Request Command Service
    Handler
    Service
    Service
    Event
    Listeners
    Response Event Bus
    Action
    Responder

    View Slide

  86. View Slide

  87. Command
    Bus
    Request Command Service
    Handler
    Service
    Service
    Event
    Listeners
    Response Event Bus
    Action
    Responder

    View Slide

  88. View Slide

  89. Switch out the action/responder pair for any adapter
    Responder can be as smart or dumb as you like
    Multiple events can populate the Responder
    CQRS and Command/Events fit well together
    Self-validating DTOs allow you to “read your own
    writes”
    “Succeed or Throw” makes application logic simpler.

    View Slide

  90. @shrikeh
    https://github.com/shrikeh
    Thank you!

    View Slide

  91. Questions

    View Slide

  92. @shrikeh
    https://github.com/shrikeh
    SO SA
    Y WE ALL

    View Slide