Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

Robert Bašić ~ PHPSrbija #29 A new project appears

Slide 3

Slide 3 text

Robert Bašić ~ PHPSrbija #29 Ready to build greatness

Slide 4

Slide 4 text

Robert Bašić ~ PHPSrbija #29 Your creation

Slide 5

Slide 5 text

Robert Bašić ~ PHPSrbija #29 // todo finish this

Slide 6

Slide 6 text

Robert Bašić ~ PHPSrbija #29 // refactor me later

Slide 7

Slide 7 text

Robert Bašić ~ PHPSrbija #29 // dirty hack to work around...

Slide 8

Slide 8 text

Robert Bašić ~ PHPSrbija #29 Same old story

Slide 9

Slide 9 text

Robert Bašić ~ PHPSrbija #29 Same old story ● An image gallery

Slide 10

Slide 10 text

Robert Bašić ~ PHPSrbija #29 Same old story ● An image gallery ● Multiple currencies

Slide 11

Slide 11 text

Robert Bašić ~ PHPSrbija #29 Same old story ● An image gallery ● Multiple currencies ● Coupons

Slide 12

Slide 12 text

Robert Bašić ~ PHPSrbija #29 Same old story ● An image gallery ● Multiple currencies ● Coupons ● Sales

Slide 13

Slide 13 text

Robert Bašić ~ PHPSrbija #29 Same old story ● An image gallery ● Multiple currencies ● Coupons ● Sales ● Reviews

Slide 14

Slide 14 text

Robert Bašić ~ PHPSrbija #29 Same old story ● An image gallery ● Multiple currencies ● Coupons ● Sales ● Reviews ● With images

Slide 15

Slide 15 text

Robert Bašić ~ PHPSrbija #29 Same old story ● An image gallery ● Multiple currencies ● Coupons ● Sales ● Reviews ● With images ● Old products

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Robert Bašić ~ PHPSrbija #29 Been there, done that

Slide 18

Slide 18 text

Robert Bašić ~ PHPSrbija #29 Command Query Responsibility Segregation

Slide 19

Slide 19 text

Robert Bašić ~ PHPSrbija #29 CQRS

Slide 20

Slide 20 text

Robert Bašić ~ PHPSrbija #29 CQRS

Slide 21

Slide 21 text

Robert Bašić ~ PHPSrbija #29 CQRS ● Use cases

Slide 22

Slide 22 text

Robert Bašić ~ PHPSrbija #29 CQRS ● Use cases ● Separation of concerns

Slide 23

Slide 23 text

Robert Bašić ~ PHPSrbija #29 CQRS ● Use cases ● Separation of concerns ● Improved readability

Slide 24

Slide 24 text

Robert Bašić ~ PHPSrbija #29 CQRS ● Use cases ● Separation of concerns ● Improved readability ● Improved performance

Slide 25

Slide 25 text

Robert Bašić ~ PHPSrbija #29 Before CQRS db->update('products', $product, ['id' => $product['id']]); } public function getProductById($productId) { /* ... */ } }

Slide 26

Slide 26 text

Robert Bašić ~ PHPSrbija #29 After CQRS (i) newPrice = Product\Price::fromString($newPrice, $currency); $this->product = $product; } public function newPrice() { return $this->newPrice; } public function product() { return $this->product; } }

Slide 27

Slide 27 text

Robert Bašić ~ PHPSrbija #29 After CQRS (ii) 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); } }

Slide 28

Slide 28 text

Robert Bašić ~ PHPSrbija #29 CQRS, the good

Slide 29

Slide 29 text

Robert Bašić ~ PHPSrbija #29 CQRS, the good ● Smaller classes

Slide 30

Slide 30 text

Robert Bašić ~ PHPSrbija #29 CQRS, the good ● Smaller classes ● Separated responsibilities

Slide 31

Slide 31 text

Robert Bašić ~ PHPSrbija #29 CQRS, the good ● Smaller classes ● Separated responsibilities ● Faster writes and reads

Slide 32

Slide 32 text

Robert Bašić ~ PHPSrbija #29 CQRS, the good ● Smaller classes ● Separated responsibilities ● Faster writes and reads ● Easier database queries

Slide 33

Slide 33 text

Robert Bašić ~ PHPSrbija #29 CQRS, the bad

Slide 34

Slide 34 text

Robert Bašić ~ PHPSrbija #29 CQRS, the bad ● More classes

Slide 35

Slide 35 text

Robert Bašić ~ PHPSrbija #29 CQRS, the bad ● More classes ● Translation

Slide 36

Slide 36 text

Robert Bašić ~ PHPSrbija #29 CQRS, the bad ● More classes ● Translation ● Complex syncing

Slide 37

Slide 37 text

Robert Bašić ~ PHPSrbija #29 CQRS, the bad ● More classes ● Translation ● Complex syncing ● Eventual consistency

Slide 38

Slide 38 text

Robert Bašić ~ PHPSrbija #29 Service Bus

Slide 39

Slide 39 text

Robert Bašić ~ PHPSrbija #29 What is a service bus?

Slide 40

Slide 40 text

Robert Bašić ~ PHPSrbija #29 Types of service buses

Slide 41

Slide 41 text

Robert Bašić ~ PHPSrbija #29 Types of service buses ● Message type

Slide 42

Slide 42 text

Robert Bašić ~ PHPSrbija #29 Types of service buses ● Message type ● Command bus

Slide 43

Slide 43 text

Robert Bašić ~ PHPSrbija #29 Types of service buses ● Message type ● Command bus ● Event bus

Slide 44

Slide 44 text

Robert Bašić ~ PHPSrbija #29 Types of service buses ● Message type ● Command bus ● Event bus ● Query bus

Slide 45

Slide 45 text

Robert Bašić ~ PHPSrbija #29 Service buses in PHP

Slide 46

Slide 46 text

Robert Bašić ~ PHPSrbija #29 Service buses in PHP ● Varying support for message types

Slide 47

Slide 47 text

Robert Bašić ~ PHPSrbija #29 Service buses in PHP ● Varying support for message types ● Tactician

Slide 48

Slide 48 text

Robert Bašić ~ PHPSrbija #29 Service buses in PHP ● Varying support for message types ● Tactician ● SimpleBus

Slide 49

Slide 49 text

Robert Bašić ~ PHPSrbija #29 Service buses in PHP ● Varying support for message types ● Tactician ● SimpleBus ● Prooph Service Bus

Slide 50

Slide 50 text

Robert Bašić ~ PHPSrbija #29 Command Bus

Slide 51

Slide 51 text

Robert Bašić ~ PHPSrbija #29 Command Bus

Slide 52

Slide 52 text

Robert Bašić ~ PHPSrbija #29 Commands

Slide 53

Slide 53 text

Robert Bašić ~ PHPSrbija #29 Commands ● Messages about user intention

Slide 54

Slide 54 text

Robert Bašić ~ PHPSrbija #29 Commands ● Messages about user intention ● CreateProduct, PutProductOnSale

Slide 55

Slide 55 text

Robert Bašić ~ PHPSrbija #29 Commands ● Messages about user intention ● CreateProduct, PutProductOnSale ● Name reveals use case

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

Robert Bašić ~ PHPSrbija #29 Dispatching commands, HTTP getProduct($request->get('productid')); $command = new UpdateProductPrice( $request->get('price'), $request->get('currency'), $product ); $this->commandBus->dispatch($command); } private function getProduct($productId): Model\Product }

Slide 58

Slide 58 text

Robert Bašić ~ PHPSrbija #29 Dispatching commands, CLI getProduct($input->get('productid')); $command = new UpdateProductPrice( $input->get('price'), $input->get('currency'), $product ); $this->commandBus->dispatch($command); } private function getProduct($productId): Model\Product }

Slide 59

Slide 59 text

Robert Bašić ~ PHPSrbija #29 A command is always valid newPrice = Product\Price::fromString($newPrice, $currency); $this->product = $product; } public function newPrice() { return $this->newPrice; } public function product() { return $this->product; } }

Slide 60

Slide 60 text

Robert Bašić ~ PHPSrbija #29 Command handlers 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); } }

Slide 61

Slide 61 text

Robert Bašić ~ PHPSrbija #29 Command buses in PHP

Slide 62

Slide 62 text

Robert Bašić ~ PHPSrbija #29 Command buses in PHP ● Tactician, SimpleBus, Prooph Service Bus

Slide 63

Slide 63 text

Robert Bašić ~ PHPSrbija #29 Command buses in PHP ● Tactician, SimpleBus, Prooph Service Bus ● Differ in creation and configuration

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

Robert Bašić ~ PHPSrbija #29 Event Bus

Slide 67

Slide 67 text

Robert Bašić ~ PHPSrbija #29 Event Bus

Slide 68

Slide 68 text

Robert Bašić ~ PHPSrbija #29 Events

Slide 69

Slide 69 text

Robert Bašić ~ PHPSrbija #29 Events ● Messages about past events

Slide 70

Slide 70 text

Robert Bašić ~ PHPSrbija #29 Events ● Messages about past events ● After a command was handled

Slide 71

Slide 71 text

Robert Bašić ~ PHPSrbija #29 Events ● Messages about past events ● After a command was handled ● ProductCreated, ProductPriceUpdated

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

Robert Bašić ~ PHPSrbija #29 Dispatching events repository->save($product); $event = new Product\Event\ProductPriceUpdated( (int) $product->id(), (double) $oldPrice, (double) $newPrice ); $this->eventBus->dispatch($event); } }

Slide 74

Slide 74 text

Robert Bašić ~ PHPSrbija #29 An event is always valid 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; } }

Slide 75

Slide 75 text

Robert Bašić ~ PHPSrbija #29 Event listeners oldPrice() <= $event->newPrice()) { return; } $product = $this→repository→get($productId); $command = new Product\Command\SendPriceDecreaseEmail($product); $this->commandBus->dispatch($command); } }

Slide 76

Slide 76 text

Robert Bašić ~ PHPSrbija #29 Event buses in PHP

Slide 77

Slide 77 text

Robert Bašić ~ PHPSrbija #29 Event buses in PHP ● SimpleBus, Prooph Service Bus

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

Robert Bašić ~ PHPSrbija #29 Event buses in PHP ● SimpleBus, Prooph Service Bus ● Differ in creation and configuration ● Similar usage

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

Robert Bašić ~ PHPSrbija #29 Query Bus

Slide 82

Slide 82 text

Robert Bašić ~ PHPSrbija #29 Query Bus

Slide 83

Slide 83 text

Robert Bašić ~ PHPSrbija #29 Queries

Slide 84

Slide 84 text

Robert Bašić ~ PHPSrbija #29 Queries ● Not the same as database queries

Slide 85

Slide 85 text

Robert Bašić ~ PHPSrbija #29 Queries ● Not the same as database queries ● A query is a question

Slide 86

Slide 86 text

Robert Bašić ~ PHPSrbija #29 Queries ● Not the same as database queries ● A query is a question ● LatestProductsCreated, ProductsOnSale

Slide 87

Slide 87 text

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

Slide 88

Slide 88 text

Robert Bašić ~ PHPSrbija #29 Dispatching queries get('from'), $request->get('to') ); $products = $this->queryBus->dispatch($query); // pass $products to template for displaying ... } }

Slide 89

Slide 89 text

Robert Bašić ~ PHPSrbija #29 A query is always valid from = new \DateTimeImmutable($from); $this->to = new \DatetTimeImmutable($to); } public function from(): \DatetTimeImmutable { return $this->from; } public function to(): \DatetTimeImmutable { return $this->to; } }

Slide 90

Slide 90 text

Robert Bašić ~ PHPSrbija #29 Query handlers productsBoughtReadModel->fetch($query->from(), $query->to()); } }

Slide 91

Slide 91 text

Robert Bašić ~ PHPSrbija #29 Query buses in PHP

Slide 92

Slide 92 text

Robert Bašić ~ PHPSrbija #29 Query buses in PHP ● Prooph Service Bus

Slide 93

Slide 93 text

Robert Bašić ~ PHPSrbija #29 Query buses in PHP ● Prooph Service Bus ● Plugins

Slide 94

Slide 94 text

Robert Bašić ~ PHPSrbija #29 Ready for the next project!

Slide 95

Slide 95 text

Robert Bašić ~ PHPSrbija #29 Resources ● CQRS Documents by Greg Young ● SimpleBus by Matthias Noback ● Prooph series by Robert Basic ● perfi by Robert Basic

Slide 96

Slide 96 text

Robert Bašić ~ PHPSrbija #29 Q&A Robert Bašić https://robertbasic.com/ @robertbasic

Slide 97

Slide 97 text

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