0
0
RabbitMQdevops~5 mins

Topic exchange (pattern matching) in RabbitMQ - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want to send messages to queues based on flexible patterns in routing keys, topic exchanges help by matching these patterns. This lets you deliver messages to multiple queues depending on keywords separated by dots.
When you want to route logs by severity and source, like 'error.system' or 'info.application'.
When you need to send notifications to different services based on categories, such as 'user.signup' or 'order.created'.
When you want to subscribe to multiple related topics using wildcards, like all messages starting with 'user.'.
When you want to separate messages by region and type, for example 'us.east.sales' or 'eu.west.support'.
Config File - topic_exchange_setup.sh
topic_exchange_setup.sh
#!/bin/bash

# Declare a topic exchange named 'topic_logs'
rabbitmqadmin declare exchange name=topic_logs type=topic

# Declare queues
rabbitmqadmin declare queue name=queue_errors
rabbitmqadmin declare queue name=queue_all_logs

# Bind queues with routing keys
rabbitmqadmin declare binding source=topic_logs destination=queue_errors routing_key="error.*"
rabbitmqadmin declare binding source=topic_logs destination=queue_all_logs routing_key="#"

This script creates a topic exchange called topic_logs. It then creates two queues: queue_errors for error messages and queue_all_logs for all messages.

The bindings use routing keys with patterns: error.* matches any single word after 'error', and # matches all routing keys.

Commands
This command creates a topic exchange named 'topic_logs' where messages will be routed based on pattern matching.
Terminal
rabbitmqadmin declare exchange name=topic_logs type=topic
Expected OutputExpected
{"name":"topic_logs","type":"topic","durable":false,"auto_delete":false,"internal":false,"arguments":{}}
type=topic - Sets the exchange type to topic for pattern matching routing keys
This command creates a queue named 'queue_errors' to receive messages matching error patterns.
Terminal
rabbitmqadmin declare queue name=queue_errors
Expected OutputExpected
{"name":"queue_errors","durable":false,"auto_delete":false,"arguments":{}}
This command creates a queue named 'queue_all_logs' to receive all messages regardless of routing key.
Terminal
rabbitmqadmin declare queue name=queue_all_logs
Expected OutputExpected
{"name":"queue_all_logs","durable":false,"auto_delete":false,"arguments":{}}
This binds the 'queue_errors' queue to the 'topic_logs' exchange with a routing key pattern that matches 'error' followed by any single word.
Terminal
rabbitmqadmin declare binding source=topic_logs destination=queue_errors routing_key="error.*"
Expected OutputExpected
{"source":"topic_logs","destination":"queue_errors","routing_key":"error.*"}
routing_key=error.* - Matches routing keys starting with 'error' followed by any single word
This binds the 'queue_all_logs' queue to the 'topic_logs' exchange with a routing key pattern that matches all messages.
Terminal
rabbitmqadmin declare binding source=topic_logs destination=queue_all_logs routing_key="#"
Expected OutputExpected
{"source":"topic_logs","destination":"queue_all_logs","routing_key":"#"}
routing_key=# - Matches all routing keys
Key Concept

If you remember nothing else from this pattern, remember: topic exchanges route messages to queues based on flexible word patterns in routing keys using * and # wildcards.

Common Mistakes
Using '#' wildcard in the middle of a routing key pattern
The '#' wildcard must be at the end or alone; placing it in the middle is invalid and will not match as expected.
Use '#' only at the end or as the entire routing key pattern to match zero or more words.
Binding queues with routing keys that do not match the message routing keys
Messages will not be delivered to queues if the routing key patterns do not match the message's routing key.
Ensure routing key patterns in bindings correctly match the expected message routing keys.
Confusing '*' and '#' wildcards
'*' matches exactly one word, '#' matches zero or more words; using them interchangeably causes unexpected routing.
Use '*' to match a single word and '#' to match multiple or no words in routing keys.
Summary
Create a topic exchange to route messages based on routing key patterns.
Declare queues to receive messages matching specific patterns.
Bind queues to the exchange using routing keys with '*' and '#' wildcards for flexible matching.