RabbitMQ Direct Exchange Explained

In RabbitMQ, messages are published to an exchange and, depending on the type of exchange, the message gets routed to one or more queues. This blog post will be focusing on the Direct Exchange in RabbitMQ.

Direct exchange is one of the exchanges that are built into RabbitMQ and is probably the simplest of them all. Use a direct exchange when the routing keys are known and you want to perform different tasks based on the routing key.

What is RabbitMQ Direct Exchange?

The direct exchange will route the message to a queue whose binding key matches the routing key of the message exactly. So if you bind a queue to a direct exchange with the binding key “blue”, all messages published to that exchange with the routing key “blue” will end up in that queue. One queue can have multiple bindings to the same exchange with different binding keys.

Two different queues can also bind to the exchange with the same binding key, in that case when a message is published with that routing key the exchange will behave like the

When to use RabbitMQ Direct Exchange

For example, you own a delivery company that delivers using a bicycle, motorcycle, or car. To trigger a delivery you post a message to one of the queues with an address. If you want a delivery using a bicycle you post a message to the direct exchange using the routing key “bicycle” and that message will get routed to the bicycle delivery queue. To generate an event log of all deliveries happening in the system, you add a log queue, and bind that to the same exchange but with three bindings of car, motorcycle, and bicycle. That way, you get all messages to that queue and can save that to the log.

RabbitMQ Direct 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 Direct Exchange? Great! Here follows a guide around the Topic 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