ryanjang-devnotes
Published in

ryanjang-devnotes

RabbitMQ 101

This story summarizes Whitney Lee’s online lecture at IBM Technology Youtube channel. For more details, watch What is RabbitMQ.

RabbitMQ — dev.to

Introduction

In the days of monolithic architecture, application components were tightly coupled. Suppose we are developing a simple retail application. If we had a checkout service communicating with an inventory service through TCP connection, it would have some limitations. As soon as the checkout service sent it out to the inventory, it would need to hear a reply before it could move on to the next task. Or worse, if the inventory service went down, it would try over and over again until it was able to make the connection. Or, if a lot of checkouts happened at once, the inventory service wouldn’t be able to keep up and the whole system would get bogged down. That’s why the message broker was created.

Benefits of Message Queue

With a message queue, the checkout can add a message to the queue and immediately move on to the next task. And the inventory, when it’s ready, can consume from the queue, process the message, and then immediately consume the next one. So, this makes the two applications decoupled.

A message broker can also help with scalability. If a lot of checkouts happen at once, the queue begins to fill. Now that it is possible to have more than one consuming service in the message queue, you can handle the amount of workloads that the checkout is producing. That’s gonna make the system more scalable.

Another big benefit of the message queue is that the queue itself can sit on its own machine. In that case, it can offload some of the work that’s done by the web application and make the whole system performant.

What is RabbitMQ?

RabbitMQ is an implementation of the AMQP message model(Advanced Message Queueing Protocol), which is version 0-9-1 specifically.

How It Works

Instead of sending messages directly, the checkout service model is going to produce them to the exchange, which is like a post office receiving and distributing all the messages to the right place. The exchange is connected with many queues through connections called ‘Bindings’, which is a relationship between an exchange and a queue. The bindings are referenced by binding keys. Each queue is connected with a consuming service, or a consumer shortly. The consumers subscribe to the queues. This is the brief system that we have known about RabbitMQ.

Image: https://www.youtube.com/watch?v=7rkeORD4jSw

One great benefit of the message model is flexibility. That flexibility is largely in part to the different types of exchange.

  • Fanout Exchange: When the checkout sends a message to the exchange, it duplicates the message and sends it to every single queue it knows about.
  • Direct Exchange: With the direct exchange, the checkout produces a message that has a routing key. The routing key is being compared to the binding key, and if it’s exactly matched, it will move on to the system accordingly.
  • Topic Exchange: the topic exchange matches both keys, based on a pattern called ‘topic’. Unlike a direct exchange, it can’t technically have an arbitrary routing key — it must be a list of words, delimited by dots.
  • Header Exchange: With the header exchange, the routing key is totally ignored. Rather, the message is moved to the whole system by header.
  • Default Exchange: default exchange is unique to RabbitMQ as it is not part of the AMQP model. Every queue that is created is automatically bound to it with a routing key which is the same as the queue name.

Main Benefits of RabbitMQ

A tremendous amount of flexibility: All the configurations for how a message moves are all defined when a messaging administrator sets up a message model. In RabbitMQ, the way it moves through the system is a part of the message metadata, It is the application and the developer that have a lot of control rather than the message broker administrator.

Cloud-friendly: You can deploy a RabbitMQ instance on Docker or any containerization software.

Good Security: It supports FASL, LDAP, and TLS for authentication and authorization.

Message acknowledgments: If a message is in a queue, it stays until a consumer lets the broker knows that it takes the message out. That prevents losing messages.

A lot of plug-ins: The open-source community of RabbitMQ has created a lot of plugins that enrich most aspects of RabbitMQ. It is so evolved that RabbitMQ now supports other message models such as AMQP 1.0, MQTT, STOMP

Thanks for reading.

References

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Minho Jang

Minho Jang

Backend Developer, Writer, and Lifelong Learner