Related posts: Message Queues: Overview
RabbitMQ is a message broker; it accepts and forwards messages just like a post office.
Producing means sending a message.
Producers receive sent messages. A
queue is the name of a post box which lives inside RabbitMQ, only bound by the host’s memory and disk limits. Essentially it is a large message buffer.
Many producers can send messages that go to one queue and many consumers can try to receive data from that queue.
Consumers are programs that mostly wait to receive messages.
The publisher will connect to RabbitMQ, send a single message, then exit.
Channel is where most of the API for getting things done resides. To send a message we declare a queue for us to send to; then we can publish a message.
Declaring a queue is idempotent. Message content is a byte array so one can encode whatever needed.
Same as sending, we open a connection and a channel and then declare a queue from which we will consume.
Our code will block while our channel has callbacks. Whenever we receive a message our callback function will be passed the received message.
Messages are not published directly to a queue, instead, the producer sends messages to an
- An exchange is responsible for the routing of the messages to the different queues.
- An exchange accepts messages from the producer application and routes them to message queues with the help of bindings and routing keys.
bindingis a link between a queue and an exchange.
Message Flow in RabbitMQ
- The producer publishes a message to an exchange. When you create the exchange, you have to specify the type of it. Read about different types of exchanges.
- The exchange receives the message and is now responsible for the routing of the message. The exchange takes different message attributes into account, such as routing key, depending on the exchange type.
- Bindings have to be created from the exchange to queues. In this case, we see two bindings to two different queues from the exchange. The Exchange routes the message into the queues depending on message attributes.
- The messages stay in the queue until they are handled by a consumer
- The consumer handles the message.
Producer: Application that sends the messages.
Consumer: Application that receives the messages.
Queue: Buffer that stores messages.
Message: Information that is sent from the producer to a consumer through RabbitMQ.
Connection: A connection is a TCP connection between your application and the RabbitMQ broker.
Channel: A channel is a virtual connection inside a connection. When you are publishing or consuming messages from a queue - it’s all done over a channel.
Exchange: Receives messages from producers and pushes them to queues depending on rules defined by the exchange type. In order to receive messages, a queue needs to be bound to at least one exchange.
Binding: A binding is a link between a queue and an exchange.
Routing key: The routing key is a key that the exchange looks at to decide how to route the message to queues. The routing key is like an address for the message.
AMQP: AMQP (Advanced Message Queuing Protocol) is the protocol used by RabbitMQ for messaging.
Users: It is possible to connect to RabbitMQ with a given username and password. Every user can be assigned permissions such as rights to read, write and configure privileges within the instance. Users can also be assigned permissions to specific virtual hosts.
Vhost, virtual host: A Virtual host provides a way to segregate applications using the same RabbitMQ instance. Different users can have different access privileges to different vhost and queues and exchanges can be created so they only exist in one vhost.