Decoupling your application using
Symfony Messenger and events
Hugo Monteiro
• Software Architect & Developer
• Co-founder of Spotahome
Twitter: @hmmonteiro
Github: @monteiro
Why?
“Why do we need to decouple our
application with Symfony Messenger
and Events”
Domain Driven Design
Domain Driven Design
“Domain-driven design (DDD) is
a software design
philosophy[1] that focuses on
understanding domain experts
and building the software as a
model of its domain.[2]"
Domain Driven Design
•Domain Experts
Domain Driven Design
•Ubiquitous Language
Domain Driven Design
• Bounded context
https://martinfowler.com/bliki/BoundedContext.html
Domain Driven Design
• Aggregate (root)
Example: Order + Line items is an aggregate root
Domain Driven Design
• Invariants are business rules
Example: Create user only if age >= 18
Domain Driven Design
Invariant example: admin users cannot be disabled
Domain Driven Design
• Domain Events
Example: UserWasCreated, OrderWasStarted, OrderWasShipped,
UserWasDisabled, BookingWasPaid, BookingWasCancelled, etc.
Domain events - How it works
Concepts Real world example
Domain driven design
• Challenge: How are entities saved in the database, if we
normally need an ID returned from the database?
Domain events - UUIDs
• Every time an aggregate is created, we need to know its id, so we can
publish the domain event with the aggregate root id (e.g. entity id).
• Libraries to generate UUIDs: Symfony UID component, Ramsey UUID
Domain events - UUIDs
Domain events - Publishing
• Domain events are published to the messaging systems
• What happens when there is a failure publishing domain events to the
messaging systems (e.g. Redis, Rabbitmq, SQS, etc).
Domain events publishing
Outbox pattern
Symfony Messenger
• How to decouple our application with Symfony Messenger
and domain events
Symfony Messenger
Symfony Messenger
CommandBus
Symfony Messenger
• Middleware
Symfony Messenger
Symfony Messenger
Symfony Messenger - Transports
• Redis, AWS SQS, RabbitMQ, Doctrine
• More transports:
• messenger-enqueue-transport
Symfony Messenger
• Now that we understand all the main concepts, let’s build something!
Implementation
• Let’s build a rent a car software
Implementation
Implementation
Implementation
Event Store (outbox)
Publish to the Transport
Publish to the Transport
Consuming from another service
Consuming from another service
Testing
Demo:
monteiro/symfonycon-winter-2021
Conclusions
Thank you
Twitter: @hmmonteiro
Github: @monteiro
References
• Photos:
• Photo by Alex Green from Pexels: https://www.pexels.com/photo/crop-faceless-female-counselor-talking-to-client-
5699486/
• Photo by Greta Hoffman from Pexels: https://www.pexels.com/photo/mature-gardeners-talking-in-greenhouse-7728658/
• Photo by Brett Jordan from Pexels: https://www.pexels.com/photo/brown-wooden-blocks-on-white-table-8953965/
• Microsoft Docs: Domain Events Design implementation
• Symfony Messenger Documentation
• Mathias Noback: Advanced Web Development Architecture
• Command Bus: https://matthiasnoback.nl/2015/01/a-wave-of-command-buses/
• Vaughn Vernon Implementing Domain Driven Design (red & blue)
• Neil Brooks: The absolute beginner's guide to Domain Driven Design with Symfony

Decoupling your application using Symfony Messenger and events