What is Topic Exchange in RabbitMQ: Explanation and Example
topic exchange in RabbitMQ routes messages to queues based on matching patterns in routing keys. It allows flexible and powerful routing using wildcards like * and # to match parts of the routing key.How It Works
Imagine a post office where letters are sorted by detailed addresses. A topic exchange acts like this post office, routing messages to different queues based on patterns in the message's routing key. The routing key is a string with words separated by dots, like "weather.us.ny".
The exchange uses wildcards to match these keys: * matches exactly one word, and # matches zero or more words. For example, a queue bound with the pattern "weather.*.ny" will get messages with routing keys like "weather.us.ny" but not "weather.us.ca.ny".
This lets you send messages to many queues flexibly, depending on detailed topics or categories, without hardcoding each route.
Example
This example shows how to declare a topic exchange, bind queues with patterns, and publish messages that get routed based on matching routing keys.
import pika connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel() # Declare a topic exchange channel.exchange_declare(exchange='topic_logs', exchange_type='topic') # Declare queues channel.queue_declare(queue='queue1') channel.queue_declare(queue='queue2') # Bind queues with routing patterns channel.queue_bind(exchange='topic_logs', queue='queue1', routing_key='kern.*') channel.queue_bind(exchange='topic_logs', queue='queue2', routing_key='*.critical') # Publish messages channel.basic_publish(exchange='topic_logs', routing_key='kern.critical', body='Kernel critical error') channel.basic_publish(exchange='topic_logs', routing_key='auth.info', body='Auth info message') print("Messages sent") connection.close()
When to Use
Use a topic exchange when you need to route messages based on multiple criteria or categories that can be expressed as words separated by dots. It is ideal for complex routing scenarios like logging systems, event notifications, or any system where messages belong to hierarchical topics.
For example, a monitoring system might send alerts with routing keys like "server1.cpu.high" or "server2.disk.low". Different teams can bind queues to receive only the alerts relevant to them using patterns like "server1.*.*" or "*.disk.#".
Key Points
- A
topic exchangeroutes messages using routing keys with dot-separated words. - Wildcards
*and#allow flexible matching of routing keys. - It supports complex and hierarchical routing scenarios.
- Queues bind to the exchange with patterns to receive matching messages.