Search code examples
rabbitmqactivemq-classicstomp

When should I use a single queue and when multiple ones?


I wonder, are there rules of when to create a single queue in RabbitMQ and when multiple ones? Or is it up to a producer whether or not send the messages to a single queue or multiple ones and technically there's no difference? Should it depend on the schema of the db, that is, should a single table correspond to a single queue?


Solution

  • single queue in RabbitMQ and when multiple ones?

    a single queue is a really bad idea. eventually you will want different types of messages to flow through rabbitmq. if you only have a single queue, you will end up reproducing the logic of how to decide which code should process the message, when RabbitMQ can do this for you with routing.

    in general, each type of "work" to be done, represented by a message, should be it's own queue.

    there will also be times when a specific message consumer will need it's own queue, even if the same types of messages are flowing to other queues.

    Or is it up to a producer whether or not send the messages to a single queue or multiple ones

    the producer does not decide which queues the message it delivered to. the producer only knows which exchange to publish through. message consumers should be the ones deciding where the messages are delivered. the message consumer should be responsible for setting up the routing between the exchange and the destination queue.

    there are exceptions to this, like every "good idea" in software development, but in general i find this to be true

    Should it depend on the schema of the db

    your RabbitMQ configuration has zero correlation to your database. don't bother trying to map between the two. it will be painful, at best.

    rather, look at behavior.

    what type of behavior does this message need to trigger? what specific behavior is it, and which exchange / routing key should it be published with?

    these are better questions that will lead you to a better RabbitMQ layout, with a focus on exchanges for groups of related messages, queues dedicated to a specific type of message consumer or a specific consumer, and routing keys that provide the logic to move a message from an exchange to the right queue.

    ...

    i'd recommend reading some books on RabbitMQ, to get yourself rolling in the right direction with the basics of how RabbitMQ should be used, and then getting into good usage patterns and topology design:

    • RabbitMQ In Action - from ground zero, what it is, why you should use it, how it works, etc. recommended for anyone looking at RMQ
    • RabbitMQ Layout - understanding how RabbitMQ fits into various scenarios, as told through stories of developers solving problems - this is the "why-to" of RMQ topology design (exchanges, queues and bindings) <disclosure: the author of the answer is also the author of the recommended book>
    • RabbitMQ Patterns - understand message types, routing and common usage patterns in messaging systems, as applied to RabbitMQ - this is the core pattern "how-to" for application and topology design with RMQ <disclosure: the author of the answer is also the author of the recommended book>