Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 12, 2021 08:05 am GMT

Part 1: Short introduction to RabbitMQ

If you like what I wrote and want to support me, please follow me on Twitter to learn more about programming and similar topics. Also check out my blog with more details about this topic here

In this blog post, we are going to cover an overview of what RabbitMQ is and give an example of when you would apply this technology.

What is RabbitMQ?

RabbitMq is an open source message broker software. sometimes called message-oriented middleware, that originally implemented the Advanced Message Queuing Protocol or >AMQp for short, and has since been extended with a plug-in architecture to support Streaming Text Oriented Messaging Protocol (STOMP), Message Queuing Telemetry >Transport (MQTT), and other protocols.

The simplest way of putting it would be RabbitMQ is used to distribute long-running task that doesnt require immediate user feedback into a separate process.

Now since we have a good understanding of the definition lets dive deep into the components of RabbitMq, its topology and how we can use it.

RabbitMq has 5 different exchanges:

  • Direct exchange
  • Fanout exchange
  • Header exchange
  • Dead Letter exchange
  • Topic exchange

Basic components of RabbitMq

Exchanges

ExchangeDescription
DirectDirect exchange delivers messages to queues based on a message routing key.
FanoutFanout exchange routes messages to all of the queues that are bound to it and the routing key is ignored.
HeaderHeaders exchange routes messages based on arguments containing headers and optional values. Headers exchanges are very similar to topic exchanges, but route messages based on header values instead of routing keys.
Dead Letter ExchangeProvides the functionality to capture messages that are not deliverable.
TopicTopic exchanges route messages to queues based on wildcard matches between the routing key and the routing pattern, which is specified by the queue binding. Messages are routed to one or many queues based on a matching between a message routing key and this pattern.

Components

ExchangeDescription
BindingBinding is a link that you set up to bind a queue to an exchange.
Routing keyRouting key The routing key is a message attribute. The exchange might look at this key when deciding how to route the message to queues (depending on exchange type).
ProducersJob of the producer is to send a new message to the exchange.
MessageMessage represents value you want the consumer to recieve and process.
QueueQueues are ordered collections of messages.
ExchangeExchange routes the message to the right queue.
ConsumersConsumers is a client that receives messages.

Direct exchange

Direct exchange topology

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. A message goes to the queue(s) with the binding key that exactly matches the routing key of the message.

Fanout exchange

Fanout exchange topology

A fan-out topology is when the producer sends a message to the exchange and the exchanges ignore the routing key and just sends the task directly to all of the queues that are available.

Topic exchange

Topic exchange topology

The logic behind the topic exchange is similar to a direct one - a message sent with a particular routing key will be delivered to all the queues that are bound with a matching binding key. There are two important special cases for binding keys:

  • * can substitute for exactly one word.
  • # can substitute for zero or more words.

Dead Letter exchange

There are three identified situations where a message becomes undeliverable after reaching RabbitMQ:

  • A message is negatively acknowledged by the consumer
  • The TTL of a message expires
  • The queue reaches capacity

By default, the broker drops these messages. Publishing is successful, however, the RabbitMQ consumer never handles or has a change to handle the message successfully.

Queues attached to a dead letter exchange collect dropped messages, with the next steps determined by developer. In other words - it's up to you to decide how to handle messages in the dead letter queue. When implemented correctly, information is almost never lost.

Header exchange

Header exchange topology

Headers exchange is an exchange which route messages to queues based on message header values instead of routing key. Producer adds some values in a form of key-value pair in message header and sends it to headers exchange. After receiving a message, exchange try to match all or any (based on the value of x-match) header value with the binding value of all the queues bound to it.

If you like what I write and want to support me, please follow me on Twitter to learn more about programming and similar topics


Original Link: https://dev.to/thedailytechtalk/part-1-short-introduction-to-rabbitmq-52nl

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To