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

All aboard the Service Bus @ ZGPHP

robertbasic
February 15, 2018

All aboard the Service Bus @ ZGPHP

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

February 15, 2018
Tweet

More Decks by robertbasic

Other Decks in Programming

Transcript

  1. Robert Bašić
    https://robertbasic.com/
    @robertbasic
    All aboard the Service Bus

    View full-size slide

  2. Robert Bašić ~ ZGPHP #78
    A new project appears

    View full-size slide

  3. Robert Bašić ~ ZGPHP #78
    Ready to build greatness

    View full-size slide

  4. Robert Bašić ~ ZGPHP #78
    Your creation

    View full-size slide

  5. Robert Bašić ~ ZGPHP #78
    // todo finish this

    View full-size slide

  6. Robert Bašić ~ ZGPHP #78
    // refactor me later

    View full-size slide

  7. Robert Bašić ~ ZGPHP #78
    // dirty hack to work around...

    View full-size slide

  8. Robert Bašić ~ ZGPHP #78
    Same old story

    View full-size slide

  9. Robert Bašić ~ ZGPHP #78
    Same old story
    ● An image gallery

    View full-size slide

  10. Robert Bašić ~ ZGPHP #78
    Same old story
    ● An image gallery
    ● Multiple currencies

    View full-size slide

  11. Robert Bašić ~ ZGPHP #78
    Same old story
    ● An image gallery
    ● Multiple currencies
    ● Coupons

    View full-size slide

  12. Robert Bašić ~ ZGPHP #78
    Same old story
    ● An image gallery
    ● Multiple currencies
    ● Coupons
    ● Sales

    View full-size slide

  13. Robert Bašić ~ ZGPHP #78
    Same old story
    ● An image gallery
    ● Multiple currencies
    ● Coupons
    ● Sales
    ● Reviews

    View full-size slide

  14. Robert Bašić ~ ZGPHP #78
    Same old story
    ● An image gallery
    ● Multiple currencies
    ● Coupons
    ● Sales
    ● Reviews
    ● With images

    View full-size slide

  15. Robert Bašić ~ ZGPHP #78
    Same old story
    ● An image gallery
    ● Multiple currencies
    ● Coupons
    ● Sales
    ● Reviews
    ● With images
    ● Old products

    View full-size slide

  16. Robert Bašić ~ ZGPHP #78
    Same old story
    ● An image gallery
    ● Multiple currencies
    ● Coupons
    ● Sales
    ● Reviews
    ● With images
    ● Old products
    ● Warehousing

    View full-size slide

  17. Robert Bašić ~ ZGPHP #78
    Been there, done that

    View full-size slide

  18. Robert Bašić ~ ZGPHP #78
    Command Query Responsibility
    Segregation

    View full-size slide

  19. Robert Bašić ~ ZGPHP #78
    CQRS

    View full-size slide

  20. Robert Bašić ~ ZGPHP #78
    CQRS

    View full-size slide

  21. Robert Bašić ~ ZGPHP #78
    CQRS
    ● Use cases

    View full-size slide

  22. Robert Bašić ~ ZGPHP #78
    CQRS
    ● Use cases
    ● Separation of concerns

    View full-size slide

  23. Robert Bašić ~ ZGPHP #78
    CQRS
    ● Use cases
    ● Separation of concerns
    ● Improved readability

    View full-size slide

  24. Robert Bašić ~ ZGPHP #78
    CQRS
    ● Use cases
    ● Separation of concerns
    ● Improved readability
    ● Improved performance

    View full-size slide

  25. Robert Bašić ~ ZGPHP #78
    Before CQRS
    namespace App\Service;
    class Product {
    public function update(array $product) {
    $presaveProduct = $this→getProductById($product['id']);
    if ($presaveProduct['sale'] == 1
    && $presaveProduct['price'] != $product['price']) {
    throw new \Exception("Can't update price of products!");
    }
    return $this->db->update('products', $product, ['id' => $product['id']]);
    }
    public function getProductById($productId) { /* ... */ }
    }

    View full-size slide

  26. Robert Bašić ~ ZGPHP #78
    After CQRS (i)
    namespace App\Product\Command;
    use App\Product;
    class UpdateProductPrice {
    public function __construct(string $newPrice,
    string $currency,
    Product\Product $product) {
    $this->newPrice = Product\Price::fromString($newPrice, $currency);
    $this->product = $product;
    }
    public function newPrice() { return $this->newPrice; }
    public function product() { return $this->product; }
    }

    View full-size slide

  27. Robert Bašić ~ ZGPHP #78
    After CQRS (ii)
    namespace App\Product\CommandHandler;
    use App\Product\Command;
    class UpdateProductPrice {
    public function handle(Command\UpdateProductPrice $command) {
    $product = $command->product();
    $newPrice = $command->newPrice();
    if ($product->onSale()) {
    throw new \Exception("Can't update price of products that are on sale!");
    }
    $product->updatePrice($newPrice);
    $this->repository->save($product);
    }
    }

    View full-size slide

  28. Robert Bašić ~ ZGPHP #78
    CQRS, the good

    View full-size slide

  29. Robert Bašić ~ ZGPHP #78
    CQRS, the good
    ● Smaller classes

    View full-size slide

  30. Robert Bašić ~ ZGPHP #78
    CQRS, the good
    ● Smaller classes
    ● Separated responsibilities

    View full-size slide

  31. Robert Bašić ~ ZGPHP #78
    CQRS, the good
    ● Smaller classes
    ● Separated responsibilities
    ● Faster writes and reads

    View full-size slide

  32. Robert Bašić ~ ZGPHP #78
    CQRS, the good
    ● Smaller classes
    ● Separated responsibilities
    ● Faster writes and reads
    ● Easier database queries

    View full-size slide

  33. Robert Bašić ~ ZGPHP #78
    CQRS, the bad

    View full-size slide

  34. Robert Bašić ~ ZGPHP #78
    CQRS, the bad
    ● More classes

    View full-size slide

  35. Robert Bašić ~ ZGPHP #78
    CQRS, the bad
    ● More classes
    ● Translation

    View full-size slide

  36. Robert Bašić ~ ZGPHP #78
    CQRS, the bad
    ● More classes
    ● Translation
    ● Complex syncing

    View full-size slide

  37. Robert Bašić ~ ZGPHP #78
    CQRS, the bad
    ● More classes
    ● Translation
    ● Complex syncing
    ● Eventual consistency

    View full-size slide

  38. Robert Bašić ~ ZGPHP #78
    Service Bus

    View full-size slide

  39. Robert Bašić ~ ZGPHP #78
    What is a service bus?

    View full-size slide

  40. Robert Bašić ~ ZGPHP #78
    Types of service buses

    View full-size slide

  41. Robert Bašić ~ ZGPHP #78
    Types of service buses
    ● Message type

    View full-size slide

  42. Robert Bašić ~ ZGPHP #78
    Types of service buses
    ● Message type
    ● Command bus

    View full-size slide

  43. Robert Bašić ~ ZGPHP #78
    Types of service buses
    ● Message type
    ● Command bus
    ● Event bus

    View full-size slide

  44. Robert Bašić ~ ZGPHP #78
    Types of service buses
    ● Message type
    ● Command bus
    ● Event bus
    ● Query bus

    View full-size slide

  45. Robert Bašić ~ ZGPHP #78
    Service buses in PHP

    View full-size slide

  46. Robert Bašić ~ ZGPHP #78
    Service buses in PHP
    ● Varying support for message types

    View full-size slide

  47. Robert Bašić ~ ZGPHP #78
    Service buses in PHP
    ● Varying support for message types
    ● Tactician

    View full-size slide

  48. Robert Bašić ~ ZGPHP #78
    Service buses in PHP
    ● Varying support for message types
    ● Tactician
    ● SimpleBus

    View full-size slide

  49. Robert Bašić ~ ZGPHP #78
    Service buses in PHP
    ● Varying support for message types
    ● Tactician
    ● SimpleBus
    ● Prooph Service Bus

    View full-size slide

  50. Robert Bašić ~ ZGPHP #78
    Command Bus

    View full-size slide

  51. Robert Bašić ~ ZGPHP #78
    Command Bus

    View full-size slide

  52. Robert Bašić ~ ZGPHP #78
    Commands

    View full-size slide

  53. Robert Bašić ~ ZGPHP #78
    Commands
    ● Messages about user intention

    View full-size slide

  54. Robert Bašić ~ ZGPHP #78
    Commands
    ● Messages about user intention
    ● CreateProduct, PutProductOnSale

    View full-size slide

  55. Robert Bašić ~ ZGPHP #78
    Commands
    ● Messages about user intention
    ● CreateProduct, PutProductOnSale
    ● Name reveals use case

    View full-size slide

  56. Robert Bašić ~ ZGPHP #78
    Commands
    ● Messages about user intention
    ● CreateProduct, PutProductOnSale
    ● Name reveals use case
    ● One command, one action

    View full-size slide

  57. Robert Bašić ~ ZGPHP #78
    Dispatching commands, HTTP
    class UpdatePriceAction {
    public function __invoke(Request $request) {
    $product = $this->getProduct($request->get('productid'));
    $command = new UpdateProductPrice(
    $request->get('price'),
    $request->get('currency'),
    $product
    );
    $this->commandBus->dispatch($command);
    }
    private function getProduct($productId): Model\Product
    }

    View full-size slide

  58. Robert Bašić ~ ZGPHP #78
    Dispatching commands, CLI
    class UpdatePriceCli {
    public function __invoke(Input $input) {
    $product = $this->getProduct($input->get('productid'));
    $command = new UpdateProductPrice(
    $input->get('price'),
    $input->get('currency'),
    $product
    );
    $this->commandBus->dispatch($command);
    }
    private function getProduct($productId): Model\Product
    }

    View full-size slide

  59. Robert Bašić ~ ZGPHP #78
    A command is always valid
    namespace App\Product\Command;
    use App\Product;
    class UpdateProductPrice {
    public function __construct(string $newPrice,
    string $currency,
    Product\Product $product) {
    $this->newPrice = Product\Price::fromString($newPrice, $currency);
    $this->product = $product;
    }
    public function newPrice() { return $this->newPrice; }
    public function product() { return $this->product; }
    }

    View full-size slide

  60. Robert Bašić ~ ZGPHP #78
    Command handlers
    namespace App\Product\CommandHandler;
    use App\Product\Command;
    class UpdateProductPrice {
    public function handle(Command\UpdateProductPrice $command) {
    $product = $command->product();
    $newPrice = $command->newPrice();
    if ($product->onSale()) {
    throw new \Exception("Can't update price of products that are on sale!");
    }
    $product->updatePrice($newPrice);
    $this->repository->save($product);
    }
    }

    View full-size slide

  61. Robert Bašić ~ ZGPHP #78
    Command buses in PHP

    View full-size slide

  62. Robert Bašić ~ ZGPHP #78
    Command buses in PHP
    ● Tactician, SimpleBus, Prooph Service Bus

    View full-size slide

  63. Robert Bašić ~ ZGPHP #78
    Command buses in PHP
    ● Tactician, SimpleBus, Prooph Service Bus
    ● Differ in creation and configuration

    View full-size slide

  64. Robert Bašić ~ ZGPHP #78
    Command buses in PHP
    ● Tactician, SimpleBus, Prooph Service Bus
    ● Differ in creation and configuration
    ● Similar usage

    View full-size slide

  65. Robert Bašić ~ ZGPHP #78
    Command buses in PHP
    ● Tactician, SimpleBus, Prooph Service Bus
    ● Differ in creation and configuration
    ● Similar usage
    ● Plugins, middlewares

    View full-size slide

  66. Robert Bašić ~ ZGPHP #78
    Event Bus

    View full-size slide

  67. Robert Bašić ~ ZGPHP #78
    Event Bus

    View full-size slide

  68. Robert Bašić ~ ZGPHP #78
    Events

    View full-size slide

  69. Robert Bašić ~ ZGPHP #78
    Events
    ● Messages about past events

    View full-size slide

  70. Robert Bašić ~ ZGPHP #78
    Events
    ● Messages about past events
    ● After a command was handled

    View full-size slide

  71. Robert Bašić ~ ZGPHP #78
    Events
    ● Messages about past events
    ● After a command was handled
    ● ProductCreated, ProductPriceUpdated

    View full-size slide

  72. Robert Bašić ~ ZGPHP #78
    Events
    ● Messages about past events
    ● After a command was handled
    ● ProductCreated, ProductPriceUpdated
    ● Once dispatched, can’t be stopped

    View full-size slide

  73. Robert Bašić ~ ZGPHP #78
    Dispatching events
    namespace App\Product\CommandHandler;
    use App\Product;
    class UpdateProductPrice {
    public function handle(Product\Command\UpdateProductPrice $command) {
    /** ... snip ... **/
    $this->repository->save($product);
    $event = new Product\Event\ProductPriceUpdated(
    (int) $product->id(),
    (double) $oldPrice,
    (double) $newPrice
    );
    $this->eventBus->dispatch($event);
    }
    }

    View full-size slide

  74. Robert Bašić ~ ZGPHP #78
    An event is always valid
    namespace App\Product\Event;
    class ProductPriceUpdated {
    public function __construct(int $productId, double $oldPrice, double $newPrice) {
    $this->productId = $productId;
    $this->oldPrice = $oldPrice;
    $this->newPrice = $newPrice;
    }
    public function productId() { return $this->productId; }
    public function oldPrice() { return $this->oldPrice; }
    public function newPrice() { return $this->newPrice; }
    }

    View full-size slide

  75. Robert Bašić ~ ZGPHP #78
    Event listeners
    namespace App\Product\EventListener;
    use App\Product;
    class NotifyAboutPriceDrop {
    public function handle(Product\Event\ProductPriceUpdated $event) {
    if ($event->oldPrice() <= $event->newPrice()) {
    return;
    }
    $product = $this→repository→get($productId);
    $command = new Product\Command\SendPriceDecreaseEmail($product);
    $this->commandBus->dispatch($command);
    }
    }

    View full-size slide

  76. Robert Bašić ~ ZGPHP #78
    Event buses in PHP

    View full-size slide

  77. Robert Bašić ~ ZGPHP #78
    Event buses in PHP
    ● SimpleBus, Prooph Service Bus

    View full-size slide

  78. Robert Bašić ~ ZGPHP #78
    Event buses in PHP
    ● SimpleBus, Prooph Service Bus
    ● Differ in creation and configuration

    View full-size slide

  79. Robert Bašić ~ ZGPHP #78
    Event buses in PHP
    ● SimpleBus, Prooph Service Bus
    ● Differ in creation and configuration
    ● Similar usage

    View full-size slide

  80. Robert Bašić ~ ZGPHP #78
    Event buses in PHP
    ● SimpleBus, Prooph Service Bus
    ● Differ in creation and configuration
    ● Similar usage
    ● Plugins, middlewares

    View full-size slide

  81. Robert Bašić ~ ZGPHP #78
    Query Bus

    View full-size slide

  82. Robert Bašić ~ ZGPHP #78
    Query Bus

    View full-size slide

  83. Robert Bašić ~ ZGPHP #78
    Queries

    View full-size slide

  84. Robert Bašić ~ ZGPHP #78
    Queries
    ● Not the same as database queries

    View full-size slide

  85. Robert Bašić ~ ZGPHP #78
    Queries
    ● Not the same as database queries
    ● A query is a question

    View full-size slide

  86. Robert Bašić ~ ZGPHP #78
    Queries
    ● Not the same as database queries
    ● A query is a question
    ● LatestProductsCreated, ProductsOnSale

    View full-size slide

  87. Robert Bašić ~ ZGPHP #78
    Queries
    ● Not the same as database queries
    ● A query is a question
    ● LatestProductsCreated, ProductsOnSale
    ● Answers from read models

    View full-size slide

  88. Robert Bašić ~ ZGPHP #78
    Dispatching queries
    class SalesReportAction {
    public function __invoke(Request $request) {
    $query = new ProductsBoughtInTimeframe(
    $request->get('from'),
    $request->get('to')
    );
    $products = $this->queryBus->dispatch($query);
    // pass $products to template for displaying ...
    }
    }

    View full-size slide

  89. Robert Bašić ~ ZGPHP #78
    A query is always valid
    namespace App\Product\Query;
    class ProductsBoughtInTimeframe {
    public function __construct(string $from, string $to) {
    $this->from = new \DateTimeImmutable($from);
    $this->to = new \DatetTimeImmutable($to);
    }
    public function from(): \DatetTimeImmutable {
    return $this->from;
    }
    public function to(): \DatetTimeImmutable {
    return $this->to;
    }
    }

    View full-size slide

  90. Robert Bašić ~ ZGPHP #78
    Query handlers
    namespace App\Product\QueryHandler;
    use App\Product\Query;
    class ProductsBoughtInTimeframe {
    public function handle(Query\ProductsBoughtInTimeframe $query) {
    return $this->productsBoughtReadModel->fetch($query->from(), $query->to());
    }
    }

    View full-size slide

  91. Robert Bašić ~ ZGPHP #78
    Query buses in PHP

    View full-size slide

  92. Robert Bašić ~ ZGPHP #78
    Query buses in PHP
    ● Prooph Service Bus

    View full-size slide

  93. Robert Bašić ~ ZGPHP #78
    Query buses in PHP
    ● Prooph Service Bus
    ● Plugins

    View full-size slide

  94. Robert Bašić ~ ZGPHP #78
    Ready for the next project!

    View full-size slide

  95. Robert Bašić ~ ZGPHP #78
    Resources
    ● CQRS Documents by Greg Young
    ● SimpleBus by Matthias Noback
    ● Prooph series by Robert Basic
    ● perfi by Robert Basic

    View full-size slide

  96. Robert Bašić ~ ZGPHP #78
    Q&A
    Robert Bašić
    https://robertbasic.com/
    @robertbasic

    View full-size slide

  97. Robert Bašić ~ ZGPHP #78
    Images used
    ● http://www.globalgeeknews.com/2011/12/22/the-life-of-a-so
    ftware-engineer-comic/

    View full-size slide