Sunday, 22 March 2015

Rabbit MQ basic concepts

In this post we will try to understand the basic concepts of Rabbit MQ, components involved in transmission of messages, basic api's required in spring-amqp and so on...

RabbitMQ: RabbitMQ is open source message broker software that implements the Advanced Message Queuing Protocol (AMQP).

AMQP: The Advanced Message Queuing Protocol (AMQP) is an open standard application layer protocol for sending and receiving messages between distributed systems.

Message Brokers: Message brokers receive messages from the publishers and route them to consumers.

Bindings: Relationship between the exchange and the queue is called binding

Rabbit MQ working:
  1. Messages are published to the exchange
  2. Exchange distributes the message copies to queues using binding rules
  3. Consumer application receives the messages. In AMQP model there are two ways for applications to consume
    1. Have message delivered to the consumer application(push API)
    2. Fetch messages as needed(pull API)


Exchange types: Exchanges take a message and route it into zero or more queues. The routing algorithm used depends on the exchange type and bindings
There are 4 types of exchanges:
  1. Direct exchange: A direct exchange delivers messages to queues based on the message routing key.
  2. Fanout exchange: A fanout exchange routes messages to all of the queues that are bound to it and the routing key is ignored.
  3. 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.
  4. Headers exchange: A headers exchange is designed for routing on multiple attributes that are more easily expressed as message headers than a routing key.
Message acknowledgements
Consumer applications may fail to process the messages due various reasons like network issue or system failure etc.
AMQP specification proposes two choices for this:
  1. Delete the message from the queue, when broker sends the message to the application (automatic acknowledgement model: using either basic.deliver or basic.get-ok AMQP method)
  2. Delete the message from the queue, when the application sends back the acknowledgement (explicit acknowledgment model: using basic.ack AMQP method)
Important Spring AMQP API’s for basic operations:

spring-amqp is the base abstraction and spring-rabbit is the RabbitMQ implementation
There are mainly 3 important basic classes we need to know before starting the development:
  1. CachingConnectionFactory: Used for creating connections
  2. RabbitAdmin: Used for declaring exchanges, queues, bindings
  3. RabbitTemplate: Specifies basic set of AMQP operations
In the next post, we will see how to configure fanout exchange using spring amqp and exchange messages between producer and consumer applications

No comments:

Post a Comment