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.
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
An exchange is responsible for routing the messages to the different queues with the help of attributes, bindings, and routing keys.
A binding is a "link" that you set up to bind a queue to an exchange.
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.
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 firstname.lastname@example.org if you have any suggestions, questions or feedback.