π RabbitMQ
π Overviewβ
RabbitMQ is a message broker that enables asynchronous communication between services. It uses the AMQP (Advanced Message Queuing Protocol) to send, route, store, and deliver messages between producers and consumers.
Itβs like a post office β producers send messages to an exchange, which routes them to queues, and consumers receive from those queues.
βοΈ 1. Technical Working (Architecture & Flow)β
Componentsβ
- Producer β Sends messages to an exchange.
- Exchange β Routes messages to queues based on rules.
- Queue β Stores messages until consumed.
- Consumer β Receives and processes messages.
- Binding β Rule connecting an exchange to a queue.
- Routing Key β Address label for routing messages.
Flow of a Messageβ
- Producer sends a message with a routing key to an exchange.
- Exchange checks its bindings and routing logic.
- Message is placed into one or more queues.
- Consumer(s) receive messages from those queues.
- Consumer sends ACK after successful processing.
Exchange Typesβ
| Type | Behavior | Example Use Case |
|---|---|---|
| Direct | Routes by exact routing key match | Logging system |
| Fanout | Broadcasts to all bound queues | Notifications |
| Topic | Pattern-based routing with wildcards | Event-driven systems |
| Headers | Routes using message headers | Complex filtering |
π’ Direct Exchangeβ
Routes messages based on exact routing key match.
Example:
- Exchange:
logs(type: direct) - Bindings:
- Queue A β key
error - Queue B β key
info
- Queue A β key
β‘οΈ Message with key error β Queue A only.
π Fanout Exchangeβ
Broadcasts messages to all bound queues, ignoring routing key.
Example:
- Exchange:
broadcast - Queues: A, B, C (all bound)
β‘οΈ Any message β A, B, C all receive.
π΅ Topic Exchangeβ
Routes messages using pattern matching on routing key.
Wildcards:
*β matches one word#β matches zero or more words
Example:
- Queue A bound to
order.* - Queue B bound to
order.#
β‘οΈ order.created β goes to both A & B.
π£ Headers Exchangeβ
Routes by message headers instead of routing keys.
Example:
- Queue A bound
{x-match=all, format=pdf, type=report} - Queue B bound
{x-match=any, format=pdf, type=log}
β‘οΈ Headers {format=pdf, type=report} β both A & B receive.
π 2. Binding β The Glueβ
A binding connects an exchange to a queue and defines the routing rule.
You can bind:
- Many queues to one exchange β
- One queue to many exchanges β
For e.g.
channel.queueBind("orderQueue", "orderExchange", "order.created");
π§ 3. Scaling & Trade-offsβ
Why RabbitMQ Doesnβt Scale Horizontally Like Kafkaβ
- Each queue lives on one node β single-node ownership.
- Scaling = add more queues + consumers, not more brokers.
- Mirroring = replication, not distribution (adds overhead).
Kafka solves this with partitioned topics, enabling true horizontal scaling.
Pros β β
- Simple and reliable messaging.
- Supports multiple patterns (work queue, pub/sub, RPC).
- Fast with low latency.
- Mature ecosystem.
Cons ββ
- Limited horizontal scaling.
- No message replay (unlike Kafka).
- Operational complexity in clusters.
π§ 4. Best Practicesβ
-
Use durable queues and persistent messages for reliability.
- durable queues store message in disk as well not just on ram so it works well even queue node goes down
-
Enable manual acknowledgments (
ACK/NACK).- Avoid message loss
-
Tune prefetch count for consumer load balancing.
- it's like conmsumer will prefetch a amount of message so no oveload happens or message bombarding in peak time.
-
Use lazy queues for large message backlogs.
- Normally, RabbitMQ stores messages in RAM for speed.
- But when queues get large (millions of messages), RAM fills up fast β system slows down.
- To solve this, lazy queues store messages on disk by default, loading them into memory only when needed.
-
Apply TLS, authentication, and vhosts for security.
-
Avoid large payloads β store externally (e.g., S3) and send metadata.