A topic exchange routes a message to every queue whose binding pattern matches the message's routing key. Patterns use two wildcards, * and #, and that's all there is to it. The details still trip people up, so this guide goes through the rules, a set of examples you can check yourself against, and the mistakes we see most often.
order.eu.created.* matches exactly one word, and # matches zero or more words.binding "order.*.created" matches order.eu.created
no match order.created, order.eu.fr.created
binding "order.#" matches order, order.created, order.eu.fr.created
binding "#.created" matches created, order.created, user.eu.created
binding "#" matches everythingWhen you publish to a topic exchange, you give a routing key. The exchange compares it to the binding key of every binding attached to it. Each binding whose pattern matches makes the message go to that binding's destination, which can be a queue or another exchange.
The comparison is word by word:
.. A word can contain any characters except ., including -, _ and digits.Order.created doesn't match order.*.* matches one word, never zero and never two.# matches any number of words, including none.If no binding matches, the message is dropped silently, unless you publish with the mandatory flag (the message is returned to the publisher) or the exchange has an alternate exchange.
Bindings on an exchange named events:
| Binding key | Queue |
|---|---|
order.*.created | new-orders-by-region |
order.# | order-audit |
*.eu.* | eu-events |
#.failed | failures |
What happens to each message:
| Routing key | Delivered to |
|---|---|
order.eu.created | new-orders-by-region, order-audit, eu-events |
order.created | order-audit |
order.eu.payment.failed | order-audit, failures |
user.eu.signup | eu-events |
payment.failed | failures |
order | order-audit (# matches zero words) |
Order.eu.created | nothing (case-sensitive) |
invoice.us.sent | nothing |
Note the order case. People expect order.# to require at least one word after order, but # also matches zero words. If you need at least one word after order, use order.*.#: it matches order.x and anything longer, but not order alone.
A topic exchange can act like the other two exchange types:
order.created) works exactly like a direct exchange binding.# matches everything, like a fanout exchange.So why not use topic everywhere? Mostly clarity. A fanout exchange says "everyone gets everything" in its type. A direct exchange makes it obvious that routing keys are exact. Performance differences are small for typical numbers of bindings, but direct and fanout matching are simpler. Our exchange types guide covers headers exchanges too.
| Direct | Topic | Fanout | |
|---|---|---|---|
| Matching | Exact routing key | Pattern with * and # | Ignores the routing key |
| Good for | Commands, task types | Events with a hierarchy | Broadcast to every subscriber |
| Example key | resize-image | order.eu.created | (none) |
The routing key format is a contract between publishers and consumers, so design it before you have 40 services depending on it.
<domain>.<entity>.<event> or <domain>.<region>.<event>. Consumers almost always filter by the first words.* patterns only work if every message has the same depth. order.eu.created and order.created side by side make patterns fragile.order.83721.created). It doesn't help routing and makes keys unbounded. Put IDs in the message body or headers.payment-failed or payment_failed), since matching is case-sensitive.We use this pattern to build an event bus with RabbitMQ.
Node.js (amqplib)
await channel.assertExchange('events', 'topic', { durable: true });
await channel.assertQueue('order-audit', { durable: true, arguments: { 'x-queue-type': 'quorum' } });
await channel.bindQueue('order-audit', 'events', 'order.#');
channel.publish('events', 'order.eu.created', Buffer.from(JSON.stringify({ id: 42 })), {
persistent: true,
contentType: 'application/json',
});Python (pika)
channel.exchange_declare(exchange="events", exchange_type="topic", durable=True)
channel.queue_declare(queue="failures", durable=True, arguments={"x-queue-type": "quorum"})
channel.queue_bind(queue="failures", exchange="events", routing_key="#.failed")
channel.basic_publish(exchange="events", routing_key="payment.failed", body=b'{"id": 42}')Go (amqp091-go)
ch.ExchangeDeclare("events", "topic", true, false, false, false, nil)
ch.QueueDeclare("eu-events", true, false, false, false, amqp.Table{"x-queue-type": "quorum"})
ch.QueueBind("eu-events", "*.eu.*", "events", false, nil)
ch.PublishWithContext(ctx, "events", "user.eu.signup", false, false, amqp.Publishing{
ContentType: "application/json",
Body: []byte(`{"id": 42}`),
})order.%, order.+ or order.created|order.updated are not patterns: they are literal words and only match a routing key that literally contains them. To match two events, create two bindings.
* to match a partial wordorder.creat* doesn't match order.created. Wildcards only replace whole words. order.creat* is a literal word creat*.
A message matching three bindings to the same queue is delivered once to that queue. A message matching bindings to three queues is copied to all three. If a queue receives messages it shouldn't, look for a broad binding like # or order.# that someone added for debugging and never removed.
A typo in a routing key (oder.eu.created) means the message is dropped without an error. Publish with mandatory: true and handle returned messages, or attach an alternate exchange to the topic exchange that routes unmatched messages to a catch-all queue.
The fastest way to check a pattern is to look at the bindings of the exchange and run the key through them in your head, which works for five bindings and fails for fifty. On a real broker, the questions are usually "which queues will get order.eu.created?" and "why did this queue get that message?".
To see what actually flows through a queue, you can also spy on real-time queue traffic without consuming messages.
* and # in RabbitMQ?* matches exactly one word. # matches zero or more words. Words are separated by dots.
# match an empty routing key?Yes. # matches everything, including an empty routing key.
No. In RabbitMQ, "topic" is a type of exchange, a routing rule. Messages are stored in queues, not topics. The closest thing to a Kafka topic in RabbitMQ is a stream. We compare the two models in RabbitMQ vs Kafka.
No. One binding has one pattern. Add one binding per pattern, to the same queue if needed.
Slightly, because patterns are more expensive to match than exact keys. For typical setups with a few hundred bindings the difference is negligible compared to disk and network costs.
Explore exchanges, queues, bindings, dead-letter and alternate exchanges in an interactive routing graph.
Visualize my routingAvailable on Windows, Mac, and Linux.
RabbitMQ tutorialRabbitMQ 3.x end of life: what it means and how to upgrade to 4.xRabbitMQ 3.13 lost community support in 2024. Here are the support dates for every series, the risks of staying, the 4.0 breaking changes and a step-by-step upgrade path to 4.x.
RabbitMQ tutorialMessages stuck in a RabbitMQ queue? 7 causes and how to fix themMessages piling up in RabbitMQ and not consumed? Diagnose missing consumers, stuck unacked messages, prefetch, slow consumers, poison messages, alarms and routing mistakes, with the commands to check each one.
RabbitMQ tutorialRabbitMQ queue types: classic vs quorum vs streamWhich RabbitMQ queue type should you use? Classic, quorum and stream queues compared on replication, data safety, ordering, features and performance, for RabbitMQ 4.x.
RabbitMQ tutorialRabbitMQ health checks: CLI, HTTP API, Docker and KubernetesCopy-paste RabbitMQ health checks. rabbitmq-diagnostics commands, /api/health endpoints, a Docker Compose healthcheck and Kubernetes probes that don't hurt your cluster.
RabbitMQ tutorialThe RabbitMQ Management UI: a complete guideHow to enable the RabbitMQ management plugin, open the UI on port 15672 (Docker too), log in, and what each tab does, plus its limits.
RabbitMQ tutorialRabbitMQ retry queue vs dead letter queue: what's the difference?A retry queue holds a failed RabbitMQ message before another attempt, a dead-letter queue holds it once it should no longer be processed. Learn when to use each, how to combine them, and how they differ from requeue.