EnergyBoy
EnergyBoy

Reputation: 635

RabbitMQ multiple consumer subscribe same queue and get same message

I am considering that can multiple consumer get the "same" message from a "same" queue which they subscribed ?

It's mean that consumer_1 and consumer_2 are both subscribe queue_1, when a single message is publish by publisher, can two of this consumer get that message at the same time ?

If yes, how can I implement it ?

Upvotes: 4

Views: 14464

Answers (3)

jmcgrory
jmcgrory

Reputation: 843

The other answers miss a key element with CloudAMQP which is that during the acknowledgement of Messages, a Consumer can choose to requeue any given Message. Effectively allowing multiple consumers to see it.

The requeued Message continues onto the "next" consumer (in the order that they connected to the Queue).

I would personally recommend multiple Queues however just for speed, however if you explicitly want all consumers to see a specific Message (and remove it when one consumer explicitly acknowledges it), then it can be a good option to prevent duplication of actions.

See this PHP implementation, or otherwise search "requeue" in CloudAMQP docs.

Upvotes: 0

It is not possible. A particular message from a queue cannot be consumed by more than one consumer.

Remember in AMQP, messages are always consumed from queue.

  1. A message from the queue is only consumed by one consumer
  2. You can have competing consumers sharing the work load from a queue

Very high level AMQP flow

  1. The producer publishes a message to an exchange.
  2. The exchange receives the message and routes it to queues.(Based on message attributes, exchange type and bindings)
  3. The consumer handles the message or it stays in the queue until it is consumed.

You achieve different message exchange patterns in AMQP based on what exchange type and bindings you create step 2. Whether it is point to point, pub sub, multi cast, it will be based on what happens in step 2.

A nice article with nice diagrams

https://www.cloudamqp.com/blog/2015-09-03-part4-rabbitmq-for-beginners-exchanges-routing-keys-bindings.html#standard-rabbitmq-message-flow

enter image description here

enter image description here

Upvotes: 10

code_kbd
code_kbd

Reputation: 486

This is not possible. A single message can be delivered to one consumer of a queue at a time. However, it is possible to route a single message to multiple queues though a single exchange. Refer to different forms of bindings and exchanges offered by rabbitMQ.

However, note that rabbitmq offers the option of requeuing and nacks.

Upvotes: 3

Related Questions