0
0
RabbitmqHow-ToBeginner ยท 4 min read

How to Use Wildcard in Topic Exchange in RabbitMQ

In RabbitMQ topic exchanges, use * to match exactly one word and # to match zero or more words in routing keys. These wildcards let you route messages flexibly by binding queues with patterns like logs.* or logs.#.
๐Ÿ“

Syntax

In a RabbitMQ topic exchange, routing keys are dot-separated words. Wildcards help match these keys:

  • * matches exactly one word.
  • # matches zero or more words.

Example binding key: sensor.*.temperature matches sensor.room1.temperature but not sensor.room1.floor1.temperature.

plaintext
binding_key = "logs.*"
# Matches routing keys like "logs.info", "logs.error" but not "logs.error.critical"

binding_key = "logs.#"
# Matches "logs", "logs.info", "logs.error.critical", etc.
๐Ÿ’ป

Example

This example shows how to declare a topic exchange, bind queues with wildcard patterns, and publish messages that get routed accordingly.

python
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='queue_info')
channel.queue_declare(queue='queue_all_logs')

# Bind queues with wildcard routing keys
channel.queue_bind(exchange='topic_logs', queue='queue_info', routing_key='logs.info')
channel.queue_bind(exchange='topic_logs', queue='queue_all_logs', routing_key='logs.#')

# Publish messages
channel.basic_publish(exchange='topic_logs', routing_key='logs.info', body='Info log message')
channel.basic_publish(exchange='topic_logs', routing_key='logs.error.critical', body='Critical error log')

print("Messages sent")
connection.close()
Output
Messages sent
โš ๏ธ

Common Pitfalls

  • Using * to match multiple words instead of exactly one word causes no matches.
  • Using # in the middle of a binding key is allowed but can lead to unexpected matches.
  • Forgetting that routing keys are dot-separated words, so wildcards only match whole words, not partial words.

Example of wrong and right usage:

plaintext
# Wrong: trying to match multiple words with '*'
binding_key_wrong = "logs.*"
# This won't match 'logs.error.critical'

# Right: use '#'
binding_key_right = "logs.#"
# This matches 'logs.error.critical' and 'logs.info'
๐Ÿ“Š

Quick Reference

WildcardMeaningExample MatchExample No Match
*Matches exactly one wordlogs.infologs.error.critical
#Matches zero or more wordslogs.error.critical
logs.*.errorMatches logs.anyword.errorlogs.app.errorlogs.app.db.error
logs.#Matches logs and any subkeyslogsother.logs
โœ…

Key Takeaways

Use '*' to match exactly one word in a routing key segment.
Use '#' to match zero or more words in routing keys.
Routing keys are dot-separated words; wildcards match whole words only.
Bind queues with wildcard patterns to flexibly route messages.
Avoid placing '#' in unexpected positions to prevent unintended matches.