2

I'm a little confused. I'm trying to implement topic exchanges and am not sure what is needed.

I want to have several routing keys and 1 topic exchange (the default amq.topic). My keys would be like:

  • customer.appA.created
  • customer.appB.created
  • customer.*.created

I want my queue(s) to be durable, but do I need 1 'customer' queue or 2 queues for appA and appB? I have my publisher figured out; connect, exchange declare, basic publish.

But I'm struggling with the consumers. Let's say I want to open 3 consoles, one for each of the aforementioned routing keys.
My current consumer has: connect, exchange declare, queue bind, basic consume. These are connected to a durable 'customer' queue. However my messages are being round-robin'ed to each console/consumer and not using the routing keys.

So my questions;

  1. For a typical topic exchange set up; how many queues do you need?
  2. Can my consumers get away with just exchange binding, or does it have to include queue interaction?
  3. Is it possible for a single message to appear in 2 consumers with topic exchange (or do you need fanout for that)?
Matteo
  • 37,680
  • 11
  • 100
  • 115
Gerben Jacobs
  • 4,515
  • 3
  • 34
  • 56
  • Can you please specify what client library or extension do you use? – pinepain Jun 12 '14 at 08:50
  • videlalvaro/php-amqplib. But this is more of an abstract/contextual question. – Gerben Jacobs Jun 12 '14 at 10:48
  • There are some differences between php-amqp extension and php-amqplib library, so it matter which one is used in particular situation. For example with php-amqplib you can consume at the same time from multiple queues while with php-amqp you can't. – pinepain Jun 12 '14 at 11:02

2 Answers2

2

First thing first : Exchange do not deliver to Consumer. It delivers messages to Queue for matching routing kyes.

      1. For a typical topic exchange set up; how many queues do you need?

If you have multiple consumer then you will need one queue for every consumer.

      2. Can my consumers get away with just exchange binding, or does it have to include queue interaction?

You need to bind consumer with queue, if queue not exist then create and bind.

      3. Is it possible for a single message to appear in 2 consumers with topic exchange (or do you need fanout for that)?

YES, Only if the consumer have their own (separate queue bind with same routing key).Otherwise it will be Round Robin way.

So best way is to have consumer's own queue with required routing key...!!!

H. Mahida
  • 2,356
  • 1
  • 12
  • 23
  • Thanks. I was stuck in thinking of it in the Direct way. So I guess anonymous queues for each consumer would be the way to go then? – Gerben Jacobs Jun 11 '14 at 09:59
  • yes I have implemented the same, create queue at run time and bind it with you routing Key then start consumer for it. Also use "x-expires" for queue if you want to make sure it is deleted once not in use. – H. Mahida Jun 11 '14 at 10:08
  • `If you have multiple consumer then you will need one queue for every consumer` - this statement is wrong. You can attach multiple consumers to the same queue as well as consume from multiple queues at the same time inside one channel (if client library support that). – pinepain Jun 12 '14 at 08:14
  • @zaq178miami yes it is , but this answer is for TOPIC exchange, and as per need of question..!!! – H. Mahida Jun 12 '14 at 08:33
  • I provided the comment above why - the statement is not true not for topic exchange nor for any other. Even under the the terms of current question (but it really depends on which client library does questioner use). Based on rule to provide answer to general question, not specific one, I voted down. If I am wrong pleas explain why and I will try to do my best to get your point. I'm also a human being and may be wrong, so below is my answer, so let community to judge us. I'm not here for points nor for line in my CV but share my knowledge and study something new. Sorry if hurt your feelings. – pinepain Jun 12 '14 at 09:05
  • the reason for own queue for each consumer is that; in TOPIC exchange if you have multiple consumer of same queue all of them not receive all the messages delivered to that queue..!!! – H. Mahida Jun 12 '14 at 09:42
1

For a typical topic exchange set up; how many queues do you need?

It depends on your application needs. You may start from just one queue to implement simple FIFO stack and later add more queues with for more granulated messages consuming.

Can my consumers get away with just exchange binding, or does it have to include queue interaction?

The idea of AMQP exchanges is to get published messages and put them to one or more queues (or even other exchanges or drop at all under certain condition). Consumer works only with queues, while publisher works only with exchanges.

There might be some misunderstandings with Default Exchange while it route messages to the queue name same as a routing key and it sometimes interpreted as publishing to queues which is not true.

Is it possible for a single message to appear in 2 consumers with topic exchange (or do you need fanout for that)?

I guess you are talking about duplicating one message to multiple queues (while there are some erroneous situations when you really may have single message being processed by multiple consumers).

If so - sure. You can just create multiple bindings for queue to get different messages. Here is small example:

Let you have three messages published to topic exchange with three different routing keys customer.appA.created, customer.appB.created and `customer.appC.created.

You can create separate queue to collect specific messages by binding queue with exact routing key - customer.appA.created, customer.appB.created and so on, or as you already noted, bind queue with wildcard routing key customer.*.created.

To collect messages only for appA and appB you can create queue with two bindings customer.appA.created and customer.appB.created, so you'll get two messages type in one queue.

pinepain
  • 12,453
  • 3
  • 60
  • 65