It’s all about eXperience
Gaetano Giunta
Gilles Guirand
Forum PHP
Paris
2015/11/24
Rabbits, indians and...
Symfony meets
queueing brokers
Brokers, don’t they work in Wall Street?
Brokers, don’t they work in Wall Street?
Message brokers are a building block of Message oriented middleware.
[wikipedia]
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]
• RabbitMQ
• ActiveMQ
• Apollo
• Kafka
• Kestrel
• Amazon SQS
• ZeroMQ*
• And many more…
Let’s try again: can you name any broker?
What do they do?
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
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
Everything clear so far?
Everything clear so far?
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?
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
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
TextBook scenario for introducing queues
Web
server
Consumer
Rabbit
MQ
Libre
Office
PHP
TextBook scenario for introducing queues
Web
server
Consumer
?
Rabbit
MQ
Libre
Office
PHP
TextBook scenario for introducing queues
Web
server
PHP
!!!
Rabbit
MQ
Libre
Office
PHP
TextBook scenario for introducing queues
Web
server
PHP
!
Rabbit
MQ
Libre
Office
PHP
TextBook scenario for introducing queues
Web
server
Symfony
!!!
Rabbit
MQ
Libre
Office
Symfony
TextBook scenario for introducing queues
Web
server
Symfony
!!!
Rabbit
MQ
Libre
Office
Symfony
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
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";
}
}
Long lived php processes ?
• Fatal errors
• Memory leaks
• Database connections might become broken
• Is it in your code or in a dependency?
A new hope
• kaliop/queueingbundle
• kaliop/queueingbundle-sqs
• kaliop/queueingbundle-stomp
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
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
Multiple protocol support
Kaliop Queueing Bundles
fusesource/stomp-phpaws/aws-sdk-php oldsound/rabbitmq
ActiveMQ ApolloSQS RabbitMQ
Application
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
Show me da code
old_sound_rabbit_mq:
connections:
default:
...
producers:
my_producer:
connection: default
exchange_options:
name: my_producer.exchange
type: topic
consumers:
my_consumer:
connection: default
exchange_options:
name: my_producer.exchange
type: topic
queue_options:
name: my_queue
callback: my_symphony_service
Show me da code
Worker
use KaliopQueueingBundleServiceMessageConsumer;
class MyConsumer extends MessageConsumer
{
/**
* @param mixed $body
*/
public function consume($body)
{
// do stuff ...
}
}
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>]
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
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
It’s all about eXperience
Thanks for
listening
The end

Rabbits, indians and... Symfony meets queueing brokers

  • 1.
    It’s all abouteXperience Gaetano Giunta Gilles Guirand Forum PHP Paris 2015/11/24 Rabbits, indians and... Symfony meets queueing brokers
  • 2.
    Brokers, don’t theywork in Wall Street?
  • 3.
    Brokers, don’t theywork in Wall Street? Message brokers are a building block of Message oriented middleware. [wikipedia]
  • 4.
    Brokers, don’t theywork 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?
  • 6.
  • 7.
    Where do theydiffer? • 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 theydiffer? • 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
  • 9.
  • 10.
  • 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 forintroducing queues Web server Consumer Rabbit MQ Libre Office PHP
  • 15.
    TextBook scenario forintroducing queues Web server Consumer ? Rabbit MQ Libre Office PHP
  • 16.
    TextBook scenario forintroducing queues Web server PHP !!! Rabbit MQ Libre Office PHP
  • 17.
    TextBook scenario forintroducing queues Web server PHP ! Rabbit MQ Libre Office PHP
  • 18.
    TextBook scenario forintroducing queues Web server Symfony !!! Rabbit MQ Libre Office Symfony
  • 19.
    TextBook scenario forintroducing queues Web server Symfony !!! Rabbit MQ Libre Office Symfony
  • 20.
    The rationale • Samelibrary 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 abundle 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 phpprocesses ? • 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 focusingon 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
  • 26.
    Multiple protocol support KaliopQueueing Bundles fusesource/stomp-phpaws/aws-sdk-php oldsound/rabbitmq ActiveMQ ApolloSQS RabbitMQ Application
  • 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
  • 28.
    Show me dacode old_sound_rabbit_mq: connections: default: ... producers: my_producer: connection: default exchange_options: name: my_producer.exchange type: topic consumers: my_consumer: connection: default exchange_options: name: my_producer.exchange type: topic queue_options: name: my_queue callback: my_symphony_service
  • 29.
    Show me dacode Worker use KaliopQueueingBundleServiceMessageConsumer; class MyConsumer extends MessageConsumer { /** * @param mixed $body */ public function consume($body) { // do stuff ... } }
  • 30.
    Show me dacode 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 • Assumesgood 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 • Insteadof 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
  • 33.
    It’s all abouteXperience Thanks for listening The end

Editor's Notes

  • #3 Uke: read title
  • #4 Tori: read slide
  • #5 Tori: read slide Uke Q: sending and receiving messages between distributed systems, do you mean email ?
  • #7 Gilles : fifo ?
  • #9 Idea: make a feature matrix
  • #20 Q: polling ?