* vs # in RabbitMQ Topic Exchange: Key Differences and Usage
* matches exactly one word in a routing key, while # matches zero or more words. Use * for precise single-word matching and # for flexible multi-word matching in routing keys.Quick Comparison
This table summarizes the main differences between * and # wildcards in RabbitMQ topic exchanges.
| Feature | * Wildcard | # Wildcard |
|---|---|---|
| Match Type | Exactly one word | Zero or more words |
| Position in Routing Key | Matches one word at a specific position | Matches multiple words or none at any position |
| Example Match | stock.*.ny matches stock.us.ny | stock.# matches stock.us.ny and stock |
| Use Case | Precise routing with fixed word count | Flexible routing with variable word count |
| Wildcard Symbol | * | # |
Key Differences
The * wildcard in RabbitMQ topic exchanges matches exactly one word in the routing key. Words are separated by dots (.), so * replaces a single word at a specific position. For example, a binding key log.*.error matches log.app.error but not log.app.db.error because the latter has two words between log and error.
In contrast, the # wildcard matches zero or more words. It can replace any number of words, including none. For example, log.# matches log, log.app, and log.app.db.error. This makes # more flexible for routing messages with variable word counts in routing keys.
In summary, use * when you want to match exactly one word at a certain position, and use # when you want to match any number of words, including none, in that part of the routing key.
Code Comparison
Here is an example of using the * wildcard in a topic exchange binding key to route messages with exactly one word in the middle position.
import pika connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel() channel.exchange_declare(exchange='topic_logs', exchange_type='topic') # Binding key with * wildcard binding_key = 'stock.*.ny' channel.queue_declare(queue='queue_star') channel.queue_bind(exchange='topic_logs', queue='queue_star', routing_key=binding_key) # Publish messages channel.basic_publish(exchange='topic_logs', routing_key='stock.us.ny', body='Message for stock.us.ny') channel.basic_publish(exchange='topic_logs', routing_key='stock.eu.ny', body='Message for stock.eu.ny') channel.basic_publish(exchange='topic_logs', routing_key='stock.us.ca', body='Message for stock.us.ca') print('Messages sent with * wildcard binding') connection.close()
# Equivalent
This example uses the # wildcard in a topic exchange binding key to route messages with zero or more words after stock.
import pika connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel() channel.exchange_declare(exchange='topic_logs', exchange_type='topic') # Binding key with # wildcard binding_key = 'stock.#' channel.queue_declare(queue='queue_hash') channel.queue_bind(exchange='topic_logs', queue='queue_hash', routing_key=binding_key) # Publish messages channel.basic_publish(exchange='topic_logs', routing_key='stock', body='Message for stock') channel.basic_publish(exchange='topic_logs', routing_key='stock.us.ny', body='Message for stock.us.ny') channel.basic_publish(exchange='topic_logs', routing_key='stock.eu.ca.ny', body='Message for stock.eu.ca.ny') print('Messages sent with # wildcard binding') connection.close()
When to Use Which
Choose * when you need to match routing keys with a fixed number of words and want precise control over which word is matched. For example, if you want to route messages only when the second word is specific, use *.
Choose # when you want to match routing keys with variable length or when you want to catch all messages under a certain prefix regardless of how many words follow. This is useful for broad routing rules.
In short, use * for exact single-word matching and # for flexible multi-word matching.
Key Takeaways
* matches exactly one word in a routing key segment.# matches zero or more words, allowing flexible routing.* for precise, fixed-length routing keys.# for broad, variable-length routing keys.