RabbitMQ - RabbitMQ tutorial

If you're having trouble going through this tutorial you can contact us through the discussion list or directly.RabbitMQ is a message broker. The principal idea is pretty simple: it accepts and forwards messages. You can think about it as a post office: when you send mail to the post box you're pretty sure that Mr. Postman will eventually deliver the mail to your recipient. Using this metaphor RabbitMQ is a post box, a post office and a postman.

The major difference between RabbitMQ and the post office is the fact that it doesn't deal with paper, instead it accepts, stores and forwards binary blobs of data ‒ messages.

RabbitMQ, and messaging in general, uses some jargon.

  • Producing means nothing more than sending. A program that sends messages is a producer. We'll draw it like that, with "P":

    digraph { bgcolor=transparent; truecolor=true; rankdir=LR; node [style="filled"]; // P1 [label="P", fillcolor="#00ffff"]; }

    A queue is the name for a mailbox. It lives inside RabbitMQ. Although messages flow through RabbitMQ and your applications, they can be stored only inside a queue. A queue is not bound by any limits, it can store as many messages as you like ‒ it's essentially an infinite buffer. Many producers can send messages that go to the one queue, many consumers can try to receive data from one queue. A queue will be drawn as like that, with its name above it:

    digraph { bgcolor=transparent; truecolor=true; rankdir=LR; node [style="filled"]; // subgraph cluster_Q1 { label="queue_name"; color=transparent; Q1 [label="{||||}", fillcolor="red", shape="record"]; }; }

    Consuming has a similar meaning to receiving. A consumer is a program that mostly waits to receive messages. On our drawings it's shown with "C":

    digraph { bgcolor=transparent; truecolor=true; rankdir=LR; node [style="filled"]; // C1 [label="C", fillcolor="#33ccff"]; }

    Hello World!

    (using the pika 0.9.5 Python client)

    Our "Hello world" won't be too complex ‒ let's send a message, receive it and print it on the screen. To do so we need two programs: one that sends a message and one that receives and prints it.

    Our overall design will look like:

    digraph G { bgcolor=transparent; truecolor=true; rankdir=LR; node [style="filled"]; // P1 [label="P", fillcolor="#00ffff"]; subgraph cluster_Q1 { label="hello"; color=transparent; Q1 [label="{||||}", fillcolor="red", shape="record"]; }; C1 [label="C", fillcolor="#33ccff"]; // P1 -> Q1 -> C1; }

    Producer sends messages to the "hello" queue. The consumer receives messages from that queue.

    RabbitMQ libraries

    RabbitMQ speaks a protocol called AMQP. To use Rabbit you'll need a library that understands the same protocol as Rabbit. There is a choice of libraries for almost every programming language. For python it's no different and there are a bunch of libraries to choose from:

    In this tutorial series we're going to use pika. To install it you can use the pip package management tool:

    The installation depends on pip and git-core packages, you may need to install them first.

  • On Ubuntu:

    On Debian:

    On ... Read more