RabbitMQ Topic Exchange Explained

Need powerful routing capabilities and reliable message deliveries? Topic exchange is a built-in exchange in RabbitMQ, enabling the use of wildcards in the binding key.

In RabbitMQ, messages are published to an exchange and, depending on the type of exchange, the message gets routed to one or more queues. RabbitMQ has some built-in exchanges and some that are enabled via a plugin. Topic exchange is one of the exchanges that are built into RabbitMQ and is similar to the direct exchange but with more powerful routing capabilities.

What is RabbitMQ Topic Exchange?

It is similar to the direct exchange in that it will route messages where the routing key matches the binding key from the queue binding. However, with a topic exchange, you can also use wildcards in the binding key. When using a topic exchange the routing key of the message must be a list of words separated by dots, like “metrics.server.cpu”. The reason is that topic exchange allows you to match on parts of the routing key and uses dots as separators.

There are two types of wildcard you can use:

  • # (hash) will match zero or more words, for example, “metrics.#” will match all routing keys that start with “metrics.”
  • * (star) will match one word, for example, “metrics.*.cpu” will match all routing keys that start with “metrics.” and end in “.cpu”.

As you can see this exchange is very powerful and allows for a lot of use cases.

Things to think about: If you publish a message that doesn’t match any binding key, the message will be lost.

When to not use the topic exchange

  • You can use the binding key “#” to catch all messages published to a topic exchange, but that is not recommended and you should probably look at fanout exchange instead.
  • If none of the binding keys uses any wildcards, the exchange behaves just like the direct exchange so that might be a better choice.

RabbitMQ Topic Exchange Fact Box

Exchange

An exchange is responsible for routing the messages to the different queues with the help of attributes, bindings, and routing keys.

Binding

A binding is a "link" that you set up to bind a queue to an exchange.

Binding key

The binding key is the key you bind the queue to the exchange with, which is compared in the exchange to the routing key of the message.

Routing key

The routing key is a message attribute. The exchange might look at this key when deciding how to route the message to queues (depending on exchange type). The routing key is like an address for the message.

Try out RabbitMQ at CloudAMQP

Did you like learning about the Topic Exchange? Great! Here follows a guide around the Fanout Exchange.

Don’t forget that you can create a free RabbitMQ instance at CloudAMQP and begin experimenting with exchanges by taking a few minutes to sign up here: https://customer.cloudamqp.com/signup

Please email us at contact@cloudamqp.com if you have any suggestions, questions or feedback.

CloudAMQP - industry leading RabbitMQ as a service

Start your managed cluster today. CloudAMQP is 100% free to try.

13,000+ users including these smart companies