There are four stations in conveying messages:
- Producer: creates messages
- Exchange: forwards messages
- Queue: stores messages
- Consumer: processes the message
The producer releases a message, but doesn’t send this directly to the consumer, instead handing it over to the exchange. This position is responsible for distributing messages to different queues, from which the consumers can help themselves to the messages. Both the exchange and the queues are part of RabbitMQ, and are administered by the software. Routing keys are used to make sure that messages reach the correct recipient. The sender provides the message with a routing key that works like an address. Based on the key, the exchange recognizes how the message is to be routed.
A binding exists as a link between the exchange and the queue. Through this, each individual queue is connected to the exchange. Binding also defines the criteria for forwarding a message. There are four main ways of distributing messages.
Direct exchange
Direct exchange is a direct connection between sender and recipient. The producer equips the message with a routing key that corresponds to a routing key in the queue. This means that only one queue is possible, which in turn generally serves only one consumer.
Topic exchange
This exchange type extends the concept of direct exchange. Instead of just one criterion (routing key = binding key), several queues can be called upon. This works using placeholders. In this way, certain queues and binding keys can be accepted, while others remain excluded.
Fan-out exchange
Fan-out exchange is a broadcast. A message is distributed to all available queues without any sorting, and the routing key is ignored.
Header exchange
With header exchanges, too, the system ignores the routing key. Instead, the header of the message plays an important role, as it’s here that the exchange finds the attributes to direct the message to the correct queues. In this respect, a header exchange works analogously to topic exchanges, because in this case several queues, but not all, can be called upon.
Consumers, that is the receiving software, register to certain queues and take the messages from these. For this reason, only one consumer is envisaged per queue. If several consumers take messages out of a queue, the correct distribution can’t be guaranteed. Optionally, the user can decide for each message whether the recipient needs to confirm receipt or whether this isn’t necessary.