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 full-size slide

  2. Celebrating ten years
    at PHP UK Conference!

    View full-size slide

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

    View full-size slide

  4. What do I do?

    View full-size slide

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

    View full-size slide

  6. We’re hiring!

    View full-size slide

  7. 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 full-size slide

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

    View full-size slide

  9. Probably nothing new…

    View full-size slide

  10. 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 full-size slide

  11. All right stop
    Collaborate and listen

    View full-size slide

  12. 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 full-size slide

  13. (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 full-size slide

  14. B eing Commanding

    View full-size slide

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

    View full-size slide

  16. Command
    Query
    Responsibility
    Segregation

    View full-size slide

  17. –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 full-size slide

  18. Command
    Pattern

    View full-size slide

  19. – 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 full-size slide

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

    View full-size slide

  21. 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 full-size slide

  22. Command Buses

    View full-size slide

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

    View full-size slide

  24. – 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 full-size slide

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

    View full-size slide

  26. Event Buses [Emission]

    View full-size slide

  27. 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 full-size slide

  28. Comparing Commands and Events

    View full-size slide

  29. 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 full-size slide

  30. 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 full-size slide

  31. Hexagonal Architecture
    “Ports & Adapters”

    View full-size slide

  32. – 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 full-size slide

  33. Credit: Fabricio Epaminondas
    Hexagonal architecture for Java applications

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  36. Using the
    Command Pattern

    View full-size slide

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

    View full-size slide

  38. Why bother with another
    level of abstraction?

    View full-size slide

  39. “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 full-size slide

  40. Apps without buses

    View full-size slide

  41. 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 full-size slide

  42. EXAMPLE:Domain
    relativel
    y unexplored

    View full-size slide

  43. 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 full-size slide

  44. Application Flow

    View full-size slide

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

    View full-size slide

  46. Command Event
    Command
    Command
    Command
    Event
    Event
    Event
    Command
    Command
    Command
    Event
    Event
    Event

    View full-size slide

  47. Action
    Domain
    Responder

    View full-size slide

  48. 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 full-size slide

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

    View full-size slide

  50. Request Action

    View full-size slide

  51. Request Command
    Action

    View full-size slide

  52. Command
    Bus
    Request Command
    Action

    View full-size slide

  53. Command
    Bus
    Request Command Handler
    Action

    View full-size slide

  54. Command
    Bus
    Request Command Service
    Handler
    Action

    View full-size slide

  55. Command
    Bus
    Request Command Service
    Handler
    Service
    Service
    Action

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  62. 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 full-size slide

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

    View full-size slide

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

    View full-size slide