Understanding AMQP Messaging with RabbitMQ

Posted on : 16th april 2013
By : Romil Gupta

In a nutshell : Giving Introduction to AMQP Messaging  with RabbitMQ

What’s  RabbitMQ & What it does ?

RabbitMQ is a lightweight, reliable, scalable and portable message broker. Its a robust messaging for application, it gives your applications a common platform to send and receive messages, and your messages a safe place to live until received. 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.

Generally RabbitMQ runs on all major Operating systems like windows, Unix, Linux etc. and its easy to use. It supports a huge number of developer platforms like Erlang, Java/JVM, Ruby ,PythonPerl , PHP etc.

RabbitMQ is based on messaging protocol, in which your applications communicate with it via a platform-neutral, wire-level protocol: the Advanced Message Queuing Protocol (AMQP) .

AMQP is a message protocol that deals with publishers and consumers. The publishers produce the messages, the consumers pick them up and process them. It’s the job of the message broker (such as RabbitMQ) to ensure that the messages from a publisher go to the right consumers. In order to do that, the broker uses two key components: exchanges and queues. The following diagram shows how they connect a publisher to a consumer:

rabbitmqfig1

* Note that since AMQP is a network protocol the publisher, consumer, and broker do not have to reside on the same machine; indeed in most applications they don’t.

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 publishers can send messages that go to one queue, many consumers can try to receive data from one queue.

The core idea in the messaging model in RabbitMQ is that the producer never sends any messages directly to a queue. Actually, quite often the publisher doesn’t even know if a message will be delivered to any queue at all.

Instead, the publisher can only send messages to an exchange. Exchanges then distribute message copies to queues . How are the messages routed from the exchange to the queue? Good question. First, the queue has to be attached to the given exchange. Typically, a consumer creates a queue and attaches it to an exchange at the same time. Second, messages received by the exchange have to be matched to the queue –the relationship between exchange and a queue is called a “binding “.

The exchange must know exactly what to do with a message it receives. Should it be appended to a particular queue? Should it be appended to many queues? Or should it get discarded. The rules for that are defined by the exchange type.

There are a few exchange types available: directtopicheaders and fanout.

Direct Exchange : A direct exchange delivers messages to queues based on the message routing key. A direct exchange is ideal for the unicast routing of messages (although they can be used for multicast routing as well).

rabbitmqfig2

Fanout Exchange : A fanout exchange routes messages to all of the queues that are bound to it and the routing key is ignored. If N queues are bound to a fanout exchange, when a new message is published to that exchange a copy of the message is delivered to all N queues. Fanout exchanges are ideal for the broadcast routing of messages.

rabbitmqfig3

Topic Exchange : Topic exchanges route messages to one or many queues based on matching between a message routing key and the pattern that was used to bind a queue to an exchange. The topic exchange type is often used to implement various publish/subscribe pattern variations. Topic exchanges are commonly used for the multicast routing of messages.

rabbitmqfig4

where:

  • *  (star) can substitute for exactly one word.
  • # (hash) can substitute for zero or more words.

Headers Exchange : A headers exchange is designed to for routing on multiple attributes that are more easily expressed as message headers than a routing key. Headers exchanges ignore the routing key attribute. Instead, the attributes used for routing are taken from the headers attribute. A message is considered matching if the value of the header equals the value specified upon binding.

Now lets discuss application of RabbitMQ……

In general, a message bus (such as RabbitMQ, but not limited to) allows for a reliable queue of job processing.What this means to you in terms of a web application is the ability to scale your app as demand grows and to keep your UI quick and responsive. Instead of forcing the user to wait while a job is processed they can request a job to be processed (for example, clicking a button on a web page to begin the search for flight at makemytrip) which sends a message to your bus, let’s the backend service pick it up when it’s turn in the queue comes up, and maybe notify the user that work has/will begin. You can then return control to the UI, so the user can continue working with the application.In this situation, your web interface does zero heavy lifting, the job could incrementally update database records with the state of process which you can query and display to the user.

In my upcoming post I am going to share my understanding on Introduction to openstack and what is the role of RabbitMQ in Openstack.

Keep reading …

References:

For more details on AMQP :

http://www.rabbitmq.com/tutorials/amqp-concepts.html

http://blog.springsource.org/

For doing hands-on :

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

Advertisements

About Romil Gupta
" Learn , Earn , Discover and share the knowledge ". I feel enthusiastic in working on Openstack Cloud, programming in Python.

One Response to Understanding AMQP Messaging with RabbitMQ

  1. Pingback: Messaging in Openstack using RabbitMQ | iLearnStack

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: