Junior — Middle
What is the difference between Topic and Direct exchange types in RabbitMQ?
sobes.tech AI
Answer from AI
In RabbitMQ, exchange types Direct and Topic differ in how they route messages:
-
Direct Exchange: routes messages to a queue if the routing key of the message exactly matches the binding key of the queue.
-
Topic Exchange: supports patterns with wildcards (
*for one word,#for multiple words), allowing flexible message routing based on key patterns.
Example:
- Direct: a message with routing key "error" will only go to the queue bound with the key "error".
- Topic: a message with routing key "logs.error.system" can go to a queue with binding key "logs.*.system" or "logs.#".
Thus, Topic is more flexible and suitable for complex routing, while Direct is for exact key matches.