Decoupling an Application with

Symfony Messenger



ConFoo, Montreal, Canada - February 23th, 2024

© David Buchmann







David Buchmann - david@liip.ch

PHP Engineer, Liip SA, Switzerland


We built a data kraken

flickr.com/photos/w-tommerdich/32823758672

Product API
massive central index of product data

First approach

Encapsulate Logic


Extract the logic out of commands and controllers.


=> Refactored into Importer classes that can handle a single item at a time.

Decouple with message queue

Reaping the benefits


Messages within the application

e.g. Compile data into Elasticsearch

Flexible on the outside,
consistent on the inside


Any questions so far?




Demo Time!


github.com/dbu/messenger-demo





https://md.davidbu.ch


Neuromancer, William Gibson





Symfony Messenger

EventMessage
BlockingAsynchronous
Can alter behaviourNo influence
Deterministic orderUndefined order
(of consumers)
Not scalableScalable




Message System Components

Message System Advantages

Why Symfony Messenger?

The flow of Symfony Messenger

https://symfony.com/doc/current/components/messenger.html#concepts

Send Message

Receiving Message

Message

class UpdatePromotion
{
    public function __construct(
        public string $id
    ) {}
}

Sending a message

private MessageBusInterface $bus;
...
$this->bus->dispatch(new UpdatePromotion($id));
            

Stamps (Metadata)

$stamps[] = new DelayStamp(20);
$this->bus->dispatch(
    new UpdatePromotion($id),
    $stamps
);

Routing


framework:
  messenger:
    transports:
      neuromancer:
        dsn: '%env(MESSENGER_TRANSPORT_DSN)%&queue_name=neuromancer'

  routing:
    App\Message\NeuromancerMessage: neuromancer
            

Retry

Keep workers running

Autoscaler

What is going on?

Message Transport



Did i hear «microservices»?

Une Bataille, ~1750, François-Joseph Casanova

Messaging != microservices



Thank you!


github.com/dbu/messenger-demo/


@dbu@phpc.social

David Buchmann, Liip SA