The RabbitMQ topic mode is similar to the routing mode, but the difference is that the topic mode's routing matching supports wildcard fuzzy matching, while the routing mode only supports exact matching.
Architecture diagram
Explanation:
- P represents the producer, X represents the exchange, and the red Q1, Q2 represent the queues, C1, C2 represent the consumers.
- The exchange type is topic.
- Topic exchange message forwarding logic: It matches the Routing key in the message with the Routing keys associated with all Bindings of the Exchange through fuzzy matching, and if there is a match, it sends the message to the bound Queue.
Tip: The difference between topic mode and direct mode is whether matching of the Routing key supports wildcard fuzzy matching, everything else is the same.
The wildcards supported by the topic are as follows:
-
#
(hash) matches one or more words -
*
(asterisk) matches only one word
For example:
The routing key bound to queue Q1 = *.orange.*
The routing key bound to queue Q2 = *.*.rabbit
and lazy.#
If the message's routing key is "quick.orange.rabbit", it matches both queues Q1 and Q2.
Tip: If the routing key does not match any queue, the message will be discarded. If the routing key is empty, it functions the same as the fanout mode and forwards the message directly to all queues.
Use case
Like the routing mode, the difference lies in the flexibility of the subscription conditions, specifically the more flexible matching rules for the Routing key.