Sub topic 12
Publish/subscribe and routing
Prerequisites
This chapter assumes you know what a producer, a consumer, a broker and a queue are. It also assumes you know how acknowledgements work. If not, review those chapters first.
From work queues to publish/subscribe
In a work queue, each message goes to exactly one consumer. That fits tasks such as "resize image 42". Some messages are events instead: "order placed" may interest billing, shipping and analytics at once.
Publish/subscribe (pub/sub) delivers a copy of each message to every interested subscriber. The producer does not know who the subscribers are. You can add a new subscriber without changing the producer.
Exchanges, bindings and queues
In RabbitMQ, producers do not publish to queues directly. They publish to an exchange, which is a routing component inside the broker. A binding links an exchange to a queue and may carry a binding key. The exchange compares each message's routing key with the bindings and copies the message into every queue that matches.
Common exchange types:
| Type | Routing rule | Typical use |
|---|---|---|
| direct | Routing key equals binding key exactly | Send a task to a specific queue |
| fanout | Ignores the key, copies to all bound queues | Broadcast to every subscriber |
| topic | Key matches a pattern; `*` matches one word, `#` matches zero or more | Selective event subscriptions |
Example: a topic exchange
Each subscriber gets its own queue. The queue stores messages for that subscriber while it is busy or offline.
import pika
conn = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
ch = conn.channel()
ch.exchange_declare(exchange="events", exchange_type="topic", durable=True)
for name, pattern in [("billing", "order.*"), ("analytics", "#")]:
ch.queue_declare(queue=name, durable=True)
ch.queue_bind(queue=name, exchange="events", routing_key=pattern)
ch.basic_publish(
exchange="events",
routing_key="order.created",
body=b"order 42",
properties=pika.BasicProperties(delivery_mode=pika.DeliveryMode.Persistent),
)
conn.close()Both queues receive a copy of order 42. A message with key user.deleted would reach only analytics.
Competing consumers still apply
Pub/sub and work queues combine. Each subscriber has one queue, but you can run several instances of that subscriber on the same queue. Each message is copied once per queue, then handled by one instance per queue. Billing gets every event once, however many billing workers you run.
Common errors
- Publishing before binding. If no queue is bound when you publish, the broker drops the message without an error by default. Declare and bind queues before the first publish.
- Sharing one queue between different subscribers. Billing and shipping would compete for messages, so each would see only some of them. Give each subscriber its own queue.
- Misreading
*and#.order.*does not matchorder.item.added.order.#does. - Exclusive, auto-delete queues for important consumers. Temporary queues vanish when the consumer disconnects, and any events published meanwhile are lost. Use named, durable queues for subscribers that must not miss events.
Exercise
A "payment.failed" event must reach the billing and support services, and billing must run three worker instances. How do you set up queues and bindings?
Show answer
Create one durable queue for billing and one for support, and bind each to the exchange with a key that matches "payment.failed" (for example "payment.*"). Start three billing consumers on the billing queue, so they compete for its messages. Start one or more consumers on the support queue. Each service receives every event once, and the three billing workers share that load.