Message Queues, Exchange Types and using RabbitMQ on Docker

Fatih Adıyaman
Neyasis Technology
Published in
5 min readJun 5, 2020

In this post, I aimed to have a general knowledge about message queues, to understand the working logic of Exchange Types and to make a simple .Net Core API application using RabbitMQ on Docker. Docker setup and integration will not be included here in order not to deviate much from the subject.

Message Queues

The message queue transmits the message or content to the queue between the message producing and consuming the message, providing an asynchronous working protocol in cases where the process does not require immediate response. Examples of this are the processes that are used in many projects, sending email, uploading files and processes that depend on one process and enable many processes to occur.

RabbitMQ, Apache Kafka, MsMQ, Microsoft Azure Service Bus, Kestrel, ActiveMQ are the systems to be given as examples to this structure.

In general, terms in the message queues are named according to the message. In order to explain these terms according to RabbitMQ, the term that sends or produces the message or event is called Producer and the Consumer receives the message from the queue, if we want to delete message form the queue, just send an auto acknowledgement. Usually the queue side works according to the FIFO(First-In-First-Out) logic.

To exemplify this structure, you have forgotten your password to login to a system and asked for your new password by mail. The system said that your transaction was successful before the password reached you and directed you to the login page. Your role was actually to trigger Producer, that function fulfilled your password request left a message in the queue and enabled Consumer to perform the required mail sending process. The important part is that this process is completed at any time.

Exchange Types

The messages sent to the RabbitMQ service are met by the exchange and the related message is prcessed by forwarding it to the relevant queue according to the designed exchange type. That is the exchange takes tha data or message from Producer and transmits it to the relevany queue or queues according to its type.

Although we diversify the Exchange Types in RabbitMQ, they are actually AMQP(Advanced Message Queuing Protocol) parts.

What is the AMQP?

AMQP is an open standard for communication between applications, organizations or devices, if simply defined. Its purpose is to ensure that the message is delivered without any loss. It operates over TCP and endpoints or volumes return a confirmation notification that they have received the message. Its advantages are being able to connect applications on different platforms, work asynchronously and adapt easily to new changing needs.

We will examine the Exchange types in 4 different types.

  • Topic Exchange
  • Direct Exchange
  • Header Exchange
  • Fanout Exchange

The image below summarizes all exchange types. Due to the similarity of Topic and Header Exchanges, it was described under a single title.

Topic Exchange

The routing key value of the Topic Exchange messages is named using the dot and transmits the messages to the relevant queue.

The logic of the distribution to the queues with routing keys:

  • tv.comedy : It is only transmitted to the queue with this routing key.
  • tv.# : tv.<does not matter>
  • # : <does not matter> accepts all messages.

The ‘#’ sign provides incredible flexibility for routing keys.

Header Exchange

It is the conversion of the routing key logic in the topic exchanged into the key-value form. It is an exchange type that transmits to the matching queue according to the key-value format read from the header of the relevant message.

The point to be considered here is the ‘x-match’ and ‘any’ or ‘all’ daya used on the value side. Functionally, it checks its status with any or all of its header values and transmits it to the corresponding queue.

Direct Exchange

Direct Exchange delivers the message directly to the relevant address. With the routing key given to the message, it ensures that the message is transmitted to all queues with that routing key value.

Fanout Exchange

In this exchange type the message is transferred directly to the relevant queues. No routing keys or types are specified here. It is transferred unconditionally to all queues attached to the message.

.Net Core API Project on Docker

In this application, I will illustrate Producer and Consumer with two .Net Core API projects. I will continue by activating RabbitMQ and two .Net Core API projects on Docker without any setup for RabbitMQ in our local.

For this application, Docker must be installed on the existing system. The projects and RabbitMQ settings are as follows on the Docker.

Our aim is to send a message to Fanout type exchange defined in RabbitMQ for a material sold through Producer and leave a message to the relevant queues. After the request we send over the consumer, we will receive the relevant message from the relevant queue by the stock or shipping methods.

The ConecctionService class must be common to the Consumer and Producer projects.

Producer

In the images below, the message publish method is shared on the Producer application. Fanout type Exchange and two queues related to these exchanges were defined and the related message was published.

Consumer

On this side, we have a function that consumes messages in the queue. We can pull the message we want from the relevant queue.

Results

The example of request in Producer project
The messages status in the RabbitMQ queue
The response of the Consumer API

You can access the related project through the links specified on github.

--

--