Академический Документы
Профессиональный Документы
Культура Документы
RabbitMQ and
CloudAMQP
Lovisa Johansson
Book version: 2.0
Published: 2017-09-04
Copyright © 84 Codes AB
Table of Contents
Introduction
What is RabbitMQ?
The Management Interface
Exchanges, routing keys and bindings
RabbitMQ and Ruby
RabbitMQ and Node.js
RabbitMQ and Python
A microservice architecture built upon RabbitMQ
RabbitMQ Troubleshooting
More Resources
Introduction
This book gives a gentle introduction to RabbitMQ and how to use it on the
CloudAMQP online platform. This book originated as a series of blog posts
on the CloudAMQP website. The continued success of these posts led us to
the idea to summarize the content in book form, to make it easy to digest for
different readers.
Thank you to all CloudAMQP users for your feedback and continued
support. It has been a great motivator to see customer projects succeed.
What is RabbitMQ?
The first chapter of Getting Started with
RabbitMQ and CloudAMQP explains what
RabbitMQ and message queueing is - a way of
exchanging data between processes,
applications, and servers.
A message can include any kind of information. It could, for example, have
information about a process/task that should start on another application
(that could be on another server), or it could be just a simple text message.
The queue-manager software stores the messages until a receiving
application connects and takes a message off the queue. The receiving
application then processes the message in an appropriate manner.
Figure: A typical message queue where messages are being sent
from a sender to a receiver.
RabbitMQ Example
A message broker solution can as a middleman for various services (e.g., a
web application, as in this example). They can be used to reduce loads and
delivery times by web application servers since tasks, which would normally
take quite a bit of time to process, can be delegated to a third party whose
only job is to perform them.
When the user has entered user information into the web interface, the web
application will put a "PDF processing" - task and all information into a
message and the message will be placed onto a queue defined in
RabbitMQ.
The consumer can take a message of the queue and start the processing of
the PDF at the same time as the producer is queueing up new messages on
the queue. The consumer can be on a totally different server than the
publisher, or they can be located on the same server. Requests can be
created in one programming language and handled in another programming
language - the two applications will only communicate through the
messages they are sending to each other. Due to that, the two applications
will have a low coupling between the sender and the receiver.
Exchanges
Messages are not published directly to a queue, instead, the producer
sends messages to an exchange. An exchange is responsible for the
routing of the messages to the different queues. An exchange accepts
messages from the producer application and routes them to message
queues with the help of bindings and routing keys. A binding is a link
between a queue and an exchange.
Types of exchanges
Direct exchanges are used in chapter 4, with sample code in Ruby, Node.js
and Python. Deeper understanding about the different exchange types,
binding keys, routing keys and how/when you should use them can be
found in chapter 3 about exchanges: Chapter 3: Exchanges, routing keys
and bindings.
When your instance is created, click on "details" for your instance to find
your username, password and connection URL for your cloud-hosted
RabbitMQ instance.
Figure: Instances in the CloudAMQP web interface.
Sample code
Sample code will be given in later chapters, with Ruby, Node.js, and Python.
It is possible to use different programming languages on different parts of
the system. The subscriber could, for example, be written in Node.js, and
the subscriber in Python.
All the tabs from the menu are explained in this post. Screenshots from the
views are shown for: Overview, Connections, and channels, Exchanges,
Queues, and Admin - users and permissions. A simple example will also
show how to set up a queue an exchange and add a binding between them.
Figure: The RabbitMQ management interface.
Concepts
In the first chapter, the default virtual host, user and permissions were used
in the examples. In this chapter, different views in the management
interface are shown, and all the examples are still only working with the
default values - still, there are some important concepts that are good to be
familiar with.
Users: Users can be added from the management interface, and every
user can be assigned permissions such as rights to read, write and
configure privileges. Users can also be assigned permissions to specific
virtual hosts.
Vhost, virtual host: Virtual hosts provide a way to segregate applications
using the same RabbitMQ instance. Different users can have different
access privileges to different vhosts and queues, and exchanges can be
created so they only exist in one vhost.
Cluster: A cluster consists of a set of connected computers that work
together. If the RabbitMQ instance consists of more than one node - it is
called a RabbitMQ cluster. A cluster is a group of nodes i.e., a group of
computers.
Node: A node is a single computer in the RabbitMQ cluster.
Overview
The overview shows two charts, one for queued messages and one with the
message rate. You can change the time interval shown in the chart by
clicking the text (chart: last minute) above the charts. Information about all
different statuses for messages can be found by pressing "?".
Figure: The RabbitMQ overview window, with one chart for queued
messages one chart for the message rate.
Queued messages
A chart of the total number of queued messages for all your queues. Ready
shows the number of messages that are available to be delivered.
Unacked are the number of messages for which the server is waiting for
acknowledgement.
Messages rate
A chart with the rate of how the messages are handled. Publish shows the
rate at which messages are entering the server and Confirm shows a rate
at which the server is confirming.
Global Count
Nodes
Nodes show information about the different nodes in the RabbitMQ cluster
(a cluster is a group of nodes i.e, a group of computers), or information
about one single node if only one node is used. Here information about
server memory, number of Erlang processes per node, and other node-
specific information can be found. Info show i.e., further information about
the node and enabled plugins.
Figure: Node-specific information.
Import/export definitions
Connections
If you click on one of the connections, you get an overview of that specific
connection. You can view channels within the connection and its data rates.
You can see client properties, and you can close the connection.
Channels
The channel tab shows information about all current channels. The vhost
shows in which vhost the channel operates, the username the user
associated with the channel. The mode tells the channel guarantee mode. It
can be in confirm or transactional mode. When a channel is in confirm
mode, both the broker and the client count messages. The broker then
confirms messages as it handles them. Confirm mode is activated once the
confrm.select method is used on a channel.
Figure: The Channels tab with information about all current channels.
If you click on one of the channels, you get a detailed overview of that
specific channel. From here you can see the message rate and the number
of logical consumers retrieving messages via the channel.
Exchanges
Queues
The queue tab shows the queues for all or one selected vhost.
Figure: The queues tab in the RabbitMQ management interface.
If you press on any chosen queue from the list of queues, all information
about the queue is shown like in the pictures that follow below.
The first two charts include the same information as the overview, but they
just show the number of queued messages and the message rates for that
specific queue.
Consumers
Bindings
Publish message
Get message
A queue can be deleted by clicking the delete button, and you can empty the
queue by clicking purge.
Admin
From the Admin view, it is possible to add users and change user
permissions. You can set up vhosts,policies, federation and shovels.
Information about shovels can be found here:
https://www.rabbitmq.com/shovel.html and
https://www.cloudamqp.com/docs/shovel.html.Information about federation
can be found here: https://www.cloudamqp.com/blog/2015-03-24-rabbitmq-
federation.html
Example
This example shows how you can create a queue "example-queue" and an
exchange calledexample.exchange.
Figure: Queue view: Add queue
The routing key is a message attribute. The exchange might look at this
key when deciding how to route the message to queues (depending on
exchange type).
In RabbitMQ, there are four different types of exchanges that route the
message differently using different parameters and bindings setups. Clients
can create their own exchanges, or use the predefined default exchanges,
the exchanges created when the server starts for the first time.
Exchange types
Direct Exchange
A direct exchange delivers messages to queues based on a message
routing key. The routing key is a message attribute added into the message
header by the producer. The routing key can be seen as an "address" that
the exchange uses to decide how to route the message. A message goes
to the queue(s) whose binding key exactly matches the routing key of
the message.
The direct exchange type is useful when you would like to distinguish
messages published to the same exchange using a simple string identifier.
The default exchange AMQP brokers must provide for the direct exchange
is "amq.direct".
Scenario 1
Exchange: pdf_events
Queue A: create_pdf_queue
Binding key between exchange (pdf_events) and Queue A
(create_pdf_queue): pdf_create
Scenario 2
Exchange: pdf_events
Queue B: pdf_log_queue
Binding key between exchange (pdf_events) and Queue B
(pdf_log_queue): pdf_log
Example
If the message routing key does not match any binding key, the message
will be discarded.
Figure: Direct Exchange Figure: A message goes to the queues
whose binding key exactly matches the routing key of the message.
Default exchange
Topic Exchange
Topic exchanges route messages to queues based on wildcard matches
between the routing key and something called the routing pattern specified
by the queue binding. Messages are routed to one or many queues based
on a matching between a message routing key and this pattern.
The routing key must be a list of words, delimited by a period (.), examples
are agreements.us and agreements.eu.stockholm which in this case
identifies agreements that are set up for a company with offices in lots of
different locations. The routing patterns may contain an asterisk (“*”) to
match a word in a specific position of the routing key (e.g. a routing pattern
of "agreements.*.*.b.*" will only match routing keys where the first word is
"agreements" and the fourth word is "b"). A pound symbol (“#”) indicates
match on zero or more words (e.g. a routing pattern of
"agreements.eu.berlin.#" matches any routing keys beginning with
"agreements.eu.berlin").
The consumers indicate which topics they are interested in (like subscribing
to a feed for an individual tag). The consumer creates a queue and sets up
a binding with a given routing pattern to the exchange. All messages with
a routing key that match the routing pattern will be routed to the queue
and stay there until the consumer consumes the message.
The default exchange AMQP brokers must provide for the topic exchange is
"amq.topic".
Scenario 1
Exchange: agreements
Queue A: berlin_agreements
Routing pattern between exchange (agreements) and Queue A
(berlin_agreements): agreements.eu.berlin.#
Example of message routing key that will match: agreements.eu.berlin
and agreements.eu.berlin.headstore
Scenario 2
Exchange: agreements
Queue B: all_agreements
Routing pattern between exchange (agreements) and Queue B
(all_agreements): agreements.#
Example of message routing key that will match: agreements.eu.berlin
and agreements.us
Scenario 3
Exchange: agreements
Queue C: headstore_agreements
Routing pattern between exchange (agreements) and Queue C
(headstore_agreements): agreements.eu.*.headstore
Example of message routing keys that will match:
agreements.eu.berlin.headstore and agreements.eu.stockholm.headstore
Example
Fanout Exchange
The fanout exchange copies and routes a received message to all
queues that are bound to it regardless of routing keys or pattern matching
as with direct and topic exchanges. Keys provided will simply be ignored.
Fanout exchanges can be useful when the same message needs to be sent
to one or more queues with consumers who may process the same
message in different ways.
The default exchange AMQP brokers must provide for the fanout exchange
is "amq.fanout".
Scenario 1
Exchange: sport_news
Queue A: Mobile client queue A
Binding: Binging between the exchange (sport_news) and Queue A
(Mobile client queue A)
Example
Headers Exchange
Headers exchanges route based on arguments containing headers and
optional values.Headers exchanges are very similar to topic exchanges, but
it routes based onheader values instead of routing keys. A message is
considered matching ifthe value of the header equals the value specified
upon binding.
A special argument named "x-match" tells if all headers must match, or just
one.The "x-match" property can the have two different values: "any" or "all",
where "all" is thedefault value. A value of "all" means all header pairs (key,
value) must match and a valueof "any" means at least one of the header
pairs must match. Headers can be constructedusing a wider range of data
types - integer or hash for example instead of a string.The headers
exchange type (used with "any") is useful for directing messages whichmay
contain a subset of known (unordered) criteria.
The default exchange AMQP brokers must provide for the headers
exchange is "amq.headers".
Scenario 2
Scenario 3
This part of the book creates a web application that allows users to upload
user information. The website will handle the information, generate a PDF
and email it back to the user. Generating the PDF and sending the email will
in this example case take several seconds.
The full code for the publisher and the consumer is first given, after that the
code will be divided into blocks and explained per code block.
Figure: The Ruby application architecture.
Full code
#example_publisher.rb
require "rubygems"
require "bunny"
require "json"
x.publish(information_message,
:timestamp => Time.now.to_i,
:routing_key => "process"
)
sleep 1.0
conn.close
#example_consumer.rb
require "rubygems"
require "bunny"
require "json"
#example_consumer.rb
require "rubygems"
require "bunny"
require "json"
Create a channel
ch.queue
is used to declare a queue with a particular name, in this case,
the queue is called
examplequeue.
The queue in this example is marked as
durable, meaning that RabbitMQ
will never lose our queue.
Publish a message
x.publish(information_message,
:timestamp => Time.now.to_i,
:routing_key => "process"
)
subscribe takes a block and processes it. It will be called every time a
message arrives.
More information about Ruby and CloudAMQP can be found in the online
documentationhere.
This tutorial follows the scenario used in the previous chapter where a web
application allows users to upload user information. The website will handle
the information and generate a PDF and email it back to the user.
Generating the PDF and sending the email will in this scenario take several
seconds.
file.
When running the full code given, a connection will be established between
the RabbiMQ instance and your application. Queues and exchanges will be
declared and created if they do not already exist and finally a message will
be published. The publish method will queue messages internally if the
connection is down and resend them later. The consumer subscribes to the
queue. The messages are handled one by one and sent to the PDF
processing method.
function start() {
amqp.connect(process.env.CLOUDAMQP_URL + "?heartbeat=60", fu
nction(err, conn) {
if (err) {
console.error("[AMQP]", err.message);
return setTimeout(start, 1000);
}
conn.on("error", function(err) {
if (err.message !== "Connection closing") {
console.error("[AMQP] conn error", err.message);
}
});
conn.on("close", function() {
console.error("[AMQP] reconnecting");
return setTimeout(start, 1000);
});
console.log("[AMQP] connected");
amqpConn = conn;
whenConnected();
});
}
amqpConn will hold the connection and channels will be set up in the
connection.
function whenConnected() {
startPublisher();
startWorker();
}
The function whenConnected calls two functions, one function that starts the
publisherand one that starts the worker (the consumer).
pubChannel = ch;
while (true) {
var m = offlinePubQueue.shift();
if (!m) break;
publish(m[0], m[1], m[2]);
}
});
}
Publish
Consumer
ch.prefetch(10);
ch.assertQueue("jobs", { durable: true }, function(err, _o
k) {
if (closeOnErr(err)) return;
ch.consume("jobs", processMsg, { noAck: false });
console.log("Worker is started");
});
function processMsg(msg) {
work(msg, function(ok) {
try {
if (ok)
ch.ack(msg);
else
ch.reject(msg, true);
} catch (e) {
closeOnErr(e);
}
});
}
});
}
processMsg processes the message. It will call the work function and wait
for it to finish.
The work function includes the handling of the message information and
the creation of the PDF. It is in this example a todo-function.
function closeOnErr(err) {
if (!err) return false;
console.error("[AMQP] error", err);
amqpConn.close();
return true;
}
Publish
setInterval(function() {
publish("", "jobs", new Buffer("work work work"));
}, 1000);
start();
More information about Node.js and CloudAMQP can be found in the online
documentationhere.
This tutorial follows the scenario used in the previous two chapters, where a
web application allows users to enter user information into a website. The
website will handle the information and generate a PDF and email it back to
the user. Generating the PDF and sending the email will in this scenario
take several seconds.
You need a RabbitMQ instance to get started. Read about how to set upan
instance in chapter 1.
When running the full code given, a connection will be established between
the RabbiMQ instance and your application. Queues and exchanges will be
declared and created if they do not already exist and finally a message will
be published. The consumer subscribes to the queue, and the messages
are handled one by one and sent to the PDF processing method.
A default exchange, identify by the empty string ("") will be used. The default
exchange means that messages are routed to the queue with the name
specified by routing_key if it exists. (The default exchange is a direct
exchange with no name)
Figure: Python and RabbitMQ application architecture
Full code
# example_publisher.py
# example_publisher.py
import pika, os, logging
logging.basicConfig()
channel.basic_publish(exchange='', routing_key='pdfprocess',
body='User information')
print("[x] Message sent to consumer")
connection.close()
# example_consumer.py
# example_consumer.py
import pika, os, time
def pdf_process_function(msg):
print(" PDF processing")
print(" Received %r" % msg)
Set up a connection
Start a channel
channel = connection.channel()
Declare a queue
Publish a message
channel.basic_publish(exchange='', routing_key='pdfprocess', b
ody='User information')
print("[x] Message sent to consumer")
connection.close()
The connection will be closed after the message has been published.
Consumer
Worker function
def pdf_process_function(msg):
print(" PDF processing")
print(" Received %r" % msg)
The callback function will be called on every message received from the
queue.The function will call a function that simulate thePDF-processing.
CloudAMQP never had a traditional monolithic setup. It’s built from scratch
on small, independent, manageable services that communicate with each
other. These services are all highly decoupled and focused on their task.This
chapter gives an overview and a deeper insight to the automated process
behindCloudAMQP. It describes some of our microservices and how we are
usingRabbitMQ as message broker when communicating between services.
Background of CloudAMQP
A few years ago Carl Hörberg (CEO @CloudAMQP) saw the need of a
hosted RabbitMQ solution. At the time he was working at a consultancy
company where he was using RabbitMQ in combination with Heroku and
AppHarbor. He was looking for a hosted RabbitMQ solution himself, but he
could not find any. Short after he started CloudAMQP, which entered the
market in 2012.
When you create a CloudAMQP instance you get the option to choose a
plan andhow many nodes you would like to have. The cluster will behave abit
different depending on the cluster setup. You also get the option to create
your instance in a dedicated VPC and select RabbitMQ version.
CloudAMQP-server-metrics
CloudAMQP-alarm
We have different services that are subscribing to this data. One of these
servicesis called CloudAMQP-alarm. A user is able to enable/disable CPU
and memory alarmsfor an instance. CloudAMQP-alarm check the server
metrics subscribedfrom RabbitMQ against the alarm thresholds for the given
server and it notifiesthe owner of the server if needed.
CloudAMQP-put-metrics
Troubleshooting RabbitMQ
General tips and tools to keep in mind once you start troubleshooting your
RabbitMQ server.
Server Metrics
Server Metrics will show performance metrics from your server.CloudAMQP
shows CPU Usage Monitoring and Memory Usage Monitoring. Do you see
any CPU or memory spikes? Do you notice some changes in the graph
during a specific time? Did you do any changes in your code around that
time?
Event stream
The event stream allows you to see the latest 1000 events from your
RabbitMQcluster. New events will be added to the collection in real time.The
event stream can be useful when you need to gain insight into what is
happening in your cluster. It is particularly good to debug if you are running
into high CPU-usage, for instance, if you have rapid opening or closing of
connections or channels or a setup for shovel that is not working, etcetera.
You should also check if your consumers are connected - consumers alarm
can beactivated from the console. The alarm will be triggered to send
notificationswhen the number of consumers for a queue is less than or equal
to a givennumber of consumers.
If you are running RabbitMQ version 3.4 or lower - upgrade - with RabbitMQ
3.5and newer you can basically queue until the disk runs out unlike in 3.4
wherethe message index had to be kept in memory at all time. You can
upgrade theversion from the console (next to where you reboot/restart the
instance).
We recommend you to enable the queue length alarm from the console or,if
possible, to set a max-length or a max-ttl on messages in your queues.
If you already have too many messages in the queue - you need to start
consumingmore messages or get your queues drained. If you are able to
turn off yourpublishers then do that until you have managed to consume the
messages from the queue.
Dedicated plans in CloudAMQP are not artificially limited in any way, the
maximum performance is determined by the underlying instance type.Every
plan has a given Max msgs/s, that is the approximate burst speed we have
measured during benchmark testing. Burst speed means that youtemporarily
can enjoy the highest speed but after a certain amount of time or once the
quota has been exceeded, you might automatically be knocked down to a
slower speed.
The benchmark tests are done with RabbitMQ PerfTest tool with one queue,
one publisher and one customer. Durable queues/exchanges have been
used andtransient messages with default message size have been used.
There are a lot ofother factors too that plays in, like type of routing, if you ack
orauto-ack, datacenter, if you publish with confirmation or not etc. If you for
example are publishing large persistent messages, it will result in alot of I/O
wait time for the CPU. Read more about Load testing and performance
measurements in RabbitMQhere.
When you have too high message throughput you should either try to
decrease theaverage load or upgrade/migrate to a larger underlying instance
type. If youwant to rescale your cluster, go to the CloudAMQP Control Panel,
andchoose edit for the instance you want to reconfigure. Here you have the
ability to add or remove nodes and change plan altogether. More about
migration can be foundhere.
Python Celery
Python Celery
If you use Python Celery, make sure that you either consume the results or
disable the result backend (set CELERY_RESULT_BACKEND = None).
Each connection uses at least one hundred kB, even more if TLS is used.
If you really need many connections - the CloudAMQP team can configure
the TCP buffer sizes to accommodate more connections for you. Send us
an email when you have created your instance and we will configure the
changes.
If you are on plan Big Bunny on AWS you are on T2-instances. If you, for
example, have too high message throughput during a too long time you will
run out of CPU credits. When that happens, steal time is added and your
instance starts throttle (your instance will be running slower than it should).
If you notice high steal time you need to upgrade to a larger instance or
identify the reason for your CPU usage.