Learn Labs
Routing

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 3

How 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_upnotificationsfanoutemail.welcomecrm.syncanalytics.usersemail serviceCRM serviceofflineanalyticscrm.sync keeps collecting while the CRM service is down; it catches up on reconnect
user.signed_up --> [ fanout ] --> email.welcome   --> email service
                           --> crm.sync        --> CRM service
                           --> analytics.users --> analytics

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 ms

Choosing

NeedPatternExchange
Spread jobs across workersWork queueDefault or direct
Every service gets every eventPub/subFanout
Services get the events they care aboutRoutingTopic
Call a service and wait for the answerRPCDefault + reply-to
Replay, many readers, historyNot a queue: a stream—

Lab

  1. Run rpc.py. Then stop the server thread early (shorten its loop) and add a client timeout that gives up after 1 second.
  2. Build pub/sub for user.signed_up with three durable queues on the notifications fanout exchange. Stop one consumer, publish, restart it, and confirm it still gets the message.
  3. Repeat with an exclusive server-named queue, and see the message disappear.

On this page