Follow the White Rabbit - Message Queues with PHP

12,553 views
12,259 views

Published on

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/

Published in: Technology
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
12,553
On SlideShare
0
From Embeds
0
Number of Embeds
65
Actions
Shares
0
Downloads
20
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

Follow the White Rabbit - Message Queues with PHP

  1. 1. Message Queues with PHP
  2. 2. 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
  3. 3. Which Rabbit ?!
  4. 4. RabbitMQ • Message Broker • Open Source • AMPQ implementation • Written in Erlang • Lots of AMQP client libraries • Wide community and commercial support
  5. 5. Message Queues 101 • Producer = Sender • Consumer = Receiver • Queue = Mailbox
  6. 6. 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
  7. 7. 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
  8. 8. AMQP Model • Exchange • Queue • Routes
  9. 9. Exchanges • 4 Types - Direct, Fanout, Topic, Headers • Durability - Exchanges survive a restart • Auto-delete - Exchange deleted when queues empty
  10. 10. 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
  11. 11. Fanout Exchange A Fanout Exchange sends messages 
 to every queue bound to the exchange
  12. 12. 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
  13. 13. 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)
  14. 14. 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/
  15. 15. 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
  16. 16. Hello MQ World - send
  17. 17. Hello MQ World - receive
  18. 18. Hello MQ World
 queue_declare
  19. 19. Hello MQ World
 basic_publish & basic_consume
  20. 20. 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
  21. 21. Work Queues - new_task
  22. 22. Work Queues - worker
  23. 23. 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
  24. 24. Pub/Sub - emit_log
  25. 25. Pub/Sub - receive_logs
  26. 26. Routing • Fanout => Direct exchange • Subscribe only to a subset of the messages • Filter messages based on their routing key
  27. 27. Routing - emit_log_direct
  28. 28. Routing - receive_logs_direct
  29. 29. 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
  30. 30. Topics - emit_log_topic
  31. 31. Topics - receive_logs_topic
  32. 32. 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
  33. 33. RPC - rpc_client 1/2
  34. 34. RPC - rpc_client 2/2
  35. 35. RPC - rpc_server
  36. 36. • Work Queues • Publish/Subscribe • Routing • Topics • Remote Procedure Call (RPC)
  37. 37. RabbitMqBundle • Fast and Easy to use RabbitMQ with Symfony2 • Use php-amqplib library • Provide messaging pattern (similar to Thumper)
  38. 38. RabbitMqBundle - Setup
  39. 39. RabbitMqBundle - Config
  40. 40. RabbitMqBundle • Producer / Consumer • Callback • RPC • Parallel RPC • Anonymous Consumer • STDIN Producer
  41. 41. 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/
  42. 42. Books • RabbitMQ in Action
 http://manning.com/videla/ • RabbitMQ Cookbook
 http://www.packtpub.com/ rabbitmq-cookbook/book
  43. 43. Thank you! eric@data.be - @wavyx be.linkedin.com/in/erodriguez - github.com/wavyx http://www.meetup.com/Symfony-User-Group-Belgium/

×