Scaling applications with RabbitMQ at SunshinePHP

149
Scaling Applications with RabbitMQ Alvaro Videla - RabbitMQ

description

Do you need to process thousands of images in the background for your web app? Do you need to share data across multiple applications, probably written in different languages and sitting at different servers? Your real time data feed is becoming slow because you are polling the database constantly for new data updates? Do you need to scale information processing during peek times? What about deploying new features with zero downtime? If any of these problems sound familiar then you probably need to use messaging in your application. In this talk I will introduce RabbitMQ, a messaging and queue server that can help us tackle those problems. We will learn the benefits of a Queue Server and see how to integrate messaging into our applications. With this talk we hope that the term 'decoupling' gets a new, broader, meaning.

Transcript of Scaling applications with RabbitMQ at SunshinePHP

Page 1: Scaling applications with RabbitMQ   at SunshinePHP

Scaling Applications with RabbitMQ

Alvaro Videla - RabbitMQ

Page 2: Scaling applications with RabbitMQ   at SunshinePHP

Alvaro Videla

• Developer Advocate at Pivotal / RabbitMQ!

• Co-Author of RabbitMQ in Action!

• Creator of the RabbitMQ Simulator!

• Blogs about RabbitMQ Internals: http://videlalvaro.github.io/internals.html!

• @old_sound — [email protected] — github.com/videlalvaro

Page 3: Scaling applications with RabbitMQ   at SunshinePHP

About Me

Co-authored!!

RabbitMQ in Action!

http://bit.ly/rabbitmq

Page 4: Scaling applications with RabbitMQ   at SunshinePHP

About this Talk

• Exploratory Talk

• A ‘what could be done’ talk instead of ‘this is how you do it’

Page 5: Scaling applications with RabbitMQ   at SunshinePHP

What is RabbitMQ

Page 6: Scaling applications with RabbitMQ   at SunshinePHP

RabbitMQ

Page 7: Scaling applications with RabbitMQ   at SunshinePHP

RabbitMQ

Page 8: Scaling applications with RabbitMQ   at SunshinePHP

RabbitMQ• Multi Protocol Messaging Server

Page 9: Scaling applications with RabbitMQ   at SunshinePHP

• Multi Protocol Messaging Server!• Open Source (MPL)

RabbitMQ

Page 10: Scaling applications with RabbitMQ   at SunshinePHP

• Multi Protocol Messaging Server!• Open Source (MPL)!• Polyglot

RabbitMQ

Page 11: Scaling applications with RabbitMQ   at SunshinePHP

• Multi Protocol Messaging Server!• Open Source (MPL)!• Polyglot!• Written in Erlang/OTP

RabbitMQ

Page 13: Scaling applications with RabbitMQ   at SunshinePHP

Polyglot

Page 14: Scaling applications with RabbitMQ   at SunshinePHP

Polyglot

Page 15: Scaling applications with RabbitMQ   at SunshinePHP

Polyglot

• Java

Page 16: Scaling applications with RabbitMQ   at SunshinePHP

Polyglot

• Java!• node.js

Page 17: Scaling applications with RabbitMQ   at SunshinePHP

Polyglot

• Java!• node.js!• Erlang

Page 18: Scaling applications with RabbitMQ   at SunshinePHP

Polyglot

• Java!• node.js!• Erlang!• PHP

Page 19: Scaling applications with RabbitMQ   at SunshinePHP

Polyglot

• Java!• node.js!• Erlang!• PHP!• Ruby

Page 20: Scaling applications with RabbitMQ   at SunshinePHP

Polyglot

• Java!• node.js!• Erlang!• PHP!• Ruby!• .Net

Page 21: Scaling applications with RabbitMQ   at SunshinePHP

Polyglot

• Java!• node.js!• Erlang!• PHP!• Ruby!• .Net!• Haskell

Page 22: Scaling applications with RabbitMQ   at SunshinePHP

Polyglot

Even COBOL!!!11

Page 23: Scaling applications with RabbitMQ   at SunshinePHP

Some users of RabbitMQ

Page 24: Scaling applications with RabbitMQ   at SunshinePHP

• Instagram

Some users of RabbitMQ

Page 25: Scaling applications with RabbitMQ   at SunshinePHP

• Instagram!• Indeed.com

Some users of RabbitMQ

Page 26: Scaling applications with RabbitMQ   at SunshinePHP

• Instagram!• Indeed.com!• Telefonica

Some users of RabbitMQ

Page 27: Scaling applications with RabbitMQ   at SunshinePHP

• Instagram!• Indeed.com!• Telefonica!• Mercado Libre

Some users of RabbitMQ

Page 28: Scaling applications with RabbitMQ   at SunshinePHP

• Instagram!• Indeed.com!• Telefonica!• Mercado Libre!• NHS

Some users of RabbitMQ

Page 29: Scaling applications with RabbitMQ   at SunshinePHP

• Instagram!• Indeed.com!• Telefonica!• Mercado Libre!• NHS!• Mozilla

Some users of RabbitMQ

Page 30: Scaling applications with RabbitMQ   at SunshinePHP

http://www.rabbitmq.com/download.html

Unix - Mac - Windows

Page 31: Scaling applications with RabbitMQ   at SunshinePHP

Messaging with RabbitMQ

A demo with the RabbitMQ Simulator

https://github.com/RabbitMQSimulator/RabbitMQSimulator

Page 32: Scaling applications with RabbitMQ   at SunshinePHP

http://tryrabbitmq.com

Page 33: Scaling applications with RabbitMQ   at SunshinePHP

RabbitMQ Simulator

Page 34: Scaling applications with RabbitMQ   at SunshinePHP

What you get out of the box

Page 35: Scaling applications with RabbitMQ   at SunshinePHP

Some RabbitMQ Features

Page 36: Scaling applications with RabbitMQ   at SunshinePHP

Some RabbitMQ Features• No message loss

Page 37: Scaling applications with RabbitMQ   at SunshinePHP

Some RabbitMQ Features• No message loss

• Persistent Messages

Page 38: Scaling applications with RabbitMQ   at SunshinePHP

Some RabbitMQ Features• No message loss

• Persistent Messages

• Publisher Confirms

Page 39: Scaling applications with RabbitMQ   at SunshinePHP

Some RabbitMQ Features• No message loss

• Persistent Messages

• Publisher Confirms

• Message Acknowledgment

Page 40: Scaling applications with RabbitMQ   at SunshinePHP

Some RabbitMQ Features• No message loss

• Persistent Messages

• Publisher Confirms

• Message Acknowledgment

• Mirrored Queues

Page 41: Scaling applications with RabbitMQ   at SunshinePHP

Some RabbitMQ Features• No message loss

• Persistent Messages

• Publisher Confirms

• Message Acknowledgment

• Mirrored Queues

• Message Ordering

Page 42: Scaling applications with RabbitMQ   at SunshinePHP

Some RabbitMQ Features

Page 43: Scaling applications with RabbitMQ   at SunshinePHP

Some RabbitMQ Features• Dead Letter Exchanges

Page 44: Scaling applications with RabbitMQ   at SunshinePHP

Some RabbitMQ Features• Dead Letter Exchanges

• Alternate Exchanges

Page 45: Scaling applications with RabbitMQ   at SunshinePHP

Some RabbitMQ Features• Dead Letter Exchanges

• Alternate Exchanges

• Message and Queues TTLs

Page 46: Scaling applications with RabbitMQ   at SunshinePHP

Some RabbitMQ Features• Dead Letter Exchanges

• Alternate Exchanges

• Message and Queues TTLs

• Consumer Priorities

Page 47: Scaling applications with RabbitMQ   at SunshinePHP

Some RabbitMQ Features• Dead Letter Exchanges

• Alternate Exchanges

• Message and Queues TTLs

• Consumer Priorities

• Federation

Page 48: Scaling applications with RabbitMQ   at SunshinePHP

Some RabbitMQ Features• Dead Letter Exchanges

• Alternate Exchanges

• Message and Queues TTLs

• Consumer Priorities

• Federation

• Shovel

Page 49: Scaling applications with RabbitMQ   at SunshinePHP

Preventing unbounded buffers with RabbitMQ

http://bit.ly/1eCKYK8

Page 50: Scaling applications with RabbitMQ   at SunshinePHP

RabbitMQ Management

Page 51: Scaling applications with RabbitMQ   at SunshinePHP

Scenario

Batch Processing

Page 52: Scaling applications with RabbitMQ   at SunshinePHP

Requirements

Page 53: Scaling applications with RabbitMQ   at SunshinePHP

Requirements

• Generate XML

Page 54: Scaling applications with RabbitMQ   at SunshinePHP

Requirements

• Generate XML

• Distribution Over a Cluster

Page 55: Scaling applications with RabbitMQ   at SunshinePHP

Requirements

• Generate XML

• Distribution Over a Cluster

• Elasticity - Add/Remove new workers

Page 56: Scaling applications with RabbitMQ   at SunshinePHP

Requirements

• Generate XML

• Distribution Over a Cluster

• Elasticity - Add/Remove new workers

• No Code Changes

Page 57: Scaling applications with RabbitMQ   at SunshinePHP

Design

Page 58: Scaling applications with RabbitMQ   at SunshinePHP

Publisher Code

$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);$channel = $conn->channel();!$channel->exchange_declare('video-desc-ex', 'direct', false,

true, false);!$msg = new AMQPMessage($video_info,

array('content_type' => 'text/plain', 'delivery_mode' => 2));

!$channel->basic_publish($msg, 'video-desc-ex');!$channel->close();$conn->close();

Page 59: Scaling applications with RabbitMQ   at SunshinePHP

Publisher Code

$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);$channel = $conn->channel();!$channel->exchange_declare('video-desc-ex', 'direct', false,

true, false);!$msg = new AMQPMessage($video_info,

array('content_type' => 'text/plain', 'delivery_mode' => 2));

!$channel->basic_publish($msg, 'video-desc-ex');!$channel->close();$conn->close();

Page 60: Scaling applications with RabbitMQ   at SunshinePHP

Publisher Code

$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);$channel = $conn->channel();!$channel->exchange_declare('video-desc-ex', 'direct', false,

true, false);!$msg = new AMQPMessage($video_info,

array('content_type' => 'text/plain', 'delivery_mode' => 2));

!$channel->basic_publish($msg, 'video-desc-ex');!$channel->close();$conn->close();

Page 61: Scaling applications with RabbitMQ   at SunshinePHP

Publisher Code

$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);$channel = $conn->channel();!$channel->exchange_declare('video-desc-ex', 'direct', false,

true, false);!$msg = new AMQPMessage($video_info,

array('content_type' => 'text/plain', 'delivery_mode' => 2));

!$channel->basic_publish($msg, 'video-desc-ex');!$channel->close();$conn->close();

Page 62: Scaling applications with RabbitMQ   at SunshinePHP

Publisher Code

$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);$channel = $conn->channel();!$channel->exchange_declare('video-desc-ex', 'direct', false,

true, false);!$msg = new AMQPMessage($video_info,

array('content_type' => 'text/plain', 'delivery_mode' => 2));

!$channel->basic_publish($msg, 'video-desc-ex');!$channel->close();$conn->close();

Page 63: Scaling applications with RabbitMQ   at SunshinePHP

Publisher Code

$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);$channel = $conn->channel();!$channel->exchange_declare('video-desc-ex', 'direct', false,

true, false);!$msg = new AMQPMessage($video_info,

array('content_type' => 'text/plain', 'delivery_mode' => 2));

!$channel->basic_publish($msg, 'video-desc-ex');!$channel->close();$conn->close();

Page 64: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code

$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);$channel = $conn->channel();!$channel->exchange_declare('video-desc-ex', 'direct', false,

true, false);$channel->queue_declare('video-desc-queue', false, true,

false, false);$channel->queue_bind('video-desc-queue', 'video-desc-ex');!$channel->basic_consume('video-desc-queue', $consumer_tag,

false, false, false, false, $consumer);!while(count($channel->callbacks)) { $channel->wait();}

Page 65: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code

$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);$channel = $conn->channel();!$channel->exchange_declare('video-desc-ex', 'direct', false,

true, false);$channel->queue_declare('video-desc-queue', false, true,

false, false);$channel->queue_bind('video-desc-queue', 'video-desc-ex');!$channel->basic_consume('video-desc-queue', $consumer_tag,

false, false, false, false, $consumer);!while(count($channel->callbacks)) { $channel->wait();}

Page 66: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code

$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);$channel = $conn->channel();!$channel->exchange_declare('video-desc-ex', 'direct', false,

true, false);$channel->queue_declare('video-desc-queue', false, true,

false, false);$channel->queue_bind('video-desc-queue', 'video-desc-ex');!$channel->basic_consume('video-desc-queue', $consumer_tag,

false, false, false, false, $consumer);!while(count($channel->callbacks)) { $channel->wait();}

Page 67: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code

$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);$channel = $conn->channel();!$channel->exchange_declare('video-desc-ex', 'direct', false,

true, false);$channel->queue_declare('video-desc-queue', false, true,

false, false);$channel->queue_bind('video-desc-queue', 'video-desc-ex');!$channel->basic_consume('video-desc-queue', $consumer_tag,

false, false, false, false, $consumer);!while(count($channel->callbacks)) { $channel->wait();}

Page 68: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code

$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);$channel = $conn->channel();!$channel->exchange_declare('video-desc-ex', 'direct', false,

true, false);$channel->queue_declare('video-desc-queue', false, true,

false, false);$channel->queue_bind('video-desc-queue', 'video-desc-ex');!$channel->basic_consume('video-desc-queue', $consumer_tag,

false, false, false, false, $consumer);!while(count($channel->callbacks)) { $channel->wait();}

Page 69: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code

$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);$channel = $conn->channel();!$channel->exchange_declare('video-desc-ex', 'direct', false,

true, false);$channel->queue_declare('video-desc-queue', false, true,

false, false);$channel->queue_bind('video-desc-queue', 'video-desc-ex');!$channel->basic_consume('video-desc-queue', $consumer_tag,

false, false, false, false, $consumer);!while(count($channel->callbacks)) { $channel->wait();}

Page 70: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code

$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);$channel = $conn->channel();!$channel->exchange_declare('video-desc-ex', 'direct', false,

true, false);$channel->queue_declare('video-desc-queue', false, true,

false, false);$channel->queue_bind('video-desc-queue', 'video-desc-ex');!$channel->basic_consume('video-desc-queue', $consumer_tag,

false, false, false, false, $consumer);!while(count($channel->callbacks)) { $channel->wait();}

Page 71: Scaling applications with RabbitMQ   at SunshinePHP

Scenario

Upload Pictures

Page 72: Scaling applications with RabbitMQ   at SunshinePHP

Requirements

Page 73: Scaling applications with RabbitMQ   at SunshinePHP

Requirements

• Upload Picture

Page 74: Scaling applications with RabbitMQ   at SunshinePHP

Requirements

• Upload Picture

• Reward User

Page 75: Scaling applications with RabbitMQ   at SunshinePHP

Requirements

• Upload Picture

• Reward User

• Notify User Friends

Page 76: Scaling applications with RabbitMQ   at SunshinePHP

Requirements

• Upload Picture

• Reward User

• Notify User Friends

• Resize Picture

Page 77: Scaling applications with RabbitMQ   at SunshinePHP

Requirements

• Upload Picture

• Reward User

• Notify User Friends

• Resize Picture

• No Code Changes

Page 78: Scaling applications with RabbitMQ   at SunshinePHP

Design

Page 79: Scaling applications with RabbitMQ   at SunshinePHP

Design

Page 80: Scaling applications with RabbitMQ   at SunshinePHP

Design

Page 81: Scaling applications with RabbitMQ   at SunshinePHP

Publisher Code$channel->exchange_declare('upload-pictures', 'fanout', false, true, false);!$metadata = json_encode(array('image_id' => $image_id,'user_id' => $user_id,‘image_path' => $image_path));

!$msg = new AMQPMessage($metadata, array('content_type' => 'application/json', 'delivery_mode' => 2));!$channel->basic_publish($msg, 'upload-pictures');

Page 82: Scaling applications with RabbitMQ   at SunshinePHP

Publisher Code$channel->exchange_declare('upload-pictures', 'fanout', false, true, false);!$metadata = json_encode(array('image_id' => $image_id,'user_id' => $user_id,‘image_path' => $image_path));

!$msg = new AMQPMessage($metadata, array('content_type' => 'application/json', 'delivery_mode' => 2));!$channel->basic_publish($msg, 'upload-pictures');

Page 83: Scaling applications with RabbitMQ   at SunshinePHP

Publisher Code$channel->exchange_declare('upload-pictures', 'fanout', false, true, false);!$metadata = json_encode(array('image_id' => $image_id,'user_id' => $user_id,‘image_path' => $image_path));

!$msg = new AMQPMessage($metadata, array('content_type' => 'application/json', 'delivery_mode' => 2));!$channel->basic_publish($msg, 'upload-pictures');

Page 84: Scaling applications with RabbitMQ   at SunshinePHP

Publisher Code$channel->exchange_declare('upload-pictures', 'fanout', false, true, false);!$metadata = json_encode(array('image_id' => $image_id,'user_id' => $user_id,‘image_path' => $image_path));

!$msg = new AMQPMessage($metadata, array('content_type' => 'application/json', 'delivery_mode' => 2));!$channel->basic_publish($msg, 'upload-pictures');

Page 85: Scaling applications with RabbitMQ   at SunshinePHP

Publisher Code$channel->exchange_declare('upload-pictures', 'fanout', false, true, false);!$metadata = json_encode(array('image_id' => $image_id,'user_id' => $user_id,‘image_path' => $image_path));

!$msg = new AMQPMessage($metadata, array('content_type' => 'application/json', 'delivery_mode' => 2));!$channel->basic_publish($msg, 'upload-pictures');

Page 86: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code$channel->exchange_declare('upload-pictures', 'fanout',

false, true, false);!$channel->queue_declare('resize-picture', false, true,

false, false);!$channel->queue_bind('resize-picture', 'upload-pictures');!$channel->basic_consume('resize-picture', $consumer_tag,

false, false, false, false, $consumer);!while(count($channel->callbacks)) {$channel->wait();

}

Page 87: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code$channel->exchange_declare('upload-pictures', 'fanout',

false, true, false);!$channel->queue_declare('resize-picture', false, true,

false, false);!$channel->queue_bind('resize-picture', 'upload-pictures');!$channel->basic_consume('resize-picture', $consumer_tag,

false, false, false, false, $consumer);!while(count($channel->callbacks)) {$channel->wait();

}

Page 88: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code$channel->exchange_declare('upload-pictures', 'fanout',

false, true, false);!$channel->queue_declare('resize-picture', false, true,

false, false);!$channel->queue_bind('resize-picture', 'upload-pictures');!$channel->basic_consume('resize-picture', $consumer_tag,

false, false, false, false, $consumer);!while(count($channel->callbacks)) {$channel->wait();

}

Page 89: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code$channel->exchange_declare('upload-pictures', 'fanout',

false, true, false);!$channel->queue_declare('resize-picture', false, true,

false, false);!$channel->queue_bind('resize-picture', 'upload-pictures');!$channel->basic_consume('resize-picture', $consumer_tag,

false, false, false, false, $consumer);!while(count($channel->callbacks)) {$channel->wait();

}

Page 90: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code$channel->exchange_declare('upload-pictures', 'fanout',

false, true, false);!$channel->queue_declare('resize-picture', false, true,

false, false);!$channel->queue_bind('resize-picture', 'upload-pictures');!$channel->basic_consume('resize-picture', $consumer_tag,

false, false, false, false, $consumer);!while(count($channel->callbacks)) {$channel->wait();

}

Page 91: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code$channel->exchange_declare('upload-pictures', 'fanout',

false, true, false);!$channel->queue_declare('resize-picture', false, true,

false, false);!$channel->queue_bind('resize-picture', 'upload-pictures');!$channel->basic_consume('resize-picture', $consumer_tag,

false, false, false, false, $consumer);!while(count($channel->callbacks)) {$channel->wait();

}

Page 92: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code

$consumer = function($msg){!$meta = json_decode($msg->body, true);

resize_picture($meta['image_id'], $meta['image_path']);

$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);

};

Page 93: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code

$consumer = function($msg){!$meta = json_decode($msg->body, true);

resize_picture($meta['image_id'], $meta['image_path']);

$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);

};

Page 94: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code

$consumer = function($msg){!$meta = json_decode($msg->body, true);

resize_picture($meta['image_id'], $meta['image_path']);

$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);

};

Page 95: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code

$consumer = function($msg){!$meta = json_decode($msg->body, true);

resize_picture($meta['image_id'], $meta['image_path']);

$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);

};

Page 96: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code

$consumer = function($msg){!$meta = json_decode($msg->body, true);

resize_picture($meta['image_id'], $meta['image_path']);

$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);

};

Page 97: Scaling applications with RabbitMQ   at SunshinePHP

Scenario

Distributed Logging

Page 98: Scaling applications with RabbitMQ   at SunshinePHP

Requirements

Page 99: Scaling applications with RabbitMQ   at SunshinePHP

Requirements• Several Web Servers

Page 100: Scaling applications with RabbitMQ   at SunshinePHP

Requirements• Several Web Servers

• Logic Separated by Module/Action

Page 101: Scaling applications with RabbitMQ   at SunshinePHP

Requirements• Several Web Servers

• Logic Separated by Module/Action

• Several Log Levels: info, warning and error

Page 102: Scaling applications with RabbitMQ   at SunshinePHP

Requirements• Several Web Servers

• Logic Separated by Module/Action

• Several Log Levels: info, warning and error

• Add Remove log listeners at will

Page 103: Scaling applications with RabbitMQ   at SunshinePHP

Design

Page 104: Scaling applications with RabbitMQ   at SunshinePHP

Design

Page 105: Scaling applications with RabbitMQ   at SunshinePHP

Design

Page 106: Scaling applications with RabbitMQ   at SunshinePHP

Design

Page 107: Scaling applications with RabbitMQ   at SunshinePHP

Design

Page 108: Scaling applications with RabbitMQ   at SunshinePHP

Publisher Code

$channel->exchange_declare('logs', 'topic', false, true, false);

!$msg = new AMQPMessage('some log message',

array('content_type' => 'text/plain'));!$channel->basic_publish($msg, 'logs',

'server1.user.profile.info');

Page 109: Scaling applications with RabbitMQ   at SunshinePHP

Publisher Code

$channel->exchange_declare('logs', 'topic', false, true, false);

!$msg = new AMQPMessage('some log message',

array('content_type' => 'text/plain'));!$channel->basic_publish($msg, 'logs',

'server1.user.profile.info');

Page 110: Scaling applications with RabbitMQ   at SunshinePHP

Publisher Code

$channel->exchange_declare('logs', 'topic', false, true, false);

!$msg = new AMQPMessage('some log message',

array('content_type' => 'text/plain'));!$channel->basic_publish($msg, 'logs',

'server1.user.profile.info');

Page 111: Scaling applications with RabbitMQ   at SunshinePHP

Publisher Code

$channel->exchange_declare('logs', 'topic', false, true, false);

!$msg = new AMQPMessage('some log message',

array('content_type' => 'text/plain'));!$channel->basic_publish($msg, 'logs',

'server1.user.profile.info');

Page 112: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code

Get messages sent by host: !

server1

Page 113: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code

$channel->exchange_declare('logs', 'topic', false, true, false);!

$channel->queue_declare('server1-logs', false, true, false, false);!

$channel->queue_bind('server1-logs', 'logs', 'server1.#');

Page 114: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code

$channel->exchange_declare('logs', 'topic', false, true, false);!

$channel->queue_declare('server1-logs', false, true, false, false);!

$channel->queue_bind('server1-logs', 'logs', 'server1.#');

Page 115: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code

$channel->exchange_declare('logs', 'topic', false, true, false);!

$channel->queue_declare('server1-logs', false, true, false, false);!

$channel->queue_bind('server1-logs', 'logs', 'server1.#');

Page 116: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code

$channel->exchange_declare('logs', 'topic', false, true, false);!

$channel->queue_declare('server1-logs', false, true, false, false);!

$channel->queue_bind('server1-logs', 'logs', 'server1.#');

Page 117: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code

Get all error messages

Page 118: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code

$channel->exchange_declare('logs', 'topic', false, true, false);

!$channel->queue_declare('error-logs', false, true,

false, false);!

$channel->queue_bind('error-logs', 'logs', '#.error');

Page 119: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code

$channel->exchange_declare('logs', 'topic', false, true, false);

!$channel->queue_declare('error-logs', false, true,

false, false);!

$channel->queue_bind('error-logs', 'logs', '#.error');

Page 120: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code

$channel->exchange_declare('logs', 'topic', false, true, false);

!$channel->queue_declare('error-logs', false, true,

false, false);!

$channel->queue_bind('error-logs', 'logs', '#.error');

Page 121: Scaling applications with RabbitMQ   at SunshinePHP

Consumer Code

$channel->exchange_declare('logs', 'topic', false, true, false);

!$channel->queue_declare('error-logs', false, true,

false, false);!

$channel->queue_bind('error-logs', 'logs', '#.error');

Page 122: Scaling applications with RabbitMQ   at SunshinePHP

Distributed Apps

Page 123: Scaling applications with RabbitMQ   at SunshinePHP

Distributed Application

App

App

App

App

Page 124: Scaling applications with RabbitMQ   at SunshinePHP

Distributed Application

App

App

App

App

Page 125: Scaling applications with RabbitMQ   at SunshinePHP

Ad-hoc solution

Page 126: Scaling applications with RabbitMQ   at SunshinePHP

A process that replicates data to the remote server

Page 127: Scaling applications with RabbitMQ   at SunshinePHP

Possible issues• Remote server is offline

• Prevent unbounded local buffers

• Prevent message loss

• Prevent unnecessary message replication

• No need for those messages on remote server

• Messages that became stale

Page 128: Scaling applications with RabbitMQ   at SunshinePHP

Can we do better?

Page 129: Scaling applications with RabbitMQ   at SunshinePHP

RabbitMQ Federation

Page 130: Scaling applications with RabbitMQ   at SunshinePHP

RabbitMQ Federation• Supports replication across different administrative domains

• Supports mix of Erlang and RabbitMQ versions

• Supports Network Partitions

• Specificity - not everything has to be federated

Page 131: Scaling applications with RabbitMQ   at SunshinePHP

RabbitMQ Federation

Page 132: Scaling applications with RabbitMQ   at SunshinePHP

RabbitMQ Federation

Page 133: Scaling applications with RabbitMQ   at SunshinePHP

RabbitMQ Federation

Page 134: Scaling applications with RabbitMQ   at SunshinePHP

RabbitMQ Federation

• It’s a RabbitMQ Plugin

Page 135: Scaling applications with RabbitMQ   at SunshinePHP

RabbitMQ Federation

• It’s a RabbitMQ Plugin

• Internally uses Queues and Exchanges Decorators

Page 136: Scaling applications with RabbitMQ   at SunshinePHP

RabbitMQ Federation

• It’s a RabbitMQ Plugin

• Internally uses Queues and Exchanges Decorators

• Managed using Parameters and Policies

Page 137: Scaling applications with RabbitMQ   at SunshinePHP

Enabling the Plugin

rabbitmq-plugins enable rabbitmq_federation

Page 138: Scaling applications with RabbitMQ   at SunshinePHP

Enabling the Plugin

rabbitmq-plugins enable rabbitmq_federation

rabbitmq-plugins enable rabbitmq_federation_management

Page 139: Scaling applications with RabbitMQ   at SunshinePHP

Federating an Exchange

rabbitmqctl set_parameter federation-upstream my-upstream \ ‘{“uri":"amqp://server-name","expires":3600000}'

Page 140: Scaling applications with RabbitMQ   at SunshinePHP

Federating an Exchange

rabbitmqctl set_parameter federation-upstream my-upstream \ ‘{“uri":"amqp://server-name","expires":3600000}' !

rabbitmqctl set_policy --apply-to exchanges federate-me "^amq\." \ '{"federation-upstream-set":"all"}'

Page 141: Scaling applications with RabbitMQ   at SunshinePHP

Federating an Exchange

Page 142: Scaling applications with RabbitMQ   at SunshinePHP

With RabbitMQ we can

Page 143: Scaling applications with RabbitMQ   at SunshinePHP

With RabbitMQ we can

• Ingest data using various protocols: AMQP, MQTT and STOMP

Page 144: Scaling applications with RabbitMQ   at SunshinePHP

With RabbitMQ we can

• Ingest data using various protocols: AMQP, MQTT and STOMP

• Distribute that data globally using Federation

Page 145: Scaling applications with RabbitMQ   at SunshinePHP

With RabbitMQ we can

• Ingest data using various protocols: AMQP, MQTT and STOMP

• Distribute that data globally using Federation

• Scale up using with different consumer strategies

Page 146: Scaling applications with RabbitMQ   at SunshinePHP

With RabbitMQ we can

• Ingest data using various protocols: AMQP, MQTT and STOMP

• Distribute that data globally using Federation

• Scale up using with different consumer strategies

• Integrate across many platforms and programming languages

Page 147: Scaling applications with RabbitMQ   at SunshinePHP

Questions?

Page 148: Scaling applications with RabbitMQ   at SunshinePHP

https://joind.in/talk/view/10525

Talk and Slides

Page 149: Scaling applications with RabbitMQ   at SunshinePHP

ThanksAlvaro Videla - @old_sound

world map: wikipedia.org federation diagrams: rabbitmq.com

Credits