RabbitMQ Overview

Jul 6, 2024

RabbitMQ Overview

Introduction

  • RabbitMQ is an open-source distributed message broker.
  • Functions like a post office in the cloud.
  • Developed in 2007, written in Erlang (used for the Open Telecom Platform).

Evolution of Application Architecture

  • Initially, apps built as monoliths (single runtime with all concerns coupled).
  • Problem: Not all components scale in parallel.
  • Emergence of microservice architecture (independent runtimes for different concerns).
  • RabbitMQ enables asynchronous communication between microservices using various protocols.

Example Use Case

  • App applying deep learning photo filters:
    • User request goes to REST API.
    • API publishes a message to an exchange instead of processing directly.
    • Exchange routes message to one or more queues.
    • Consumer (image processing server) handles the message from the queue.

Routing Messages

  • Exchange: Routes messages to queues using bindings and routing keys.
  • Types of routing:
    • Direct: Specific queue.
    • Topics: Multiple queues with shared patterns.
    • Fanout: Every queue it knows about.

Getting Started

  • Install RabbitMQ or run it in a Docker container (port 5672).
  • Use CLI tool to manage and inspect the broker.

Implementation Steps

  1. Create a file in your preferred server-side language.
  2. Bring in a library for a messaging protocol (e.g., AMQP 0.9.1).
  3. Publishing a message:
    • Connect to RabbitMQ.
    • Use createChannel method to declare a queue (durable or transient).
    • Send a buffer message to the queue.
  4. Consuming a message:
    • Create another file for the receiver.
    • Connect to RabbitMQ and reference the same queue.
    • Use consume method to receive the message and run a callback.

Expanding Functionality

  • Create an Exchange to manage multiple queues.
  • Use a fanout or topic exchange to allow multiple servers to subscribe to the same messages and consume them at different times.

Conclusion

  • RabbitMQ enables scalable, asynchronous communication between microservices.
  • Various routing patterns enhance flexibility in message handling.
  • Useful in developing robust microservice architectures.