What is a topic exchange and how does routing key pattern matching work?
Understand how a RabbitMQ topic exchange routes messages using '*' and '#' wildcard patterns against routing keys for flexible, selective message delivery.
Expected Interview Answer
A topic exchange routes messages to queues based on wildcard pattern matching between the message's routing key and the binding patterns, using '*' to match exactly one word and '#' to match zero or more words.
Routing keys are dot-delimited words such as 'order.eu.created'. Queues bind with patterns like 'order.*.created' or 'order.#'. RabbitMQ delivers a message to every queue whose binding pattern matches the routing key, enabling flexible, selective, multi-dimensional routing that sits between the rigid direct exchange and the everything-goes fanout exchange.
- Flexible, selective routing by pattern
- Supports multi-dimensional topic hierarchies
- '*' matches one word, '#' matches zero or more
- Consumers subscribe only to events they care about
- Can emulate both direct and fanout behavior
AI Mentor Explanation
A topic exchange is like subscribing to specific match commentary feeds using patterns: ask for 'india.*.wicket' and you hear every wicket in any India format, while 'test.#' streams everything from Test cricket. The routing key on each ball, like 'india.test.wicket', is matched against your pattern to decide if that update reaches you.
Step-by-Step Explanation
Step 1
Design the routing key scheme
Choose dot-delimited words, e.g. 'order.region.status', ordered from general to specific.
Step 2
Declare a topic exchange
Create an exchange of type 'topic' on the channel.
Step 3
Bind queues with patterns
Use '*' for exactly one word and '#' for zero or more words, e.g. 'order.*.created' or 'order.#'.
Step 4
Publish with a concrete key
Producer publishes with a specific routing key like 'order.eu.created'.
Step 5
Broker matches and routes
RabbitMQ delivers to every queue whose pattern matches the routing key.
What Interviewer Expects
- Correct meaning of '*' (one word) and '#' (zero or more)
- Understanding of dot-delimited routing keys
- How topic sits between direct and fanout
- A message can match multiple bindings
- Practical routing-key design examples
Common Mistakes
- Swapping the meanings of '*' and '#'
- Thinking '*' matches multiple words
- Assuming a message reaches only one queue
- Overcomplicating routing keys with too many segments
Best Answer (HR Friendly)
“A topic exchange in RabbitMQ routes messages by matching a labeled routing key against subscription patterns with wildcards. It lets each service subscribe to exactly the categories of events it cares about, giving flexible and selective delivery.”
Code Example
const amqp = require('amqplib');
async function run() {
const conn = await amqp.connect('amqp://localhost');
const ch = await conn.createChannel();
const exchange = 'orders';
await ch.assertExchange(exchange, 'topic', { durable: false });
const q = await ch.assertQueue('', { exclusive: true });
// Matches order.eu.created, order.us.created, etc.
await ch.bindQueue(q.queue, exchange, 'order.*.created');
ch.publish(exchange, 'order.eu.created', Buffer.from('new EU order'));
console.log('Routed by pattern order.*.created');
await ch.close();
await conn.close();
}
run();Follow-up Questions
- What is the difference between '*' and '#' in topic bindings?
- How can a topic exchange behave like a fanout exchange?
- How can a topic exchange behave like a direct exchange?
- What happens if a routing key matches multiple bindings?
- How would you design routing keys for a logging system?
MCQ Practice
1. In a topic exchange binding, what does '*' match?
'*' substitutes for exactly one word in the dot-delimited routing key, while '#' matches zero or more words.
2. Which binding pattern receives the routing key 'log.error.db'?
'log.#' matches 'log' followed by zero or more words, so 'log.error.db' matches; 'log.*' would need exactly one word after 'log'.
Flash Cards
What does '*' match in a topic binding? — Exactly one word in the dot-delimited routing key.
What does '#' match in a topic binding? — Zero or more words, enabling broad subtree matching.
Where does topic sit among exchange types? — Between direct (exact key) and fanout (all queues) — flexible pattern routing.