Follow the White Rabbit - Message Queues with PHP

43
Message Queues with PHP

description

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/

Transcript of Follow the White Rabbit - Message Queues with PHP

Page 1: Follow the White Rabbit - Message Queues with PHP

Message Queues with PHP

Page 2: Follow the White Rabbit - Message Queues with PHP

About MeEric 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

Page 3: Follow the White Rabbit - Message Queues with PHP

Which Rabbit ?!

Page 4: Follow the White Rabbit - Message Queues with PHP

RabbitMQ

• Message Broker

• Open Source

• AMPQ implementation

• Written in Erlang

• Lots of AMQP client libraries

• Wide community and commercial support

Page 5: Follow the White Rabbit - Message Queues with PHP

Message Queues 101

• Producer = Sender

• Consumer = Receiver

• Queue = Mailbox

Page 6: Follow the White Rabbit - Message Queues with PHP

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

Page 7: Follow the White Rabbit - Message Queues with PHP

AMQPAdvanced Message Queuing Protocol

• Open Standard Protocol coming from finance

• Interoperable Messaging Middleware

• Queuing

• Routing (point-to-point and publish-and-subscribe)

• Reliability

• Security

Page 8: Follow the White Rabbit - Message Queues with PHP

AMQP Model• Exchange • Queue • Routes

Page 9: Follow the White Rabbit - Message Queues with PHP

Exchanges

• 4 Types - Direct, Fanout, Topic, Headers

• Durability - Exchanges survive a restart

• Auto-delete - Exchange deleted when queues empty

Page 10: Follow the White Rabbit - Message Queues with PHP

Direct ExchangeA Direct Exchange sends a message to a queue if the message's

routing key is identical to the binding key for the queue

Page 11: Follow the White Rabbit - Message Queues with PHP

Fanout ExchangeA Fanout Exchange sends messages

to every queue bound to the exchange

Page 12: Follow the White Rabbit - Message Queues with PHP

Topic ExchangeA Topic Exchange sends a message to a queue if the message's

routing key matches the binding key for the queue, using wildcard matching

Page 13: Follow the White Rabbit - Message Queues with PHP

AlternativesCheck 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)

Page 14: Follow the White Rabbit - Message Queues with PHP

Cloud Alternatives• IronMQ

http://www.iron.io/mq

• Amazon SQShttp://aws.amazon.com/sqs/

• StormMQhttp://stormmq.com/

• Rackspace Queueshttp://www.rackspace.com/cloud/queues/

• Windows Azure Service Bus http://www.windowsazure.com/en-us/services/messaging/

Page 15: Follow the White Rabbit - Message Queues with PHP

PHP Implementation

• php-amqplib - pure php client https://github.com/videlalvaro/php-amqplib

• PECL AMQP library - built on RabbitMQ C clienthttp://pecl.php.net/package/amqp

• amqphp - pure php client https://github.com/BraveSirRobin/amqphp

• Thumper - library of messaging patterns https://github.com/videlalvaro/Thumper

Page 16: Follow the White Rabbit - Message Queues with PHP

Hello MQ World - send

Page 17: Follow the White Rabbit - Message Queues with PHP

Hello MQ World - receive

Page 18: Follow the White Rabbit - Message Queues with PHP

Hello MQ Worldqueue_declare

Page 19: Follow the White Rabbit - Message Queues with PHP

Hello MQ Worldbasic_publish & basic_consume

Page 20: Follow the White Rabbit - Message Queues with PHP

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

Page 21: Follow the White Rabbit - Message Queues with PHP

Work Queues - new_task

Page 22: Follow the White Rabbit - Message Queues with PHP

Work Queues - worker

Page 23: Follow the White Rabbit - Message Queues with PHP

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

Page 24: Follow the White Rabbit - Message Queues with PHP

Pub/Sub - emit_log

Page 25: Follow the White Rabbit - Message Queues with PHP

Pub/Sub - receive_logs

Page 26: Follow the White Rabbit - Message Queues with PHP

Routing

• Fanout => Direct exchange

• Subscribe only to a subset of the messages

• Filter messages based on their routing key

Page 27: Follow the White Rabbit - Message Queues with PHP

Routing - emit_log_direct

Page 28: Follow the White Rabbit - Message Queues with PHP

Routing - receive_logs_direct

Page 29: Follow the White Rabbit - Message Queues with PHP

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

Page 30: Follow the White Rabbit - Message Queues with PHP

Topics - emit_log_topic

Page 31: Follow the White Rabbit - Message Queues with PHP

Topics - receive_logs_topic

Page 32: Follow the White Rabbit - Message Queues with PHP

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

Page 33: Follow the White Rabbit - Message Queues with PHP

RPC - rpc_client 1/2

Page 34: Follow the White Rabbit - Message Queues with PHP

RPC - rpc_client 2/2

Page 35: Follow the White Rabbit - Message Queues with PHP

RPC - rpc_server

Page 36: Follow the White Rabbit - Message Queues with PHP

• Work Queues

• Publish/Subscribe

• Routing

• Topics

• Remote Procedure Call (RPC)

Page 37: Follow the White Rabbit - Message Queues with PHP

RabbitMqBundle

• Fast and Easy to use RabbitMQ with Symfony2

• Use php-amqplib library

• Provide messaging pattern (similar to Thumper)

Page 38: Follow the White Rabbit - Message Queues with PHP

RabbitMqBundle - Setup

Page 39: Follow the White Rabbit - Message Queues with PHP

RabbitMqBundle - Config

Page 40: Follow the White Rabbit - Message Queues with PHP

RabbitMqBundle

• Producer / Consumer

• Callback

• RPC

• Parallel RPC

• Anonymous Consumer

• STDIN Producer

Page 41: Follow the White Rabbit - Message Queues with PHP

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/

Page 42: Follow the White Rabbit - Message Queues with PHP

Books• RabbitMQ in Action

http://manning.com/videla/• RabbitMQ Cookbook

http://www.packtpub.com/rabbitmq-cookbook/book

Page 43: Follow the White Rabbit - Message Queues with PHP

Thank [email protected] - @wavyx

be.linkedin.com/in/erodriguez - github.com/wavyx

http://www.meetup.com/Symfony-User-Group-Belgium/