DEV Community

Josephat Macharia
Josephat Macharia

Posted on

Introduction To RabbitMQ

What is RabbitMQ

RabbitMQ is a message broker that accepts and forwards messages.
It accepts messages from producers, and delivers them to consumers. It acts like a middleman which can be used to reduce loads and delivery times taken by web application servers.

Message flow in RabbitMQ

Alt Text

  1. The producer publishes a message to an exchange. When creating an exchange, the type must be specified.

  2. The exchange receives the message and is now responsible for routing the message. The exchange takes different message attributes into account, such as the routing key, depending on the exchange type.

  3. Bindings must be created from the exchange to queues. In this case, there are two bindings to two different queues from the exchange. The exchange routes the message into the queues depending on message attributes.

4.The messages stay in the queue until they are handled by a consumer

5.The consumer handles the message

Types of exchanges in RabbitMQ:

  1. Fanout
    This exchange basically sends a copy of messages to all the queues it knows. (in the dashboard every queue should be bind ed to one exchange)

  2. Direct
    A direct exchange delivers messages to queues based on a message routing key. The routing key is a message attribute added to the message header by the producer. Think of the routing key as an “address” that the exchange is using to decide how to route the message.

  3. Topic
    Topic exchanges route messages to one or many queues based on matching between a message routing key and the pattern that was used to bind a queue to an exchange.

4.Headers
A header exchange is an exchange that route messages to queues based on message header values instead of routing key. If a match is found, it routes the message to the queue whose binding value is matched and if a match is not found, it ignored the message.

RabbitMQ Terminologies and Concepts

Producer: A producer is a user application that sends messages to the consumers.

Consumer: A consumer is a user application that receives messages.

Message: It’s a simple serialized object from the producer!

Exchange: An exchange is a very simple thing. On one side it receives messages from producers and the other side pushes them to queues. This is a place that messages come first and its main purpose is to decide which consumer application, the message should go!

Queue:
A queue is a buffer that stores messages basically, the exchange routes the message to queues, then the consumer clients receive messages from the queue. We have queues as many as the consumer’s applications
The producers send the message to the exchange first, the exchange decides which queues, the message should go.

When to Use RabbitMQ

  • For long-running tasks, when you need to run reliable background jobs. e.g such as file scaning, image scaling or PDF
  • communication and integration within, and between applications, i.e as middleman between microservices; where a system simply needs to notify another part of the system to start to work on a task, like ordering handling in a webshop (order placed, update order status, send order, payment, etc.)

Top comments (0)