RabbitMQ fairly-indepth

60
A somewhat in-depth look

description

Following up from AMQP presentation, this is a more in-depth coverage of RabbitMQ with workshop-style walkthrough, covering various aspects of the system.

Transcript of RabbitMQ fairly-indepth

Page 1: RabbitMQ fairly-indepth

A somewhat in-depth look

Page 2: RabbitMQ fairly-indepth

Overview

Page 3: RabbitMQ fairly-indepth

• Idea was born around the same time AMQP was first drafted by JP Morgan Chase

• First released in 2006

• Acquired by SpringSource in 2010 (now part of VMWare)

• First version built in under 3 months!

• Written in Erlang/OTP

RabbitMQ History & Facts

Page 4: RabbitMQ fairly-indepth

• Used by quite a few major projects including

‣ Huffington Post (Huff Live)

‣ Indeed (Job Aggregation service)

‣ VMWare (vFabric)

‣ SoundCloud (transcoding service)

‣ Mozilla (Pulse)

RabbitMQ History & Facts

Page 5: RabbitMQ fairly-indepth

The Basics

Page 6: RabbitMQ fairly-indepth

Virtual Host

Queues Exchange

Binding

AMQ Model Recap

➡ Queue: ‣ critical_error_queue

➡ Exchange: ‣ errors_exchange

➡ Filter:‣ *.error.critical

Message

Publisher

Consumer

Final overview of what the AMQ Model is about - in a nutshell... * all within a virtual host (completely isolated) * this is protocol at the server’s service level

Page 7: RabbitMQ fairly-indepth

QueuesExchange Binding

AMQ Model Recap

Publisher Consumer

‣ creates a relationship between queues and exchanges‣ contains criteria and properties‣ can be created at runtime by consumers

‣ store messages‣ named‣ bound-able to exchange‣ criteria‣ can be created at runtime by consumers

‣ routes messages based on criteria‣ doesn’t store messages‣ can inspect message content‣ can be created at runtime consumers

‣ can create exchanges and queues

‣ can create exchanges and queues

Page 8: RabbitMQ fairly-indepth

Fanout Topic Headers System

Exchange

Types(routing algo)

Direct

Exchange Types

Page 9: RabbitMQ fairly-indepth

Direct Exchange

“events”type: direct

Q: user_clicksX: eventsK: user.click

MessagePublisher Consumer

K: user.click

Page 10: RabbitMQ fairly-indepth

Direct Exchange

“events”type: direct

Q: user_clicksX: eventsK: user.click

Message

Publisher Consumer

K: user.view

Message

K: user.click

Message

K: user.share

Page 11: RabbitMQ fairly-indepth

Direct Exchange

“events”type: direct

Q: user_clicksX: eventsK: user.click

Message

Publisher Consumer

K: user.view

Message

K: user.click

Message

K: user.share

Q: user_sharesX: eventsK: user.share

Q: user_viewsX: eventsK: user.click user.share user.view

Page 12: RabbitMQ fairly-indepth

Direct Exchange

“events”type: direct

Q: user_clicksX: eventsK: user.click

Message

Publisher Consumer

K: user.view

Message

K: user.click

Message

K: user.share

Q: user_sharesX: eventsK: user.share

Q: user_viewsX: eventsK: user.click user.share user.view

Page 13: RabbitMQ fairly-indepth

Direct Exchange

“events”type: direct

Q: user_clicksX: eventsK: user.click

Message

Publisher Consumer

K: user.view

Message

K: user.click

Message

K: user.share

Q: user_sharesX: eventsK: user.share

Q: user_viewsX: eventsK: user.click user.share user.view

Page 14: RabbitMQ fairly-indepth

Direct Exchange

“events”type: direct

Q: user_clicksX: eventsK: user.click

Message

Publisher Consumer

K: user.view

Message

K: user.click

Message

K: user.share

Q: user_sharesX: eventsK: user.share

Q: user_viewsX: eventsK: user.click user.share user.view

Page 15: RabbitMQ fairly-indepth

Direct Exchange

Walkthrough and demo

Page 16: RabbitMQ fairly-indepth

Topic Exchange

“events”type: topic

Q: all_errorsX: eventsK: *.error

Message

Publisher Consumer

K: sys.info

Message

K: sys.error

Message

K: app.info

Q: system_errorX: eventsK: sys.error

Q: app_logs_sys_errorX: eventsK: app.* sys.error

Message

K: app.error

Page 17: RabbitMQ fairly-indepth

Topic Exchange

“events”type: topic

Q: all_errorsX: eventsK: *.error

Message

Publisher Consumer

K: sys.info

Message

K: sys.error

Message

K: app.info

Q: system_errorX: eventsK: sys.error

Q: app_logs_sys_errorX: eventsK: app.* sys.error

Message

K: app.error

Page 18: RabbitMQ fairly-indepth

Topic Exchange

“events”type: topic

Q: all_errorsX: eventsK: *.error

Message

Publisher Consumer

K: sys.info

Message

K: sys.error

Message

K: app.info

Q: system_errorX: eventsK: sys.error

Q: app_logs_sys_errorX: eventsK: app.* sys.error

Message

K: app.error

Page 19: RabbitMQ fairly-indepth

Topic Exchange

“events”type: direct

Q: all_errorsX: eventsK: *.error

Message

Publisher Consumer

K: sys.info

Message

K: sys.error

Message

K: app.info

Q: system_errorX: eventsK: sys.error

Q: app_logs_sys_errorX: eventsK: app.* sys.error

Message

K: app.error

Page 20: RabbitMQ fairly-indepth

Topic Exchange

Walkthrough and demo

Page 21: RabbitMQ fairly-indepth

Topic Exchange

“events”type: topic

Q: all_errorsX: eventsK: *.error

Message

Publisher Consumer

K: sys.info

Message

K: sys.error

Message

K: app.info

Q: system_errorX: eventsK: sys.error

Q: app_logs_sys_errorX: eventsK: app.* sys.error

Message

K: app.error

Page 22: RabbitMQ fairly-indepth

Persistence & Durability

exchangeMessagePublisher ConsumerQueues

Page 23: RabbitMQ fairly-indepth

Persistence & Durability

exchangeMessagePublisher ConsumerQueues

‣persistent

Page 24: RabbitMQ fairly-indepth

Persistence & Durability

exchangeMessagePublisher ConsumerQueues

‣persistent ‣durable

Page 25: RabbitMQ fairly-indepth

Persistence & Durability

exchangeMessagePublisher ConsumerQueues

‣persistent ‣durable ‣durable

Page 26: RabbitMQ fairly-indepth

Persistence & Durability

exchangeMessagePublisher ConsumerQueues

‣persistent ‣durable ‣durable

Component will be recreatedin the event of reboot/restart.

Does not mean its content will be retained

Page 27: RabbitMQ fairly-indepth

Persistence & Durability

exchangeMessagePublisher ConsumerQueues

‣persistent ‣durable ‣durable

Component will be recreatedin the event of reboot/restart.

Does not mean its content will be retained

Messages will be persisted to disk.

In RabbitMQ, messages are actually queued in memory before written to disk and f ’syncd

For recoverable messages, queue(s) must be durable

Page 28: RabbitMQ fairly-indepth

Administration

Page 29: RabbitMQ fairly-indepth

Administration

• Management tools via rabbitmq-plugins

• Command:sudo rabbitmq-plugins enable rabbitmq_management

• Gives you:

• rabbitmqctl

• MochiWeb: http://localhost:15672

Page 30: RabbitMQ fairly-indepth

Administration

• Important tools:

‣ rabbitmqctl commands

- users, perms, queues, bindings

- status, reset

‣ MochiWeb features

Page 31: RabbitMQ fairly-indepth

Distributed RabbitMQClustering & Replication

Page 32: RabbitMQ fairly-indepth

Distributed RabbitMQ

Clustering Replication HA

Page 33: RabbitMQ fairly-indepth

Distributed RabbitMQ

Clustering Replication

➡Acts as a single logical unit

➡Consumer sees cluster as single node

➡Nodes shares the same Erlang cookie

➡Automatic metadata replication, etc

➡ Requires reliable LAN-like environment

➡ Emphasis on Consistency & Availability

HA

Page 34: RabbitMQ fairly-indepth

Distributed RabbitMQ

Clustering Replication

➡Acts as a single logical unit

➡Consumer sees cluster as single node

➡Nodes shares the same Erlang cookie

➡Automatic metadata replication, etc

➡ Requires reliable LAN-like environment

➡ Emphasis on Consistency & Availability

➡Acts like a forwarding agent

➡Nodes doesn’t share any cookie and is standalone

➡Uses AMQP protocol itself for forwarding

➡ Suitable for unreliable WAN connections

➡ Emphasis on Availability & Partition Tolerance

HA

Page 35: RabbitMQ fairly-indepth

Distributed RabbitMQ

Clustering Replication

➡Acts as a single logical unit

➡Consumer sees cluster as single node

➡Nodes shares the same Erlang cookie

➡Automatic metadata replication, etc

➡ Requires reliable LAN-like environment

➡ Emphasis on Consistency & Availability

➡Acts like a forwarding agent

➡Nodes doesn’t share any cookie and is standalone

➡Uses AMQP protocol itself for forwarding

➡ Suitable for unreliable WAN connections

➡ Emphasis on Availability & Partition Tolerance

HA

➡A combo of both

➡ It’s cluster with replication

➡Usually in form of master/slave

➡ Emphasis on Consistency & Availability

Page 36: RabbitMQ fairly-indepth

Clustering

Clustering

Page 37: RabbitMQ fairly-indepth

metadata

Clustering

Clustering

metadata

metadata

rabbit01 rabbit03

rabbit02

Cluster

Typical clusteringMetadata is replicated across all nodes (operations, queues, exchange and bindings declaration, etc)Let’s see what happens when a queue is created... Let’s say we have a publisher...

Page 38: RabbitMQ fairly-indepth

metadata

Clustering

Clustering

metadata

metadata

rabbit01 rabbit03

rabbit02

Cluster

P

and this is the first time it’s publishing message to a newly declared queue

Page 39: RabbitMQ fairly-indepth

metadata

Clustering

Clustering

shares_queue

metadata

metadata

rabbit01 rabbit03

rabbit02

Cluster

P

Through a LB, the connection is established with rabbit01. Publish the message, which creates a queue and then message to it.What should happen?

Page 40: RabbitMQ fairly-indepth

metadata

Clustering

Clustering

shares_queue

metadata

metadata

rabbit01 rabbit03

rabbit02

Cluster

Queues are created on the node called by consumer/publisher and not replicatedBUT the metadata is replicated.

Page 41: RabbitMQ fairly-indepth

metadata

Clustering

Clustering

shares_queue

metadata

metadata

rabbit01 rabbit03

rabbit02

Cluster

P

clicks_queue

Queues are created on the node called by consumer/publisher and not replicated

Page 42: RabbitMQ fairly-indepth

metadata

Clustering

Clustering

shares_queue

metadata

metadata

clicks_queue

rabbit01 rabbit03

rabbit02

Cluster

Page 43: RabbitMQ fairly-indepth

Clustering Notes• There are three types of nodes:

‣ Stat, Disk and Ram

• All nodes must share the same cookie

• Works like a shard by default, thus adding new node to cluster improves performance

• Upgrade is not automated

• Best have one stat-node with WebUI, rest with agents

Page 44: RabbitMQ fairly-indepth

Important

• RabbitMQ was not designed to handle network partition.

• Nodes in clusters should have LAN-like reliability

Page 45: RabbitMQ fairly-indepth

Clustering

Walkthrough and demo

Page 46: RabbitMQ fairly-indepth

Distributed RabbitMQ

Clustering Replication

➡Acts as a single logical unit

➡Consumer sees cluster as single node

➡Nodes shares the same Erlang cookie

➡Automatic metadata replication, etc

➡ Requires reliable LAN-like environment

➡ Emphasis on Consistency & Availability

➡Acts like a forwarding agent

➡Nodes doesn’t share any cookie and is standalone

➡Uses AMQP protocol itself for forwarding

➡ Suitable for unreliable WAN connections

➡ Emphasis on Availability & Partition Tolerance

HA

➡A combo of both

➡ It’s cluster with replication

➡Usually in form of master/slave

➡ Emphasis on Consistency & Availability

Naturally clustering doesn’t really solve high availability problem as it is a separate issue. Let’s look at HA next

Page 47: RabbitMQ fairly-indepth

HA with Mirrored Queues

metadata

shares_queue

metadata

metadata

shares_queue

rabbit01 rabbit03

rabbit02

Cluster

HA

Needs to be in a clustered environment before HA can be achieved

Page 48: RabbitMQ fairly-indepth

HA with Mirrored Queues

metadata

shares_queue

metadata

metadata

shares_queue

rabbit01 rabbit03

rabbit02

Cluster

HA

Needs to be in a clustered environment before HA can be achieved

Page 49: RabbitMQ fairly-indepth

• Set up as a queue policy in RabbitMQ

‣ Create a new policy

‣ Attach a set of policy keys to it to trigger mirroring

• Policy key: ha-mode

• Policy values: all, exactly(N), nodes(str...)

HA with Mirrored Queues

Page 50: RabbitMQ fairly-indepth

HA with Mirrored Queues

Here is an example of what queues look like when it is replicated/mirrored across multiple nodes. “+2” means it’s on two other nodes.

Page 51: RabbitMQ fairly-indepth

HA with Mirrored Queues

The most powerful part of this approach is that the policy is applied to queues using a regex pattern. This makes it extremely powerful.

Page 52: RabbitMQ fairly-indepth

HA with Mirrored Queues

How it would typically look like. The “definition” is the name you gave the policyMost powerful part is you can create something similar to namematch-two-only. Basically this means any future queues that ends with *.ha.two will be automatically mirrored across 2 nodes, like the next slide

Page 53: RabbitMQ fairly-indepth

HA with Mirrored Queues

you can see here app.info.ha.two and event.share.ha.two is a “+1”

Page 54: RabbitMQ fairly-indepth

HA with Mirrored Queues

Walkthrough and demo

Page 55: RabbitMQ fairly-indepth

Distributed RabbitMQ

Clustering Replication

➡Acts as a single logical unit

➡Consumer sees cluster as single node

➡Nodes shares the same Erlang cookie

➡Automatic metadata replication, etc

➡ Requires reliable LAN-like environment

➡ Emphasis on Consistency & Availability

➡Acts like a forwarding agent

➡Nodes doesn’t share any cookie and is standalone

➡Uses AMQP protocol itself for forwarding

➡ Suitable for unreliable WAN connections

➡ Emphasis on Availability & Partition Tolerance

HA

➡A combo of both

➡ It’s cluster with replication

➡Usually in form of master/slave

➡ Emphasis on Consistency & Availability

Page 56: RabbitMQ fairly-indepth

Distributed RabbitMQReplication

Page 57: RabbitMQ fairly-indepth

Distributed RabbitMQReplication

SydneyMelbourne

rabbit01 rabbit01

shovel

federation

• consumes and republishes• exchange to exchange• works well across WAN• two main difference:

• shovel is lower level• shovel is more flexible

Two main plugins - shovel and federation. Both are very very similar but it seems there’s more push for Shovel. Both does the same things listed here...

Page 58: RabbitMQ fairly-indepth

Distributed RabbitMQReplication

shovel

Melbourne

rabbit01

rabbit02

rabbit02

Sydney

syd.rb01

syd.rb03syd.rb03

Idea is to have local rabbitmq on web nodes re-publishing messages across WAN to a cluster.

Page 59: RabbitMQ fairly-indepth

Replication with Shovel

Walkthrough and demo

Page 60: RabbitMQ fairly-indepth

Q&A