Messaging Patterns
Work queues, publish/subscribe, routing, and request/reply
Exchanges and queues combine into a few standard patterns. Knowing their names makes design discussions much shorter.
Work queue (competing consumers)
One queue, many consumers. Each message goes to one of them; RabbitMQ round-robins deliveries and redelivers anything a consumer didn't acknowledge. This is how you scale background jobs: add workers.
publisher --> [ default exchange ] --> lab.jobs --> worker 1
--> worker 2
--> worker 3How work is shared depends heavily on prefetch, the topic of the next chapter. Ordering is lost with more than one consumer: worker 2 may finish message 7 before worker 1 finishes message 6.
Publish/subscribe
One message, many independent consumers: a fanout (or topic) exchange with one queue per subscriber. Each service gets its own copy and processes at its own pace:
user.signed_up --> [ fanout ] --> email.welcome --> email service
--> crm.sync --> CRM service
--> analytics.users --> analyticsFor durable pub/sub, each subscriber needs its own named, durable
queue. The common shortcut, an exclusive server-named queue per running
consumer (queue_declare("", exclusive=True)), only receives messages
while that consumer is connected. Restart the service and everything
published in between is gone. That's fine for live notifications and wrong
for anything that must be processed.
Routing
Pub/sub with filtering: a topic (or direct) exchange where each
subscriber binds for the slice it cares about. The pre-loaded orders
exchange is this pattern: billing wants every order, EU shipping only
order.*.eu.
Request/reply (RPC)
A client sends a request and waits for a reply. The request carries a
reply_to (where to send the answer) and a correlation_id (which
request it answers). labs/rpc.py uses direct reply-to,
amq.rabbitmq.reply-to, a pseudo-queue that needs no declaration:
ch.basic_consume("amq.rabbitmq.reply-to", on_reply, auto_ack=True)
ch.basic_publish("", "lab.rpc.prices", body=sku,
properties=pika.BasicProperties(reply_to="amq.rabbitmq.reply-to",
correlation_id=cid))
# server side
ch.basic_publish("", props.reply_to, body=str(price),
properties=pika.BasicProperties(correlation_id=props.correlation_id))price(kb-01) = 50 round trip 6.7 ms
price(monitor-27) = 100 round trip 4.6 ms
price(hub) = 30 round trip 4.4 msRPC over a broker buys you load-balancing across servers and buffering during spikes, but it isn't free: every call needs timeouts on the client, servers must handle requests whose caller already gave up, and you've added a component to the request path. For simple service-to-service calls, HTTP or gRPC is usually simpler. Use RabbitMQ RPC when the requests are work that benefits from queueing.
Choosing
| Need | Pattern | Exchange |
|---|---|---|
| Spread jobs across workers | Work queue | Default or direct |
| Every service gets every event | Pub/sub | Fanout |
| Services get the events they care about | Routing | Topic |
| Call a service and wait for the answer | RPC | Default + reply-to |
| Replay, many readers, history | Not a queue: a stream | — |
Lab
- Run
rpc.py. Then stop the server thread early (shorten its loop) and add a client timeout that gives up after 1 second. - Build pub/sub for
user.signed_upwith three durable queues on thenotificationsfanout exchange. Stop one consumer, publish, restart it, and confirm it still gets the message. - Repeat with an exclusive server-named queue, and see the message disappear.