Você está na página 1de 11

RabbitMQ Logical Components

Message Broker
Publisher

Consumer
queue

Machine 1

Machine 2

Machine 3

A publisher sends messages to the queue within the message broker and the consumer dequeues the
messages and delivers them to their destination. The message broker manages a series of message queues
which hold the actual messages to be sent and received. The publisher, message broker, and consumer can
be located on separate machines.

RabbitMQ: Publisher & Consumer Role

Message Broker
Publisher

Consumer
Queued
messages

A publishers responsibility is to send messages into the message broker queue. If a publisher sends
messages to the message broker queue, there is no consumer, and the publisher goes away, the messages
will persist inside the message brokers queue until there is a consumer to dequeue the messages.

RabbitMQ: Guaranteed Message Delivery


Message Broker
Publisher

Consumer
queue
Confirmation

ACK

If a publisher sends messages to the message broker queue, the broker sends back a confirmation to the
publisher acknowledging receipt of the message(s). When the consumer receives a message from the
message broker, the consumer sends an ACK to acknowledge receipt of the message(s) from the broker and
dequeues the messages from the broker.

RabbitMQ: Recovery from Failure Scenarios


Messages

Message Broker
Publisher

Consumer
queue
No
Confirmation

If the publisher tries to send a message to the message broker and there is a network failure between the
publisher and broker, the message can be stored in the queue, while the publisher doesnt receive a
confirmation of message delivery. This means that the publisher will retry the message delivery and thus
produce duplicate messages within the message queue.

RabbitMQ: Recovery from Failure Scenarios


ACK
Consumer

Messages

Consumer

Messages

Message Broker
Publisher
queue
No
ACK

If the consumer is accepting the messages from the queue, but it is not sending an ACK back to the message
queue, then the messages will remained queued. Should another consumer poll the message broker and send
an ACK after receiving the messages, in this scenario, the new consumer will not only dequeue the messages
within the queue, but it will contain duplicate messages due to redelivery. Inside the redelivered message is a
flag which alerts the message recipient that it is a duplicate message.

RabbitMQ: Message Queues


M1

Message Broker
Publisher

queue
M1

M2

M3

Consumer
Consumer

M3

M4
M2

M4

The ability of messaging queues to have multiple consumers, allows one to process messages in parallel. If
you have more than one consumer attached to a queue, the messages will be dispatched to the consumer
using a round-robin algorithm. Once consumer is finished with its current message, itll dequeue the next
message in line, etc, as shown in the above diagram.

RabbitMQ: Message Exchange


Routing methods
fanout

direct

topic
Binding

Publisher

Exchange

Message Broker
queue
M1

M2

Consumer
M3

M4

The publisher always sends a message to an exchange, then the queue binds to that exchange in order to
facilitate the queue receiving messages which the consumer then accepts via dequeuing. The AMQP
(advanced messaging and queuing protocol) protocol is what introduces the exchange into the
communications workflow. There are different types of methods for exchanges to route messages to the
queues: fanout, direct, and topic.

RabbitMQ: Fanout Exchange Routing


Message Broker
Consumer

queue
M1

Publisher

M2

M3

M4

Exchange

Message Broker
queue
Consumer
M1

M2

M3

M4

When the publisher sends a message to the exchange, in this model, the exchange can send messages to
more than one queue, since the queues are subscribing to messages from the exchange. In a fanout
exchange, all messages will go to each queue binding to that exchange.

RabbitMQ: Direct Exchange Routing


Message Broker

ple g ke
p
n
a
ndi
Bi

data
Routing key
Publisher

1
Consumer

queue
M1

M2

M3

M4

Exchange

Bi
or ndin
an g k
ge ey

Message Broker

queue
Consumer
M1

M2

M3

M4

In order for the system to work, the queue has to be listening on the exchange. When you bind the queue to the exchange, you must use a binding key
which filters where the messages must go to which queue. The binding key is a string. (i.e. apple, orange, etc) When the publisher sends a message to
the exchange, it must specify a routing key, which is also a string. The exchange uses the routing key to match the binding key in order to send the
message to the queue. For example, If the publisher specifies a particular routing key, in order for the exchange and queue to bind and move the
message along the communications path, the binding key must have the same string as the routing key. (i.e. if routing key is apple, then messages will
go to queue 1; if routing key is orange, messages will go to queue 2.)

RabbitMQ: Fanout versus Direct

Fanout Exchange:
ALWAYS deliver messages to binded queues REGARDLESS of bindings

Direct Exchange:
ONLY deliver messages to binded queues when routing key MATCHES bindings

RabbitMQ: Topic Exchange Routing


.*
pe key
a
sh ding
Bin

shape
Routing key
Publisher

Exchange

shape.#
Binding key

sh
Bin ape
din
gk
e

Message Broker

1
Consumer

queue

Message Broker

2
Consumer

queue

Message Broker
queue

3
Consumer

Star (*) can substitute for exactly one word; Hash (#) can substitute for zero or more words. The topic exchange supports the wildcard methodology,
where you will have more flexibility with the routing/binding key relationships. This routing method is a hybrid of the fanout and direct exchange routing
methodologies. For example, if the routing key is shape, messages will be delivered to queues 2 and 3, but not 1. Queue 1 requires matching exactly
one word, whereas queue 2 can match multiple variations of the word and queue 3 matches exactly. If the routing key was shape.circle, messages
would go to queues 1 and 2, because the filters on queues 1 and 2 will match both one and more than one word, whereas queue 3 must match exactly.
Note: The wildcard methodology can also be used before and after the string variable (i.e. *.shape.*) Also, if the routing key equals #, then it acts as
though its a fanout, thus matching any/all string variables.

Você também pode gostar