RabbitMQ and AMQP 0-9-1 message broker implementation
Scope: AMQP 0-9-1 protocol, RabbitMQ broker, exchanges, queues, routing patterns, clustering, high availability Lines: ~350 Last Updated: 2025-10-27
Activate this skill when:
AMQP (Advanced Message Queuing Protocol): Open standard application layer protocol for message-oriented middleware.
Key characteristics:
Message flow:
Publisher → Exchange → Binding → Queue → Consumer
↓ ↓
Routing Key Delivery Ack
Routes messages with a specific routing key to queues bound with that exact key.
Use cases:
Example binding:
Exchange: tasks
Queue: email_tasks Binding: email
Queue: sms_tasks Binding: sms
Queue: push_tasks Binding: push
Message with routing_key="email" → email_tasks queue
Routes messages to all bound queues (ignores routing key).
Use cases:
Example:
Exchange: notifications
Queue: slack_notifier
Queue: email_notifier
Queue: sms_notifier
All queues receive every message
Routes based on wildcard pattern matching of routing keys.
Use cases:
Patterns:
* matches exactly one word# matches zero or more wordsExample:
Routing key: "order.created.us"
Binding: "order.*.*" → Matches
Binding: "order.created.#" → Matches
Binding: "order.#" → Matches
Binding: "*.created.*" → Matches
Binding: "invoice.#" → No match
Routes based on message header attributes instead of routing key.
Use cases:
Example:
Headers: {type: "order", region: "us", priority: "high"}
Binding: {type: "order", region: "us"} → Matches if x-match=all
Goal: Distribute tasks among multiple workers.
Setup:
Python example:
import pika
# Publisher
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='tasks', durable=True)
channel.basic_publish(
exchange='',
routing_key='tasks',
body='Task data',
properties=pika.BasicProperties(delivery_mode=2) # Persistent
)
# Consumer
def callback(ch, method, properties, body):
print(f"Processing: {body}")
# Do work...
ch.basic_ack(delivery_tag=method.delivery_tag)
channel.basic_qos(prefetch_count=1) # Fair dispatch
channel.basic_consume(queue='tasks', on_message_callback=callback)
channel.start_consuming()
Goal: Broadcast messages to multiple subscribers.
Setup:
Python example:
# Publisher
channel.exchange_declare(exchange='logs', exchange_type='fanout')
channel.basic_publish(exchange='logs', routing_key='', body='Log message')
# Subscriber
result = channel.queue_declare(queue='', exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='logs', queue=queue_name)
channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)
Goal: Subscribe to subset of messages.
Setup:
Python example:
# Publisher
channel.exchange_declare(exchange='logs', exchange_type='direct')
channel.basic_publish(exchange='logs', routing_key='error', body='Error log')
# Consumer (only error logs)
channel.queue_bind(exchange='logs', queue=queue_name, routing_key='error')
Goal: Remote procedure calls with response.
Setup:
Python example:
# Client
import uuid
class RpcClient:
def __init__(self):
self.connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
self.channel = self.connection.channel()
result = self.channel.queue_declare(queue='', exclusive=True)
self.callback_queue = result.method.queue
self.channel.basic_consume(
queue=self.callback_queue,
on_message_callback=self.on_response,
auto_ack=True
)
self.response = None
self.corr_id = None
def on_response(self, ch, method, props, body):
if self.corr_id == props.correlation_id:
self.response = body
def call(self, n):
self.response = None
self.corr_id = str(uuid.uuid4())
self.channel.basic_publish(
exchange='',
routing_key='rpc_queue',
properties=pika.BasicProperties(
reply_to=self.callback_queue,
correlation_id=self.corr_id,
),
body=str(n)
)
while self.response is None:
self.connection.process_data_events()
return int(self.response)
Requirements:
Python example:
# Durable queue
channel.queue_declare(queue='tasks', durable=True)
# Persistent message
channel.basic_publish(
exchange='',
routing_key='tasks',
body='Important task',
properties=pika.BasicProperties(
delivery_mode=2, # Persistent
)
)
Note: Persistence doesn't guarantee 100% durability. For stronger guarantees, use publisher confirms.
Ensure message was written to disk before considering it sent.
Python example:
channel.confirm_delivery()
try:
channel.basic_publish(
exchange='',
routing_key='tasks',
body='Task',
properties=pika.BasicProperties(delivery_mode=2),
mandatory=True
)
print("Message delivered")
except pika.exceptions.UnroutableError:
print("Message could not be routed")
Consumer should acknowledge after processing completes.
def callback(ch, method, properties, body):
try:
process_message(body)
ch.basic_ack(delivery_tag=method.delivery_tag)
except Exception as e:
# Requeue on failure
ch.basic_nack(delivery_tag=method.delivery_tag, requeue=True)
channel.basic_consume(
queue='tasks',
on_message_callback=callback,
auto_ack=False # Manual ack
)
Control how many messages delivered to consumer before acknowledgment.
channel.basic_qos(prefetch_count=1) # One at a time (fair dispatch)
channel.basic_qos(prefetch_count=10) # Batch processing
Best practice: Use prefetch_count=1 for long-running tasks to ensure fair distribution.
Route failed messages to another exchange for retry or inspection.
Setup:
# Main queue with DLX
channel.queue_declare(
queue='tasks',
durable=True,
arguments={
'x-dead-letter-exchange': 'dlx',
'x-dead-letter-routing-key': 'failed.tasks',
'x-message-ttl': 60000 # Optional: TTL before DLX
}
)
# Dead letter exchange
channel.exchange_declare(exchange='dlx', exchange_type='direct')
# Dead letter queue
channel.queue_declare(queue='failed_tasks', durable=True)
channel.queue_bind(exchange='dlx', queue='failed_tasks', routing_key='failed.tasks')
Messages sent to DLX when:
requeue=FalseSetup (3-node cluster):
# Node 1
rabbitmq-server -detached
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app
# Node 2
rabbitmq-server -detached
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster rabbit@node1
rabbitmqctl start_app
# Node 3
rabbitmqctl stop_app
rabbitmqctl join_cluster rabbit@node1
rabbitmqctl start_app
Replicated queue type with high availability and data safety.
Python example:
channel.queue_declare(
queue='ha_tasks',
durable=True,
arguments={
'x-queue-type': 'quorum'
}
)
Characteristics:
Reuse connections and channels.
import pika.pool
params = pika.ConnectionParameters('localhost')
pool = pika.pool.ConnectionPool(
lambda: pika.BlockingConnection(params),
max_size=10,
max_overflow=20,
timeout=10,
recycle=3600
)
with pool.acquire() as connection:
channel = connection.channel()
channel.basic_publish(...)
Publish multiple messages in one go.
for i in range(1000):
channel.basic_publish(
exchange='',
routing_key='tasks',
body=f'Task {i}'
)
# All published in one network round trip
Keep messages on disk instead of RAM.
channel.queue_declare(
queue='large_queue',
durable=True,
arguments={'x-queue-mode': 'lazy'}
)
Use when: Queue has many messages (millions) or messages are large.
Monitor these via management API or Prometheus:
Queue metrics:
messages_ready: Messages ready for deliverymessages_unacknowledged: Messages delivered but not ackedmessage_stats.publish: Publish ratemessage_stats.deliver: Delivery rateConnection metrics:
connection_count: Active connectionschannel_count: Active channelsNode metrics:
mem_used: Memory usagefd_used: File descriptorsdisk_free: Available disk spacePython monitoring example:
import requests
response = requests.get(
'http://localhost:15672/api/queues',
auth=('guest', 'guest')
)
queues = response.json()
for queue in queues:
print(f"{queue['name']}: {queue['messages']} messages")
x-max-length