Slide 1

Slide 1 text

Message Queues with PHP

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Which Rabbit ?!

Slide 4

Slide 4 text

RabbitMQ • Message Broker • Open Source • AMPQ implementation • Written in Erlang • Lots of AMQP client libraries • Wide community and commercial support

Slide 5

Slide 5 text

Message Queues 101 • Producer = Sender • Consumer = Receiver • Queue = Mailbox

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

AMQP Model • Exchange • Queue • Routes

Slide 9

Slide 9 text

Exchanges • 4 Types - Direct, Fanout, Topic, Headers • Durability - Exchanges survive a restart • Auto-delete - Exchange deleted when queues empty

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Fanout Exchange A Fanout Exchange sends messages 
 to every queue bound to the exchange

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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)

Slide 14

Slide 14 text

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/

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Hello MQ World - send

Slide 17

Slide 17 text

Hello MQ World - receive

Slide 18

Slide 18 text

Hello MQ World
 queue_declare

Slide 19

Slide 19 text

Hello MQ World
 basic_publish & basic_consume

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Work Queues - new_task

Slide 22

Slide 22 text

Work Queues - worker

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Pub/Sub - emit_log

Slide 25

Slide 25 text

Pub/Sub - receive_logs

Slide 26

Slide 26 text

Routing • Fanout => Direct exchange • Subscribe only to a subset of the messages • Filter messages based on their routing key

Slide 27

Slide 27 text

Routing - emit_log_direct

Slide 28

Slide 28 text

Routing - receive_logs_direct

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Topics - emit_log_topic

Slide 31

Slide 31 text

Topics - receive_logs_topic

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

RPC - rpc_client 1/2

Slide 34

Slide 34 text

RPC - rpc_client 2/2

Slide 35

Slide 35 text

RPC - rpc_server

Slide 36

Slide 36 text

• Work Queues • Publish/Subscribe • Routing • Topics • Remote Procedure Call (RPC)

Slide 37

Slide 37 text

RabbitMqBundle • Fast and Easy to use RabbitMQ with Symfony2 • Use php-amqplib library • Provide messaging pattern (similar to Thumper)

Slide 38

Slide 38 text

RabbitMqBundle - Setup

Slide 39

Slide 39 text

RabbitMqBundle - Config

Slide 40

Slide 40 text

RabbitMqBundle • Producer / Consumer • Callback • RPC • Parallel RPC • Anonymous Consumer • STDIN Producer

Slide 41

Slide 41 text

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/

Slide 42

Slide 42 text

Books • RabbitMQ in Action
 http://manning.com/videla/ • RabbitMQ Cookbook
 http://www.packtpub.com/ rabbitmq-cookbook/book

Slide 43

Slide 43 text

Thank you! [email protected] - @wavyx be.linkedin.com/in/erodriguez - github.com/wavyx http://www.meetup.com/Symfony-User-Group-Belgium/