Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Follow the White Rabbit - Message Queues with PHP

Follow the White Rabbit - Message Queues with PHP

An introduction to message queues with PHP. We'll focus on RabbitMQ and how to leverage queuing scenarios in your applications. The talk will cover the main concepts of RabbitMQ server and AMQP protocol and show how to use it in PHP. The RabbitMqBundle for Symfony2 will be presented and we'll see how easy you can start to use message queuing in minutes.

Presented at Symfony User Group Belgium: http://www.meetup.com/Symfony-User-Group-Belgium/events/169953362/

Eric Rodriguez

March 26, 2014
Tweet

More Decks by Eric Rodriguez

Other Decks in Programming

Transcript

  1. About Me Eric Rodriguez Founder @ Data.be Founder @ Pictawall.com


    CTO @ Auctelia.com ! • Web entrepreneur • Multi-Language: PHP, Java/Groovy/Grails, .Net, … ! be.linkedin.com/in/erodriguez ! github.com/wavyx ! @wavyx
  2. RabbitMQ • Message Broker • Open Source • AMPQ implementation

    • Written in Erlang • Lots of AMQP client libraries • Wide community and commercial support
  3. Use Cases • Send notifications to users • Process heavy

    work/tasks with multiple workers
 Upload picture, make thumbnails, clear CDN caches… • Distribute logs/messages to multiple endpoints • Interoperability between different platforms • Remote Procedure Call
  4. AMQP
 Advanced Message Queuing Protocol • Open Standard Protocol coming

    from finance • Interoperable Messaging Middleware • Queuing • Routing (point-to-point and publish-and-subscribe) • Reliability • Security
  5. Exchanges • 4 Types - Direct, Fanout, Topic, Headers •

    Durability - Exchanges survive a restart • Auto-delete - Exchange deleted when queues empty
  6. Direct Exchange A Direct Exchange sends a message to a

    queue if the message's routing key is identical to the binding key for the queue
  7. Topic Exchange A Topic Exchange sends a message to a

    queue if the message's 
 routing key matches the binding key for the queue, using wildcard matching
  8. Alternatives Check http://queues.io/ • ZeroMQ - http://zeromq.org/ (ZMTP) • Beanstalkd

    - http://kr.github.io/beanstalkd/ (Job Server) • Gearman - http://gearman.org/ (Job Server) • HornetQ - http://www.jboss.org/hornetq/ (JMS/STOMP) • Apache Kafka -http://kafka.apache.org/ (PubSub) • Apache Qpid - http://qpid.apache.org/ (AMQP) • Apache ActiveMQ - http://activemq.apache.org/ (AMQP) • Apache Apollo - https://activemq.apache.org/apollo/ (AMQP)
  9. Cloud Alternatives • IronMQ
 http://www.iron.io/mq • Amazon SQS
 http://aws.amazon.com/sqs/ •

    StormMQ
 http://stormmq.com/ • Rackspace Queues
 http://www.rackspace.com/cloud/queues/ • Windows Azure Service Bus
 http://www.windowsazure.com/en-us/services/messaging/
  10. PHP Implementation • php-amqplib - pure php client
 https://github.com/videlalvaro/php-amqplib •

    PECL AMQP library - built on RabbitMQ C client
 http://pecl.php.net/package/amqp • amqphp - pure php client
 https://github.com/BraveSirRobin/amqphp • Thumper - library of messaging patterns
 https://github.com/videlalvaro/Thumper
  11. Work Queues / Task Queues • Avoid doing a resource-intensive

    task immediately • Schedule the task to be done later • Encapsulate a task as a message and send it to a queue • A worker process running in the background will execute the job • Multiple workers will share tasks
  12. Publish/Subscribe • Task queues: task is delivered to exactly one

    worker • Pub/Sub: deliver a message to multiple consumers • Published messages are going to be broadcast to all the receivers • Fanout exchanges
  13. Routing • Fanout => Direct exchange • Subscribe only to

    a subset of the messages • Filter messages based on their routing key
  14. Topics • Direct exchanges cannot do routing based on multiple

    criteria • Example: how to combine “severity” and “resource” • Topic exchange: messages are sent with a routing key composed by a list of words, delimited by dots • * (star) can substitute for exactly one word • # (hash) can substitute for zero or more words
  15. RPC • Work Queues = distribute time-consuming tasks among multiple

    workers • Remote Procedure Call or RPC = run a function on a remote computer and wait for the result
  16. RabbitMqBundle • Fast and Easy to use RabbitMQ with Symfony2

    • Use php-amqplib library • Provide messaging pattern (similar to Thumper)
  17. RabbitMqBundle • Producer / Consumer • Callback • RPC •

    Parallel RPC • Anonymous Consumer • STDIN Producer
  18. Resources • http://www.rabbitmq.com/getstarted.html • http://www.rabbitmq.com/how.html • RabbitMQ - AMQP concepts

    • RedHat - Messaging tutorial • http://www.slideshare.net/old_sound/theres-a-rabbit-on-my-symfony • http://www.slideshare.net/jasonlotito/php-rabbitmq-and-you • https://github.com/videlalvaro/rabbitmqbundle • http://reddikh.com/rabbitmq-bundle-in-symfony2/ • http://techportal.inviqa.com/2013/11/18/let-rabbitmq-do-the-work-in-your- symfony2-application/