100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Messaging

Publish-Subscribe Pattern

Learn how RabbitMQ's fanout exchange implements publish-subscribe messaging, decoupling producers from any number of independent subscribers.

Messaging PatternsBeginner7 min readJul 10, 2026
Analogies

The Publish-Subscribe Pattern with Fanout Exchanges

Publish-subscribe in RabbitMQ is built on the fanout exchange type, which ignores routing keys entirely and copies every incoming message to all queues currently bound to it. A producer publishes once to the exchange, unaware of who — or how many — consumers exist; each subscriber declares its own queue, binds it to the exchange, and receives an independent copy of every message. This decouples producers from consumers completely: services can be added or removed without touching the publisher's code.

🏏

Cricket analogy: It is like a stadium's public address announcer calling out a wicket falling — every section of the ground, from the members' pavilion to the general stands, hears the exact same announcement simultaneously, regardless of how many sections exist.

Producers, Consumers, and Independent Bindings

Because a fanout exchange delivers to whichever queues happen to be bound at the moment, publish-subscribe systems are highly extensible: a new analytics service can bind a fresh queue to the same exchange tomorrow and start receiving events without any change to the publisher or existing consumers. Each subscriber typically uses its own exclusive, auto-delete queue if it only cares about live events, or a durable named queue if it needs to catch up on messages published while it was offline.

🏏

Cricket analogy: It is like a new regional TV network signing up to a match broadcast feed mid-tournament — they simply plug in and start receiving the live coverage without the host broadcaster changing anything about how it films the game.

python
import pika

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

channel.exchange_declare(exchange="order_events", exchange_type="fanout")

# Each subscriber gets its own private, temporary queue
result = channel.queue_declare(queue="", exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange="order_events", queue=queue_name)

def callback(ch, method, properties, body):
    print(f"Received event: {body}")

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

Fanout vs. Topic Exchanges for Selective Subscriptions

A plain fanout exchange broadcasts indiscriminately, which is perfect when every subscriber genuinely wants every message. When subscribers only care about a subset — say, only order.cancelled events rather than all order.* events — a topic exchange is the better fit, since it lets each queue bind with a pattern like order.cancelled.* while still supporting a full wildcard # for subscribers that want everything. Both remain publish-subscribe in spirit: the producer still publishes once, oblivious to the number and identity of subscribers.

🏏

Cricket analogy: It is like a cricket board offering a full match feed to every broadcaster (fanout) versus letting regional networks subscribe only to the wicket highlights package (topic), while the host broadcaster still films the match exactly once.

Fanout is the fastest exchange type because it skips routing-key evaluation entirely — it just copies to every bound queue. Reach for topic exchanges only when subscribers genuinely need selective filtering; otherwise fanout's simplicity and speed win.

  • Publish-subscribe in RabbitMQ is implemented with the fanout exchange type, which ignores routing keys and copies messages to every bound queue.
  • Producers publish once and remain completely decoupled from the number and identity of subscribers.
  • Each subscriber typically declares its own queue: exclusive/auto-delete for live-only interest, durable/named for catch-up after downtime.
  • New subscribers can bind at any time without requiring changes to the publisher or existing consumers.
  • Topic exchanges offer selective publish-subscribe via routing-key wildcard patterns (*, #) when subscribers only want a subset of messages.
  • Fanout is the fastest exchange type since it skips routing-key matching entirely.

Practice what you learned

Was this page helpful?

Topics covered

#Messaging#RabbitMQStudyNotes#Database#PublishSubscribePattern#Publish#Subscribe#Pattern#Fanout#StudyNotes#SkillVeris