DEV Community

meet
meet

Posted on

RabbitMQ Basic Overview

RabbitMQ is a messaging broker and supports AMQP 0-9-1 (Advanced Message Queuing Protocol) protocol.

RabbitMQ Broker

RabbitMQ allows for communication by providing support for applications that publish a message to it (i.e broker) and it delivers the messages to consumer applications.

RabbitMQ main components/concepts:

Exchange

Exchanges are entities where messages are sent.
It take a message and route it into zero or more queues based on routing algorithm.
Routing algorithm depends on exchange type and rules called bindings.
Queues are bound to exchanges by using the routing key.

Exchange Type

AMQP 0-9-1 brokers provide four exchange types:
Exchange Type Listing

Default Exchange

The default exchange is a direct exchange with no name (empty string) predeclared by the broker.
It has one special property that makes it very useful for simple applications: every queue that is created is automatically bound to it with a routing key which is the same as the queue name.

Direct Exchange

A direct exchange delivers messages to queues based on the message routing key.

Direct Exchange

Direct exchanges can be used to distribute tasks between multiple workers as seen in above diagram.

Publisher 1 publishes messages with 'emails.marketing' routing key to direct exchange.
Queue 2 is bounded to direct exchange with 'emails.marketing' routing key, so exchange forwards the such messages to Queue 2.
Consumer 3 is listening to Queue 2 and will hence receive such messaged.

Direct exchanges can be used to distribute messages between multiple workers [i.e consumers] as seen in above diagram.
These messages are load balanced between consumers in a round robin manner and not between queues.

Publisher 2 publishes messages with 'emails.sales' routing key to direct exchange.
Queue 1 and Queue 3 are bounded to direct exchange with 'emails.sales' routing key, so exchange makes 2 copies of received messages and forwards one each to to Queue 1 and Queue 2.
Consumer 1 and Consumer 2 are listening to Queue 1. Each of them will receive unique message for processing in round robin manner. That is a single message cannot be consumed by multiple consumers.

Fanout Exchange

A fanout exchange routes messages to all of the queues that are bound to it and the routing key is ignored.

Fanout Exchange

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.

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.

Two special cases for binding keys in topic exchange:

  • * (star) can substitute for exactly one word.
  • # (hash) can substitute for zero or more words and if used alone to bind queue, it will behave like fan-out exchange

Topic Exchange

The first word in the routing key here describes a communication-method, second a business function and third personalized: [communication-method].[business].[personalized].

As in above diagram,
Colons can be used to align columns.

Routing Key Queue Delivered To
sms.marketing.all Q2
emails.sales.specific Q1,Q3
sms.sales.specific Q3
sms.sales.specific.all Q3
emails.sales.specific.all Q3
emails.marketing.specific.all Lost message
References

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

Top comments (0)