this slide is intentionally left blank
Before we begin...
Copy folder from USB to your local machine.
$ cd /path/to/the/folder/you/copied/from/usb/
$ vagrant box add asgrim/rmq-vm rmq-vm.box
$ vagrant up --no-provision
Test it in your browser...
http://192.168.33.99:15672/
@asgrim
Introducing
Practical RabbitMQ
James Titcumb
@asgrim
James Titcumb
www.jamestitcumb.com
www.roave.com
www.phphants.co.uk
www.phpsouthcoast.co.uk
@asgrim
Who is this guy?
What is a message?
@asgrim
What is message
queueing?
@asgrim
Photo: http://spitalfieldslife.com/2011/08/27/a-fox-in-hoxton-2/
Separation of Concerns
@asgrim
Scaling with Rabbit
RabbitMQApplication
Background processing
@asgrim
Scaling with Rabbit
RabbitMQApplication
Background processing
Background processing
@asgrim
Scaling with Rabbit
RabbitMQApplication
Background processing
Background processing
Background processing
@asgrim
Scaling with Rabbit
RabbitMQApplication
Background processing
Background processing
Background processing
Background processing
@asgrim
Scaling with Rabbit
RabbitMQApplication
Background processing
Background processing
Background processing
Background processing
Background processing
@asgrim
Real world uses?
@asgrim
SOA
@asgrim
Why RabbitMQ?
@asgrim
(you need Vagrant+VirtualBox already)
Installing RabbitMQ
@asgrim
vagrant up
Copy folder from USB to your local machine.
$ cd /path/to/the/folder/you/copied/from/usb/
$ vagrant box add asgrim/rmq-vm rmq-vm.box
$ vagrant up --no-provision
Test it in your browser...
http://192.168.33.99:15672/
@asgrim
Library with Composer
composer require videlalvaro/php-amqplib
@asgrim
The Management Console
http://192.168.33.99:15672/
@asgrim
Practical
@asgrim
Objective: Basic Queuing
Producer Consumer
test_queue
1 2 3 4 5
@asgrim
Exchanges: Fanout
@asgrim
Objective: Fanout Exchange
test_exchange
amq.KfgPZ3PE
amq.cK5Cp3FC
Consumer
Consumer
Producer
1
1
2
2
3
3
4
4
5
5
@asgrim
A word on Temporary Queues
test_exchangeProducer
Messages
go nowhere
@asgrim
Exchanges: Direct
@asgrim
Objective: Direct Exchange
test_direct
BK = apple
BK = banana, apple
Consumer
Consumer
Producer
BK = orange, banana,
apple
Consumer
@asgrim
Objective: Direct Exchange
test_direct
BK = apple
BK = banana, apple
Consumer
Consumer
Producer
MESSAGE
ROUTING KEY
= ORANGE
BK = orange, banana,
apple
Consumer
@asgrim
Objective: Direct Exchange
test_direct
BK = apple
BK = banana, apple
Consumer
Consumer
Producer
MESSAGE
ROUTING KEY
= BANANA
BK = orange, banana,
apple
Consumer
@asgrim
Objective: Direct Exchange
test_direct
BK = apple
BK = banana, apple
Consumer
Consumer
Producer
MESSAGE
ROUTING KEY
= APPLE
BK = orange, banana,
apple
Consumer
@asgrim
Exchanges: Topic
@asgrim
Objective:
Topic Exchange
test_topic
BK = *.vegetable
BK = #
Consumer
Consumer
Producer
BK = green.#
Consumer
BK = *.grass.* / *.*.long
Consumer
@asgrim
Objective:
Topic Exchange
test_topic
BK = *.vegetable
BK = #
Consumer
Consumer
Producer
RED.VEGETABLE
BK = green.#
Consumer
BK = *.grass.* / *.*.long
Consumer
@asgrim
Objective:
Topic Exchange
test_topic
BK = *.vegetable
BK = #
Consumer
Consumer
Producer
GREEN.VEGETABLE
BK = green.#
Consumer
BK = *.grass.* / *.*.long
Consumer
@asgrim
Objective:
Topic Exchange
test_topic
BK = *.vegetable
BK = #
Consumer
Consumer
Producer
GREEN.GRASS.LONG
BK = green.#
Consumer
BK = *.grass.* / *.*.long
Consumer
@asgrim
Message
Acknowledgement
@asgrim
Real World Example
@asgrim
Fetch message
Logging Sequence
ApplicationBrowser Log Server
HTTP request
JSON via AMQP
Error!
HTTP response
RabbitMQ
@asgrim
Flexibility!
@asgrim
Parallel Processing
@asgrim
test_exchange
amq.KfgPZ3PE
amq.cK5Cp3FC
Consumer
Producer
1
1
2
2
3
3
4
4
5
5
Consumer
Consumer
Consumer
Consumer
Consumer
Consumer
Consumer
Parallel Processing
@asgrim
Photo: © Rob Allen (akrabat) https://flic.kr/p/6wp9iz
@asgrim
RPC
@asgrim
RPC example
Producer Consumer
test_queue
1
reply_to: amq.gen-Xa2
1’
@asgrim
TTL
@asgrim
TTL - per queue message
Producer Consumer
test_queue
1 2 3 4 5
}
10s@asgrim
TTL - per message
Producer Consumer
test_queue
1 2 3 4 5
5s 3s 7s 1s 9s@asgrim
TTL - queue
Producer
test_queue
}
5s (if no consumers)
@asgrim
DLX
@asgrim
DLX
test_exchange
amq.KfgPZ3PE
Producer 1
dlx_exchange
dlx_queue
1
@asgrim
Scheduling / Delayed messages
@asgrim
Shovel
@asgrim
Priority
@asgrim
Management HTTP API
@asgrim
Infrastructure
@asgrim
Problem: SPOF
@asgrim
Solution 1: Clustering
@asgrim
Clustering
RabbitMQ
Node 1
RabbitMQ
Node 3
RabbitMQ
Node 2
RabbitMQ
Node 4
RabbitMQ
Node 5
RabbitMQ
Node 6
Load Balance / Floating IP / Low TTL DNS etc.
@asgrim
Everything Replicates
(except queues…)
@asgrim
Disk / RAM
@asgrim
Configuration...
@asgrim
/etc/rabbitmq/rabbitmq.config
[
{rabbit, [
{loopback_users, []},
{vm_memory_high_watermark, 0.8}
]}
].
@asgrim
/etc/rabbitmq/rabbitmq.config
[{{{[{{[{{}}{][[[{[{{}[[}{[[{}[][}{}}}{}}{{,},]{
[[{rabbit, [{{}[[}{,,{}[][}{[][][{}{{{{}}}}[[}{{
{{}}{loopback_users, []},[][][]{}{}{}<}{[}[][][}
[{{[{vm_memory_high_watermark, 0.8}]]{}{[[[]]{}]
{{]}[{[{{}[[}{]]{}[][,{}[][}{[][][{}.[]}{]][][]}
]...{}[][,]{.}[][}{}[[[{}{][]}{}{}[}{}{}{]{}{}}[
@asgrim
node1$ rabbitmqctl cluster_status
Cluster status of node rabbit@node1 ...
[{nodes,[{disc,[rabbit@node1]}]},{running_nodes,[rabbit@node1]}]
...done.
Creating a cluster
@asgrim
node2$ rabbitmqctl join_cluster --ram rabbit@node1
node3$ rabbitmqctl join_cluster rabbit@node2
node3$ rabbitmqctl cluster_status
Cluster status of node rabbit@node3 ...
[{nodes,[{disc,[rabbit@node3,rabbit@node1]},{ram,[rabbit@node2]}]},
{running_nodes,[rabbit@node2,rabbit@node1,rabbit@node3]}]
...done.
Creating a cluster
@asgrim
Starting/Stopping Nodes
@asgrim
node1$ rabbitmqctl stop_app
node2$ rabbitmqctl forget_cluster_node rabbit@node1
node1$ rabbitmqctl reset
node1$ rabbitmqctl start_app
node2$ rabbitmqctl cluster_status
Cluster status of node rabbit@node2 ...
[{nodes,[{disc,[rabbit@node3]},{ram,[rabbit@node2]}]},
{running_nodes,[rabbit@node2,rabbit@node3]}]
...done.
Removing Nodes
@asgrim
Solution 2: HA
@asgrim
HA + Queue Mirroring
RabbitMQ
Node 1
RabbitMQ
Node 2
Load Balance / Floating IP / Low TTL DNS etc.
@asgrim
One more thing...
@asgrim
Challenge!
@asgrim
Any questions? :)
https://joind.in/talk/8a46c
James Titcumb @asgrim

Introducing Practical RabbitMQ (php[tek] 2016 - Tutorial)