What makes up a Message Queue?
Let’s learn some of the terminology that is used in just about all message queues in computing.
AMQP
RabbitMQ is based on the Advanced Message Queuing Protocol, or AMQP. AMQP is a set of rules that helps define standards for the setup of queues and the delivery of messages between different systems. The concepts in this protocol are used in Magento’s RabbitMQ implementation.
Messages
The most crucial aspect of a message queue is the message. You can think of a message as a note that contains information about something... anything.
The whole idea of adding a message to a queue is to execute some action at a later time. The message typically carries data or information that is meant to be processed later, such as a data payload that can be acted upon.
Queues
The other crucial aspect of message queues is the queue itself. When a message is created, it needs to be stored someplace because it won’t be processed immediately. A queue is a buffer that temporarily holds messages. They’ll wait in line here until they are processed, one-by-one, in the order that they are added onto the queue.
Producers
Producers are responsible for creating and sending messages. They are sent either directly to the queue, or to an exchange. Within the AMQP protocol, these messages are published to an exchange.
Exchanges
Exchanges are like post office boxes. They are responsible for routing incoming messages to one or more queues.
There are four different types of exchanges: Direct, Fanout, Topic, and Headers. The type of exchange that Magento uses is Topic. Topic exchanges are not unique to Magento but are one of the types of exchanges defined in AMQP that Magento utilizes.
Topics
In Magento, queues are organized into groups called topics. A topic is a way for publishers to tag the messages they send, and for subscribers to listen to the topics in which they are interested.
Topics are simply names that are used to help determine where to route messages to and also represent the different types of messages that can be put on the queue.
Routing Rules
Message queues that use exchanges, and topics, employ routing rules to determine which queue a message should be sent to. Certain message properties may trigger specific routing rules to be followed, which then decide which queue a message is routed to.
The main routing rules that Magento uses are based on topic names and patterns. Topic exchanges route messages to queues based on wildcard matching between the routing key and the routing pattern, as defined in the binding.
Bindings
Before a message can be routed from an exchange to a queue, there must be a link established. In RabbitMQ, a binding is what links an exchange to a queue. These routing rules are defined within these bindings.
Consumers
Messages stay in the queue until they are ready to be handled. Then they are picked off a queue by a consumer, and the message is passed to a piece of code that then executes an action from the contents of it.
The consumer process is typically executed by a long-running background process that stays alive and continues to handle incoming messages as they come in. It can also be handled by polling the queue at intervals or triggered by other events such as executing a cron job.
Recap
It’s important to note that although Magento uses RabbitMQ, which is an implementation of AMQP, the concepts of producers, exchanges, and consumers are generic across various messaging systems. However, the specifics of how they are implemented can vary significantly between systems.
Also, while Magento defaults to using RabbitMQ for message queuing, it also abstracts out the queuing system so that other systems can be used.
Magento defines these concepts within various XML files, which we’ll cover in detail later on in the course.