Sub topic 3
The broker and its queues
This chapter assumes you know what producers and consumers are, and that you can run Python and a local RabbitMQ broker. The examples use RabbitMQ, but the ideas apply to most brokers.
What the broker does
A broker is a server that accepts messages from producers, stores them, and hands them to consumers. Producers and consumers never connect to each other. Each side only needs the broker's address and the name of a queue.
A queue is an ordered buffer inside the broker. Messages enter at the tail and are delivered from the head. The broker keeps a message until a consumer acknowledges it, so the producer can finish and move on while the consumer is slow or offline.
Exchanges and routing keys
In RabbitMQ, producers publish to an exchange, not directly to a queue. The exchange applies routing rules and copies the message into the queues that match. The empty-string exchange is the default exchange. It routes a message to the queue whose name equals the routing key. That is why routing_key="tasks" reaches the tasks queue in the examples. Later chapters cover other exchange types for publish/subscribe.
Other brokers use other names. Amazon SQS has queues without exchanges, and Kafka has topics. The core idea is the same: the broker sits between the two sides and owns the storage.
Declaring a queue
A client declares a queue before using it. Declaring is idempotent: if the queue already exists with the same settings, nothing changes.
import pika
conn = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
ch = conn.channel()
ch.queue_declare(queue="tasks", durable=True)
ch.basic_publish(exchange="", routing_key="tasks", body=b"resize image 42")
status = ch.queue_declare(queue="tasks", durable=True)
print("ready messages:", status.method.message_count)
conn.close()The second queue_declare returns the queue's current state. message_count is the number of messages waiting to be delivered. If nothing consumes from the queue, it grows with each run.
Queue properties that matter
| Property | Meaning |
|---|---|
| `durable` | The queue definition survives a broker restart. |
| `exclusive` | Only the declaring connection can use it, and it is deleted when that connection closes. |
| `auto_delete` | The queue is removed after its last consumer leaves. |
| Message TTL | Messages older than the limit are dropped or dead-lettered. |
| Max length | Caps the queue size, and the broker drops or rejects messages once it is full. |
Common errors
- Publishing to a queue that does not exist. With the default exchange, a message whose routing key matches no queue is silently discarded. Declare the queue first, from both the producer and the consumer.
- Assuming a durable queue keeps its messages.
durable=Truekeeps the queue, not its contents. Messages also need to be published as persistent, which a later chapter covers. - Letting a queue grow without limit. A queue with no consumer, or a slow one, uses broker memory and disk until the broker raises an alarm. Set a maximum length or TTL where losing old messages is acceptable.
Exercise
A producer publishes to the default exchange with routing key "reports", but no queue named "reports" has ever been declared. What happens to the message, and how do you prevent it?
Show answer
The broker discards the message without an error, because no queue matches the routing key. Prevent it by declaring the queue (idempotently) before publishing, or before the consumer starts, so a queue exists to receive the message.