RabbitMQ: AMQP Architecture, Exchange Types, and Routing Topologies
RabbitMQ is an open-source message broker implementing the Advanced Message Queuing Protocol (AMQP 0-9-1). It excels in complex routing, flexible delivery guarantees, task dispatching, and asynchronous RPC communication.
⚡ Quick Dive
The 4 Core Exchange Types
| Exchange Type | Routing Logic | Binding Key / Routing Key Example | Ideal Use Case |
|---|---|---|---|
| Direct | Exact match between routing key and binding key | orders.europe $\leftrightarrow$ orders.europe |
Targeted unicast task queues |
| Fanout | Ignores routing key; broadcasts to all bound queues | (Wildcard broadcast) | Pub/Sub notifications, cache invalidation |
| Topic | Pattern matching with wildcards (* = 1 word, # = 0+ words) |
stock.us.* matches stock.us.nyse |
Multi-criteria log and telemetry routing |
| Headers | Matches message headers (key-value) instead of routing key | x-match: all, format: pdf, type: invoice |
Content-aware routing |
RabbitMQ Core Architecture Flow
[ Producer ] ── (Publishes with Routing Key: 'user.signup') ──► [ Exchange: 'events' ]
│
┌──────────────────────────────────┴──────────────────────────────────┐
▼ (Binding: 'user.*') ▼ (Binding: '#')
┌─────────────────┐ ┌─────────────────┐
│ Welcome Email │ │ Audit Log Queue │
│ Queue │ │ │
└────────┬────────┘ └────────┬────────┘
│ │
▼ ▼
[ Email Worker ] [ Audit Worker ]
📖 Extended Guide
1. Consumer Acknowledgements, Prefetch & Backpressure
- Manual ACKs (
basic.ack/basic.nack):- Consumers only acknowledge after database persistence succeeds.
- If a consumer crashes before sending ACK, RabbitMQ automatically re-queues the message for another worker.
- Fair Dispatch (
basic.qosPrefetch):- By default, RabbitMQ pushes messages round-robin to all connected workers without checking queue depth.
- Set
basic.qos(prefetch_count=1)to instruct the broker not to send a worker a new message until it has processed and acknowledged its previous one.
2. High Availability: Quorum Queues vs. Classic Mirrored Queues
- Classic Mirrored Queues (Deprecated): Master-slave synchronization vulnerable to split-brain during network partitions.
- Quorum Queues (Modern Standard): Implemented using the Raft consensus algorithm. Replicates messages across a majority quorum of cluster nodes, guaranteeing FIFO ordering and zero data loss during node failure.