Symfony Messenger
Symfony User Group, Zurich - March 4th, 2024
© David Buchmann
https://md.davidbu.ch l11pr0cks
Neuromancer, William Gibson
Symfony Messenger
Event | Message |
Blocking | Asynchronous |
Can alter behaviour | No influence |
Deterministic order | Undefined order (of consumers) |
Not scalable | Scalable |
Message System Components
- Message (payload application specific)
- Sender (Publisher, Producer)
- Receiver (Subscriber, Consumer)
- Channels (Consume only relevant messages)
Message System Advantages
- Decoupled (mix systems or languages)
- Scalable (add more producers or consumers)
- Buffering/load management (Async)
- Resilient (buffered when receivers are down)
- Reliable (acknowledgement and retry)
- Order (sequential if only 1 instance)
- Extensions (e.g. message priorities)
Why Symfony Messenger?
- High level functionality
- Abstract from specific message transport
- Nice and modern architecture
Retry
- In a large system, things will go wrong
- Software crashes, maschines reboot
- Network issues
- Service or system not available
- Actual logic bugs
- Message goes back to queue
- Exponential backoff
- Failure transport
Keep workers running
- Long-running PHP processes
- Careful about memory leaks.
- supervisord or similar to restart
- Rejuvenation: Restart after number of messsages, time or when memory consumption exceeds threshold
- Restart workers on deployment
Autoscaler
- Own service to start and stop workers
- Application registers its workers with autoscaler
- Autoscaler monitors queue sizes
- Spawns workers up to the limit when queue grows
Message Transport
- Without a configured transport, messages are processed immediately
- Doctrine (using a database table)
- AMQP with ext-amqp (rabbitmq, kafka)
- Redis with ext-redis
- Amazon SQS
- Plug your own, e.g. to call a custom API
Thank you!
github.com/dbu/messenger-demo/
@dbu@phpc.social
David Buchmann, Liip