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

All aboard the Service Bus!

All aboard the Service Bus!

We deal with complicated and complex applications on a daily basis, codebases that are filled with classes that do too many things. One design pattern that can help us with this is CQRS, Command Query Responsibility Seggregation. The idea behind CQRS is to split our models in two - the Command for writing, and the Query for reading. Applying CQRS can lead us to a more maintainable code, code that follows the SOLID principles more closely.

At the heart of CQRS lies the Service Bus - a transport mechanism responsible for dispatching our command, event, and query messages to their destinations.

This talk will give an overview of the CQRS pattern and take a closer look at the different types of service buses - command, event, and query ones. Main takeaway will be practical information on why, when, and how to use them, with emphasis on their differences. We'll also take a look at some of the PHP libraries out there that help us work with service buses like Prooph Service Bus, Simple Bus, Tactician, to name a few.

robertbasic

June 08, 2018
Tweet

More Decks by robertbasic

Other Decks in Programming

Transcript

  1. All aboard the Service Bus!
    All aboard the Service Bus!
    Robert Basic
    Robert Basic
    @robertbasic
    @robertbasic
    https:/
    /robertbasic.com/
    https:/
    /robertbasic.com/
    1

    View Slide

  2. PHP
    PHP
    Mockery
    Mockery
    Go
    Go
    Writing
    Writing
    Board games
    Board games
    D&D
    D&D
    2

    View Slide

  3. Photo by Mike Erskine
    An introduction
    An introduction
    CQS & CQRS
    CQS & CQRS
    Service buses
    Service buses
    Command buses
    Command buses
    Event buses
    Event buses
    Query buses
    Query buses
    Recap
    Recap
    3

    View Slide

  4. Been there.
    Been there.
    Done that.
    Done that.
    4

    View Slide

  5. CQS
    CQS
    CQRS
    CQRS
    Command-query separation
    Command Query Responsibility Segregation
    5

    View Slide

  6. 6

    View Slide

  7. Before CQRS
    Before CQRS
    7

    View Slide

  8. After CQRS I
    After CQRS I
    8

    View Slide

  9. After CQRS II
    After CQRS II
    9

    View Slide

  10. Smaller classes
    Smaller classes
    Separated responsibilities
    Separated responsibilities
    Faster writes and reads
    Faster writes and reads
    Easier database queries
    Easier database queries
    Photo by Aziz Acharki
    10

    View Slide

  11. More classes
    More classes
    Translation
    Translation
    Complex syncing
    Complex syncing
    Eventual consistency
    Eventual consistency
    Photo by Daniel Páscoa
    11

    View Slide

  12. Service Bus
    Service Bus
    Photo by Nick Baker
    12

    View Slide

  13. 13

    View Slide

  14. Message type support
    Message type support
    Tactician
    Tactician
    SimpleBus
    SimpleBus
    Prooph Service Bus
    Prooph Service Bus
    Photo by Rob Allen
    14

    View Slide

  15. 15

    View Slide

  16. Commands
    Commands
    Messages about user intention
    Messages about user intention
    CreateProduct, PutProductOnSale
    CreateProduct, PutProductOnSale
    Name reveals use case
    Name reveals use case
    One command, one action
    One command, one action
    16

    View Slide

  17. A command is always valid
    A command is always valid
    17

    View Slide

  18. Dispatching commands, HTTP
    Dispatching commands, HTTP
    18

    View Slide

  19. Dispatching commands,CLI
    Dispatching commands,CLI
    19

    View Slide

  20. Command handlers
    Command handlers
    20

    View Slide

  21. 21

    View Slide

  22. Events
    Events
    Messages about past events
    Messages about past events
    After a command was handled
    After a command was handled
    ProductCreated, ProductPriceUpdated
    ProductCreated, ProductPriceUpdated
    Once dispatched, can’t be stopped
    Once dispatched, can’t be stopped
    22

    View Slide

  23. An event holds primitives
    An event holds primitives
    23

    View Slide

  24. Dispatching events
    Dispatching events
    24

    View Slide

  25. Event listeners
    Event listeners
    25

    View Slide

  26. 26

    View Slide

  27. Queries
    Queries
    Not the same as database queries
    Not the same as database queries
    A query is a question
    A query is a question
    LatestProductsCreated, ProductsOnSale
    LatestProductsCreated, ProductsOnSale
    Answers from read models
    Answers from read models
    27

    View Slide

  28. A query is always valid
    A query is always valid
    28

    View Slide

  29. Dispatching queries
    Dispatching queries
    29

    View Slide

  30. Query handlers
    Query handlers
    30

    View Slide

  31. Take away
    Take away
    Photo by Ian Usher
    31

    View Slide

  32. ChimeraPHP
    ChimeraPHP
    https:/
    /github.com/chimeraphp
    https:/
    /github.com/chimeraphp
    https://gist.github.com/lcobucci/6c89a97ea9c11a6cccfc887afa15df81
    32

    View Slide

  33. Thank you!
    Thank you!
    Robert Basic
    Robert Basic
    @robertbasic
    @robertbasic
    https:/
    /robertbasic.com/
    https:/
    /robertbasic.com/
    https:/
    /joind.in/talk/5a9d7
    https:/
    /joind.in/talk/5a9d7
    33

    View Slide