Pro Yearly is on sale from $80 to $50! »

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.

8a2f83ee500e675e87b216d28ad7fb3e?s=128

Barney Hanlon

February 15, 2018
Tweet

Transcript

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

  2. Firstl y…

  3. Celebrating ten years at PHP UK Conference!

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

  5. What do I do?

  6. None
  7. Foreign currency and payment specialists 60+ IT (developers, DevOps, infrastructure)

    working on our products Guess what?
  8. We’re hiring!

  9. None
  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
  11. None
  12. [Some] Solutions Domain-Driven Design approach Move to micro(ish) services TDD

    and BDD everywhere
  13. Probably nothing new…

  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
  15. ???

  16. All right stop Collaborate and listen

  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?
  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.
  19. B eing Commanding

  20. What’s a “Command”? Gang of Four talk about the Command

    Pattern. DDD refers to Commands within business processes. CQRS
  21. CQRS

  22. Command Query Responsibility Segregation

  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.”
  24. Command Pattern

  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.”
  26. – Wikipedia “This information includes the method name, the object

    that owns the method and values for the method parameters.”
  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.
  28. None
  29. None
  30. Command Buses

  31. – The League of Extraordinary Packages “The term is mostly

    used when we combine the Command pattern with a service layer.”
  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.”
  33. Tactician league/tactician Broadway broadway/broadway Command Bus Libraries

  34. None
  35. None
  36. Event Buses [Emission]

  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
  38. None
  39. None
  40. None
  41. Comparing Commands and Events

  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
  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
  44. Hexagonal Architecture “Ports & Adapters”

  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.”
  46. Credit: Fabricio Epaminondas Hexagonal architecture for Java applications

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

    of the input device.”
  48. What is an adapter? HTTP Command Line Queue Listener Testing

    framework
  49. Using the Command Pattern

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

    Command Pattern
  51. Why bother with another level of abstraction?

  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
  53. Apps without buses

  54. None
  55. None
  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.
  57. EXAMPLE:Domain relativel y unexplored

  58. None
  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
  60. Application Flow

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

    Command Bus Command Handler Create Event - “Something was Done” Event Bus
  62. None
  63. None
  64. None
  65. Command Event Command Command Command Event Event Event Command Command

    Command Event Event Event
  66. Action Domain Responder

  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
  68. T aking ADR Further Command/Event buses to perform the interactions

  69. Request

  70. Request Action

  71. None
  72. Request Command Action

  73. Command Bus Request Command Action

  74. None
  75. Command Bus Request Command Handler Action

  76. Command Bus Request Command Service Handler Action

  77. Command Bus Request Command Service Handler Service Service Action

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

  80. None
  81. Command Bus Request Command Service Handler Service Service Event Event

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

    Event Bus Action
  83. None
  84. Command Bus Request Command Service Handler Service Service Event Listeners

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

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

    Response Event Bus Action Responder
  88. None
  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.
  90. @shrikeh https://github.com/shrikeh Thank you!

  91. Questions

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