Bird
Raised Fist0
RabbitMQdevops~5 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a topic exchange in RabbitMQ?
easy
A. To broadcast messages to all queues regardless of routing keys
B. To store messages until consumers are ready
C. To route messages based on pattern matching of routing keys
D. To encrypt messages before sending

Solution

  1. Step 1: Understand the role of exchanges in RabbitMQ

    Exchanges route messages to queues based on rules. Topic exchanges use patterns in routing keys.
  2. Step 2: Identify the unique feature of topic exchanges

    Topic exchanges route messages by matching routing keys with patterns containing * and # wildcards.
  3. Final Answer:

    To route messages based on pattern matching of routing keys -> Option C
  4. Quick Check:

    Topic exchange = pattern matching routing [OK]
Hint: Topic exchange uses wildcards to match routing keys [OK]
Common Mistakes:
  • Confusing topic exchange with fanout exchange
  • Thinking topic exchange stores messages
  • Assuming topic exchange encrypts messages
2. Which of the following is the correct syntax to bind a queue named logs to a topic exchange named topic_logs with routing key pattern kern.* using RabbitMQ CLI?
easy
A. rabbitmqadmin declare binding source=topic_logs destination=logs routing_key=kern.*
B. rabbitmqctl bind queue=logs exchange=topic_logs routing_key=kern.*
C. rabbitmqadmin bind queue=logs exchange=topic_logs routing_key=kern.*
D. rabbitmqctl declare binding source=topic_logs destination=logs routing_key=kern.*

Solution

  1. Step 1: Identify the correct RabbitMQ CLI tool for binding

    Binding queues to exchanges is done with rabbitmqadmin declare binding, not rabbitmqctl.
  2. Step 2: Check the correct parameters for binding

    The correct parameters are source for exchange, destination for queue, and routing_key for pattern.
  3. Final Answer:

    rabbitmqadmin declare binding source=topic_logs destination=logs routing_key=kern.* -> Option A
  4. Quick Check:

    Use rabbitmqadmin declare binding with source, destination, routing_key [OK]
Hint: Use rabbitmqadmin declare binding with source and destination [OK]
Common Mistakes:
  • Using rabbitmqctl instead of rabbitmqadmin for binding
  • Swapping source and destination parameters
  • Omitting routing_key parameter
3. Given a topic exchange with a queue bound using the routing key pattern *.orange.*, which routing keys will deliver messages to this queue?
medium
A. quick.orange.rabbit, orange
B. orange.rabbit, quick.orange
C. quick.orange, orange.lazy.rabbit
D. quick.orange.rabbit, lazy.orange.elephant

Solution

  1. Step 1: Understand the pattern *.orange.*

    The pattern means exactly three words separated by dots, with 'orange' as the middle word. The * matches exactly one word.
  2. Step 2: Check each routing key against the pattern

    quick.orange.rabbit, lazy.orange.elephant keys: 'quick.orange.rabbit' and 'lazy.orange.elephant' both have three words with 'orange' in the middle, so they match.
  3. Final Answer:

    quick.orange.rabbit, lazy.orange.elephant -> Option D
  4. Quick Check:

    Pattern *.orange.* matches three-word keys with orange in middle [OK]
Hint: Count words; * matches one word, # matches zero or more [OK]
Common Mistakes:
  • Assuming * matches zero or multiple words
  • Ignoring word count in routing key
  • Confusing * with # wildcard
4. You have a queue bound to a topic exchange with the routing key pattern stock.#. Messages with routing key stock.us.nyse are not delivered to the queue. What is the most likely cause?
medium
A. The exchange type is not set to topic
B. The routing key pattern stock.# does not match stock.us.nyse
C. The queue is not bound correctly to the exchange
D. The message was published with an empty routing key

Solution

  1. Step 1: Verify the exchange type

    The pattern stock.# matches routing keys starting with 'stock' followed by zero or more words. If messages are not delivered, the exchange might not be a topic exchange.
  2. Step 2: Check other possible causes

    Binding and routing key pattern are correct. If the exchange is not topic type, pattern matching won't work.
  3. Final Answer:

    The exchange type is not set to topic -> Option A
  4. Quick Check:

    Topic exchange required for pattern matching [OK]
Hint: Ensure exchange type is topic for pattern matching [OK]
Common Mistakes:
  • Assuming binding is always correct without verification
  • Thinking # wildcard doesn't match multiple words
  • Ignoring exchange type setting
5. You want to receive all messages related to any fruit except those about apples or bananas using a topic exchange. Which binding key pattern should you use?
hard
A. *.fruit.*
B. #.fruit.# and bind separate queues for apple and banana to discard
C. #.fruit.# but exclude apple and banana keys manually
D. #.fruit.#

Solution

  1. Step 1: Understand topic exchange limitations

    Topic exchanges route messages by matching patterns but cannot exclude specific keys directly in binding patterns.
  2. Step 2: Plan a solution using multiple queues

    Bind one queue with #.fruit.# to get all fruit messages. Bind separate queues for apple and banana keys to handle or discard them separately.
  3. Step 3: Implement exclusion by filtering in consumers or separate queues

    Since exclusion is not supported in binding keys, use separate queues or consumer logic to exclude apples and bananas.
  4. Final Answer:

    #.fruit.# and bind separate queues for apple and banana to discard -> Option B
  5. Quick Check:

    Exclusion requires separate queues or consumer filtering [OK]
Hint: Topic exchange can't exclude keys; use separate queues [OK]
Common Mistakes:
  • Trying to exclude keys using binding patterns
  • Assuming * or # can exclude specific words
  • Not using multiple queues or consumer filtering
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard