CodePark/ questions / RabbitMQ: exchanges, queues & routing
START PRACTICINGPRACTICE
Backend question · Message queues & streaming › RabbitMQ: exchanges, queues & routing

In RabbitMQ, where do producers publish messages, and what are the exchange types?

Producers publish to an exchange, never directly to a queue. The exchange routes each message to zero or more queues. It uses bindings (rules that link an exchange to a queue) and the message's routing key or headers. Consumers read from queues.

The exchange types:

  • Direct: delivers to queues whose binding key equals the routing key exactly.
  • Fanout: ignores the routing key and copies the message to every bound queue.
  • Topic: matches dot-separated routing keys against patterns. * matches exactly one word. # matches zero or more words.
  • Headers: routes on message header values, with x-match set to all or any.

The default exchange is a direct exchange with no name. Every queue is bound to it by the queue's own name. So publishing to it with routing key emails reaches the emails queue. This is why it looks like you can send straight to a queue.

  • Producers publish to exchanges, and bindings route messages to queues.
  • A direct exchange needs an exact routing key match. A fanout exchange sends to all bound queues.
  • In a topic exchange, * matches one word and # matches zero or more words.
  • A headers exchange routes on header values, with x-match set to all or any.
  • The default exchange routes by queue name.