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

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

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

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

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

    View full-size slide

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

    View full-size slide

  6. Before CQRS
    Before CQRS
    7

    View full-size slide

  7. After CQRS I
    After CQRS I
    8

    View full-size slide

  8. After CQRS II
    After CQRS II
    9

    View full-size slide

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

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

    View full-size slide

  11. Service Bus
    Service Bus
    Photo by Nick Baker
    12

    View full-size slide

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

    View full-size slide

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

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

    View full-size slide

  15. Dispatching commands, HTTP
    Dispatching commands, HTTP
    18

    View full-size slide

  16. Dispatching commands,CLI
    Dispatching commands,CLI
    19

    View full-size slide

  17. Command handlers
    Command handlers
    20

    View full-size slide

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

  19. An event holds primitives
    An event holds primitives
    23

    View full-size slide

  20. Dispatching events
    Dispatching events
    24

    View full-size slide

  21. Event listeners
    Event listeners
    25

    View full-size slide

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

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

    View full-size slide

  24. Dispatching queries
    Dispatching queries
    29

    View full-size slide

  25. Query handlers
    Query handlers
    30

    View full-size slide

  26. Take away
    Take away
    Photo by Ian Usher
    31

    View full-size slide

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

    View full-size slide

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