Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm...

76
Mastering Message Queues Tobias Nyholm @tobiasnyholm @tobiasnyholm

Transcript of Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm...

Page 1: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

Mastering Message QueuesTobias Nyholm

@tobiasnyholm

@tobiasnyholm

Page 2: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

The data structureTop

Bottom

FrontBack

Page 3: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Why?

Page 4: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Tobias Nyholm• Full stack unicorn on Happyr.com

• Certified Symfony developer

• Symfony core member

• PHP-Stockholm

• Open source

Page 5: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Open source

PHP-cache

HTTPlugMailgun

LinkedIn API clientSwap

Stampie

BazingaGeocoderBundlePHP-Geocoder

FriendsOfApi/boilerplateGuzzle Buzz

CacheBundlePSR7

SymfonyBundleTest

NSA

SimpleBus integrations

PSR HTTP clients

Neo4j

KNP Github API

PHP-Translation

Puli

Assert

Backup-manager/symfony

php-http/httplug-bundle php-http/multipart-stream

php-http/discovery

happyr/normal-distribution-bundle

nyholm/effective-interest-rate

MailgunBundle

league/geotools

Page 6: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

2013

Page 7: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Page 8: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Page 9: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

<?php

namespace Webfish\CompanyBundle\Controller;

use Money\Money; use Rawls\BaseBundle\Controller\BaseController; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Webfish\CompanyBundle\Entity\Company; use Webfish\CompanyBundle\Entity\SaleImport; use Webfish\CompanyBundle\Form\SalesImportType; use Webfish\CompanyBundle\Model\SalesImportModel;

class ImportController extends BaseController { /** * @param Request $request * * @Route("/import/sales", name="sales_import") * @Template() * * @return array|RedirectResponse */ public function importAction(Request $request) {

Page 10: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding
Page 11: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

–Twitter

“Testing your controllers.”

Page 12: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Page 13: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding
Page 14: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Matthias Noback

Page 15: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Infrastructure

The world outside

Web browser

TerminalDatabase

Messaging

Filesystem(E)mail

(Matthias Noback)

Page 16: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding
Page 17: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Controller Command

Creates

CommandHandler

Command bus

Page 18: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

HTTP Request

Form

Request

Controller

Patient (entity)

Web p

ort

PatientRepository

RegisterPatient- Handler

RegisterPatient (command)

Infras

truc

ture

Applica

tion

Domain

(Matthias Noback)

Page 19: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Infrastructure

The world outside

Web browser

TerminalDatabase

Messaging

Filesystem(E)mail

(Matthias Noback)

Page 20: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Hexagonal architecture

https://www.youtube.com/watch?v=fgQWnglnGeU https://matthiasnoback.nl/2015/01/a-wave-of-command-buses/https://martinfowler.com/bliki/CQRS.htmlhttps://hexagonal-architecture.eu/

Page 21: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

What did we win?

Page 22: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Middlewares!

Page 23: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Controller Command

Creates

CommandHandler

Command bus

Page 24: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Controller Command

Creates

CommandHandler

Command bus

Queue

Page 25: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Let’s talk about queues

Page 26: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholmWork queue

Producer

Consumer A

Consumer B

Page 27: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Fanout

Producer

Consumer A

Consumer B

Binding

Binding

Publish/Subscribe

Page 28: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Direct

Producer

Consumer A

Consumer B

Foo

Baz

Bar

Routing

Page 29: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholmRouting

Direct

Producer

Consumer A

Consumer B

Foo

Foo

Page 30: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholmTopics

Topic

Producer

Consumer A

Consumer B

*.error

app.*

firewall.#

app.error

firewall.noticeapp.notice

Page 31: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Let’s see some code

Page 32: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

$ composer req symfony/messenger

Page 33: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Controller Message

Creates

Message handler

Message bus

Page 34: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm<?php

namespace App\Message\Command;

class SendNotification { private $message; private $users;

public function __construct(string $message, array $users) { $this->message = $message; $this->users = $users; }

public function getMessage(): string { return $this->message; }

public function getUsers(): array { return $this->users; } }

Page 35: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm<?php

namespace App\Controller;

use App\Message\Command\SendNotification; use Symfony\Component\Messenger\MessageBusInterface; // ...

class DefaultController { public function index(MessageBusInterface $bus, Request $request) { $users = ['samuel', 'christelle']; $message = $request->query->get('message', 'Something.');

$bus->dispatch(new SendNotification($message, $users));

return new Response('<html><body>OK.</body></html>'); } }

Page 36: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Page 37: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

<?php

namespace App\MessageHandler;

use App\Message\Command\SendNotification;

class SendNotificationHandler { public function __invoke(SendNotification $message) { foreach ($message->getUsers() as $user) { echo "Send notification to... ".$user."\n"; } } }

Page 38: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

# config/services.yaml services: App\Message\CommandHandler\SendNotificationHandler: tags: - messenger.message_handler

# — — — — — —

# config/services.yaml services: App\Message\CommandHandler\: resource: ‘../src/Message/CommandHandler/*’ tags: ['messenger.message_handler']

Page 39: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Page 40: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Message Message handler

Message bus

Message handlerdispatch handles

Worker

dispatc

h

TransportTransport

receive

send

Transports

Page 41: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Message handler

Message bus

Message handlerdispatch handles

Worker

dispatc

h

TransportTransport

receive

send

Message

Transports

• Sender

• Receiver

• Configurable with DSN

• Encode/Decode

Page 42: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

# config/packages/messenger.yaml framework: messenger: transports: default: '%env(MESSENGER_DSN)%'

routing: ‘App\Message\Command\SendNotification’: default

# -------------

# .env MESSENGER_DSN=amqp://guest:guest@localhost:5672/%2f/messages

Page 43: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Page 44: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

$ bin/console messenger:consume-messages

Page 45: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Page 46: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Page 47: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Message handler

Message bus

Message handlerdispatch handles

Worker

dispatc

h

TransportTransport

receive

send

Message

Page 48: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Page 49: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Message handler

Message bus

Message handlerdispatch handles

Worker

dispatc

h

TransportTransport

receive

send

Message

Page 50: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Transport

dispatch

Transport

send

Workerreceive

Message handlerhandles

Message handler

dispatch

App 1

TransportTransport Workerreceive

Message handler handles

Message handler

dispatch

App 2

MessageMessage

Page 51: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

# config/packages/messenger.yaml framework: messenger: transports: default: ‘%env(MESSENGER_DSN)%' enqueue: '%env(ENQUEUE_DSN)%'

routing: ‘App\Message\Command\SendNotification': [default, enqueue]

# — — — — — —

# .env MESSENGER_DSN=amqp://guest:guest@localhost:5672/%2f/messages ENQUEUE_DSN=enqueue://acme

Page 52: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Direct

Producer

Consumer A

Consumer B

Foo

Baz

Bar

TransportTransport Workerreceive

Message handler handles

Message handler

dispatch

App 2

Page 53: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Multiple busses

Page 54: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Multiple busses

Command Command Handler

Event

Query Query Handler

Event SubscriberEvent SubscriberEvent Subscriber

Page 55: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Multiple busses# config/packages/messenger.yaml framework: messenger: # The bus that is going to be injected when injecting MessageBusInterface: default_bus: messenger.bus.commands

# Create buses buses: messenger.bus.command: ~ messenger.bus.query: ~ messenger.bus.event: ~

Page 56: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Multiple busses# config/packages/messenger.yaml framework: messenger: # The bus that is going to be injected when injecting MessageBusInterface: default_bus: messenger.bus.commands

# Create buses buses: messenger.bus.command: middleware: - messenger.middleware.exactly_one_handler - messenger.middleware.validation - 'App\Middleware\EventStoreMiddleware' messenger.bus.query: middleware: - messenger.middleware.exactly_one_handler - messenger.middleware.validation messenger.bus.event: middleware: - messenger.middleware.allow_no_handler - messenger.middleware.validation

Page 57: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Multiple bussesservices: _defaults: # ...

bind: $commandBus: '@messenger.bus.command' $queryBus: '@messenger.bus.query' $eventBus: '@messenger.bus.event'

Page 58: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm<?php

namespace App\Message\QuaryHandler;

use App\Message\Quary\GetUserForNotification;

class GetUsersForNotificationHandler { public function __invoke(GetUserForNotification $query) { $users = $this->getUsers($query->getSubject());

return $users; }

private function getUsers(string $subject) { // TODO Decide upon $subject return ['tobias', ‘maria', 'samuel']; } }

Page 59: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Send events from handlerclass SendNotificationHandler { private $eventBus;

public function __construct(MessageBusInterface $eventBus) { $this->eventBus = $eventBus; }

public function __invoke(SendNotification $message) { foreach ($message->getUsers() as $user) { echo "Send notification to... ".$user."\n"; }

$this->eventBus->dispatch(new NotificationSent()); } }

Page 60: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Handle failure

Page 61: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Message handler

Message bus

Message handlerdispatch handles

Worker

dispatc

h

TransportTransport

receive

send

Requeue

Message

Page 62: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Error

Page 63: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Message handler

Message bus

Message handlerdispatch handles

Worker

dispatc

h

TransportTransport

receive

send

Requeue

Message

Page 64: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Create new entities

Page 65: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

<?php

namespace App\Controller;

use App\Message\SendNotification; use Symfony\Component\Messenger\MessageBusInterface; // ...

class UserController { public function create(MessageBusInterface $bus, Request $request) { $bus->dispatch(new CreateUser($request->request->get('username', '')));

return new Response('See /api/user/{user_id}’); } }

Page 66: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholmuse Symfony\Component\Validator\Constraints as Assert;

class CreateUser { /** * @Assert\Uuid() * @Assert\NotBlank() */ private $uuid; /** * @Assert\NotBlank() * @Assert\Length(min=3) */ private $username;

public function __construct(string $uuid, string $username) { $this->uuid = $uuid; $this->username = $username; }

public function getUuid(): string { return $this->uuid; }

public function getUsername(): string { return $this->username;

Page 67: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm<?php

namespace App\Controller;

use App\Message\SendNotification; use Symfony\Component\Messenger\MessageBusInterface; // ...

class UserController { public function create(MessageBusInterface $bus, Request $request) { $uuid = (string) Uuid::uuid4(); $bus->dispatch(new CreateUser($uuid, $request->request->get('username', '')));

return new Response('See /api/user/'.$uuid); } }

Page 68: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholmuse Symfony\Component\Validator\Constraints as Assert;

class CreateUser { /** * @Assert\Uuid() * @Assert\NotBlank() */ private $uuid; /** * @Assert\NotBlank() * @Assert\Length(min=3) */ private $username;

public function __construct(string $uuid, string $username) { $this->uuid = $uuid; $this->username = $username; }

public function getUuid(): string { return $this->uuid; }

public function getUsername(): string { return $this->username;

Page 69: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding
Page 70: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Cache everything

HTTP RequestThin app

Cache

TranspoTransport Workerreceive

Message handlesMessage handler

dispatch

GET

POST

Page 71: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Handle 1M requests / second

Thin app

Workers Workers

Workers

HTTP Request

Page 72: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Handle 1M requests / second

AWS Lambda

Workers Workers

Workers

HTTP Request

Page 73: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Questions?

Page 74: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Envelope

Message handler

Message bus

Message handlerdispatch handles

Worker

dispatc

h

TransportTransport

receive

send

Message

Page 75: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding

@tobiasnyholm

Questions?@tobiasnyholm

Page 76: Mastering Message Queues - nyholm.tech · Let’s talk about queues. Work queue @tobiasnyholm Producer Consumer A Consumer B. @tobiasnyholm Fanout Producer Consumer A Consumer B Binding