This document discusses using message brokers like RabbitMQ to introduce queues for processing document conversions. It describes how using a queue allows processing to be distributed across multiple consumers for better reliability and scalability compared to a single process approach. Specifically, it outlines how the Kaliop QueueingBundle for Symfony provides an easy way to integrate queues and supports multiple broker protocols with a common API.
3. Brokers, don’t they work in Wall Street?
Message brokers are a building block of Message oriented middleware.
[wikipedia]
4. Brokers, don’t they work in Wall Street?
Message brokers are a building block of Message oriented middleware.
[wikipedia]
Message oriented middleware (MOM) is software or hardware
infrastructure supporting the sending and receiving of messages
between distributed systems
[same as above]
5. • RabbitMQ
• ActiveMQ
• Apollo
• Kafka
• Kestrel
• Amazon SQS
• ZeroMQ*
• And many more…
Let’s try again: can you name any broker?
7. Where do they differ?
• Protocol support (amqp, stomp, …)
• Libraries exist for writing clients using standard protocols
• Other offer their own SDK
• Routing
• Amqp has exchanges and queues, Stomp only queues
• Wildcards in queue names are supported by most systems
• Delivery guarantees
• Possible double delivery?
• Out of order dispatch
8. Where do they differ?
• Surviving failures
• Are messages sent before the client is started buffered or lost?
• What happens when a client crashes?
• Support for ACK/NACK mechanisms
• Clustering
• And if the server crashes?
• Performances
• Batch send/receive – prefetch
• Polling vs. persistent connections
11. A case study: generating MSOffice docs
The needs
• Produce content in Microsoft Office formats
• Many formats for each document
• Both Word and Excel docs
• XML content generated by CMS
• LibreOffice used to generate MS Office and PDF versions
• Has anyone tried generating OOXML by hand?
12. A case study: generating MSOffice docs
Reality bites
• Slooow
• Rest assured that some of the docs will be pretty big
• Unreliable
• Depending on version, LO crashes from a-bit to
almost-always
• Race-prone
• Two conversion jobs in parallel step on each other
13. A case study: generating MSOffice docs
…and the results are:
• Ugly code: lots of polling, copying of files around, manual
locking
• Does not scale at all: only one conversion process active at
any given time
Web
server
PHP
Libre
Office
Waiting processes
14. TextBook scenario for introducing queues
Web
server
Consumer
Rabbit
MQ
Libre
Office
PHP
15. TextBook scenario for introducing queues
Web
server
Consumer
?
Rabbit
MQ
Libre
Office
PHP
16. TextBook scenario for introducing queues
Web
server
PHP
!!!
Rabbit
MQ
Libre
Office
PHP
17. TextBook scenario for introducing queues
Web
server
PHP
!
Rabbit
MQ
Libre
Office
PHP
18. TextBook scenario for introducing queues
Web
server
Symfony
!!!
Rabbit
MQ
Libre
Office
Symfony
19. TextBook scenario for introducing queues
Web
server
Symfony
!!!
Rabbit
MQ
Libre
Office
Symfony
20. The rationale
• Same library handling both sides of the connection
• Easier to troubleshoot
• Sf Console component is lovely
• Everything developed in a single repository
• No need to learn new languages
• Everything which I can rewrite in PHP, I eventually will*
* = 3rd whimsical law of Gaetano
21. There is a bundle for that
oldsound/rabbitmq-bundle
• specific to RabbitMQ
• supports both producers and consumers
php app/console rabbitmq:consumer <name>
class FooConsumer implements ConsumerInterface
{
public function execute(AMQPMessage $msg)
{
$foo = unserialize($msg->body);
echo 'foo '.$foo->getName()." successfully downloaded!n";
}
}
22. Long lived php processes ?
• Fatal errors
• Memory leaks
• Database connections might become broken
• Is it in your code or in a dependency?
23. A new hope
• kaliop/queueingbundle
• kaliop/queueingbundle-sqs
• kaliop/queueingbundle-stomp
24. A design focusing on safety
• The consumer has been split in 2
• The listener is long lived; it spins up a php worker
process each time it receives a command
• The worker process is a Symfony console command
• It handles a single message then exits
Symfony
listener
Symfony
worker
Libre
Office
Queue
25. Does it scale ?
• The listener waits for the worker to finish before staring another
• Solution: run multiple listeners in parallel
W
Symfony
listener
Symfony
worker
Libre
Office
Queue
Symfony
listener
Symfony
worker
Libre
Office
27. Easy to use
• Message encoding taken care of (JSON by default)
• Has events your code can listen to alter the consumption
loop
Message
received
Do stuff
Consump
tion
failed
Message
consumed
Exception
29. Show me da code
Worker
use KaliopQueueingBundleServiceMessageConsumer;
class MyConsumer extends MessageConsumer
{
/**
* @param mixed $body
*/
public function consume($body)
{
// do stuff ...
}
}
30. Show me da code
Lots of console commands available
php app/console kaliop_queueing:queuemessage <queuename> <msg body>
php app/console kaliop_queueing:consumer <queuename> [-i<driver>]
php app/console kaliop_queueing:managedriver list
php app/console kaliop_queueing:managequeue list-configured [-i<driver>]
php app/console kaliop_queueing:managequeue info <queuename> [-i<driver>]
31. Known limitations
• Assumes good security on the network
• Low throughput
• Spinning up a new php worker process takes time
• Specific features of the different protocols are not (yet) supported
• The goal remains to have a uniform API
32. Further evolution
• Instead of spinning off a console command for each message
received, send an http call to a php-fpm process
• Support for RPC-style calls
• Others?
• Suggestions are welcome
Symfony
listener
Symfony
worker
Libre
Office
Queue
HTTP
Web server: php
daemon