Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 37 ยท Routing Patterns

Fanout and Publish/Subscribe

Build publish/subscribe with the RabbitMQ fanout exchange: broadcasting to every bound queue, temporary subscriber queues, and fanout versus work queues.

When many different parties need to hear about the same event, you want publish/subscribe: the publisher sends once, and every subscriber receives its own copy. In RabbitMQ that is the job of the fanout exchange. It ignores the routing key completely and copies each message to every queue bound to it.

pika blocks are read-only for a real broker; runnable blocks are Python stand-ins of the same behavior.

The pattern

flowchart LR
    P["Publisher: user.registered"] --> X(["events (fanout)"])
    X --> Q1["email service queue"]
    X --> Q2["analytics queue"]
    X --> Q3["audit queue"]
    Q1 --> C1["Mailer"]
    Q2 --> C2["Analytics"]
    Q3 --> C3["Audit logger"]

One publish, three independent queues, three independent consumers.

Each subscriber has its own queue. That is the key to the pattern: queues decide who gets a copy, so sharing a queue between two services would make them compete instead of both receiving the message. Within one service, several instances can consume from that service's queue to share its workload.

Real code

Python
# publisher.py  (real pika code, read-only)
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
channel = connection.channel()

channel.exchange_declare(exchange="events", exchange_type="fanout", durable=True)

channel.basic_publish(
    exchange="events",
    routing_key="",                      # ignored by fanout
    body=b'{"type":"user.registered","user":"ada"}',
)
connection.close()
Python
# subscriber.py  (real pika code, read-only)
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
channel = connection.channel()
channel.exchange_declare(exchange="events", exchange_type="fanout", durable=True)

# Variant 1: a private temporary queue (gets only messages published while connected)
queue = channel.queue_declare(queue="", exclusive=True).method.queue
channel.queue_bind(exchange="events", queue=queue)

# Variant 2: a durable named queue per service (keeps messages while the service is down)
# channel.queue_declare(queue="analytics.events", durable=True)
# channel.queue_bind(exchange="events", queue="analytics.events")

def on_event(ch, method, properties, body):
    print("event:", body.decode())

channel.basic_consume(queue=queue, on_message_callback=on_event, auto_ack=True)
channel.start_consuming()

The routing key passed to queue_bind and basic_publish is ignored, so leave it empty.

Temporary or durable subscribers?

This is the most important design choice in publish/subscribe:

  • Temporary subscriber (exclusive, server-named queue): receives only events published while it is connected. When it disconnects its queue is deleted. Use it for live dashboards and debugging tools.
  • Durable subscriber (named, durable queue, often quorum): the queue exists even when the service is offline, so events accumulate and are processed when the service returns. Use it for services that must not miss events.

The stand-in demonstrates the difference with a subscriber that is offline for a while.

Python Fanout with temporary vs durable subscribers (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The new dashboard never sees Ada or Grace: a fresh queue starts empty. The durable queue holds everything.

Fanout versus work queues

Both patterns put several consumers on the scene; the difference is how queues are arranged.

Two ways to use many consumers

Work queue Many consumers share ONE queue. Each message is handled once.
Pub/sub Each consumer group has ITS OWN queue. Each gets every message.
Combined Fanout to one queue per service, many workers per queue.

Most real systems use the combined form.

1 / 2
Python Combined: fanout to services, workers per service (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Every event is handled once per service, and shared among that service's workers.

Things to know

  • Fanout ignores routing keys, so there is no filtering. If subscribers want only some events, use a topic exchange (The Topic Exchange).
  • Cost grows with subscribers. Each bound queue stores its own copy, so a fanout to hundreds of queues multiplies memory and disk work. For huge fan-out with replay, consider a stream.
  • A fanout with no bindings drops everything. Publish before any queue is bound and the messages are lost.
  • Slow subscribers only fill their own queue. Add limits (Queue Arguments) so one stuck subscriber cannot grow without bound.
  • The predeclared exchange amq.fanout exists, but declare your own with a meaningful name.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a fanout exchange do with the routing key?

    Fanout copies the message to every bound queue regardless of the routing key.
  2. 2. Why does each subscriber need its own queue in publish/subscribe?

    Copies are made per queue. Consumers on the same queue split the messages between them.
  3. 3. Which subscriber type receives events published while the service is offline?

    A durable named queue persists and accumulates events while the consumer is away.
  4. 4. What happens to a message published to a fanout exchange that has no bound queues?

    Exchanges store nothing, so without bindings the message is dropped unless mandatory or an alternate exchange is used.

Summary

  • A fanout exchange copies every message to every bound queue and ignores the key.
  • Each subscriber (or service) needs its own queue; temporary queues miss events while offline, durable named queues do not.
  • Combine fanout between services with work queues inside each service.
  • Fanout cannot filter; use a topic exchange when subscribers want subsets.

Next: The Topic Exchange