Skip to content

Widhian Bramantya

coding is an art form

Menu
  • About Me
Menu
rabbitmq

Understanding RabbitMQ Exchange Types with Go: Default, Direct, Fanout, and Topic

Posted on September 8, 2025September 8, 2025 by admin

3. Fanout Exchange

Concept

  • Sends every message to all queues bound to the exchange.
  • Ignores routing keys.
  • Good for broadcast scenarios (notifications, pub/sub).

Code Example

fanout/publisher/main.go

ch.ExchangeDeclare("logs_fanout", "fanout", true, false, false, false, nil)
ch.Publish("logs_fanout", "", false, false, amqp.Publishing{
    ContentType: "text/plain",
    Body:        []byte("Broadcast log"),
})

fanout/consumer_1/main.go

q, _ := ch.QueueDeclare("", false, false, true, false, nil) // auto-generated queue
ch.QueueBind(q.Name, "", "logs_fanout", false, nil)

msgs, _ := ch.Consume(q.Name, "", true, false, false, false, nil)
for d := range msgs {
    log.Printf("[fanout consumer] %s", d.Body)
}

Diagram

flowchart LR
  P[Producer] --> F((Fanout Exchange: logs_fanout))
  F --> Q1[Queue: c1] --> C1[Consumer1]
  F --> Q2[Queue: c2] --> C2[Consumer2]
  F --> Q3[Queue: c3] --> C3[Consumer3]

4. Topic Exchange

Concept

  • Routes messages based on pattern-matching in the routing key.
  • Supports wildcards:
    • * matches one word.
    • # matches zero or more words.
  • Good for event-driven systems where events have hierarchical names.

Code Example

topic/publisher/main.go

ch.ExchangeDeclare("logs_topic", "topic", true, false, false, false, nil)
ch.Publish("logs_topic", "order.us.created", false, false, amqp.Publishing{
    ContentType: "text/plain",
    Body:        []byte("Order created in US"),
})

topic/consumer_1/main.go

q, _ := ch.QueueDeclare("us_orders", true, false, false, false, nil)
ch.QueueBind(q.Name, "order.us.*", "logs_topic", false, nil)

msgs, _ := ch.Consume(q.Name, "", true, false, false, false, nil)
for d := range msgs {
    log.Printf("[us_orders] %s", d.Body)
}

Diagram

flowchart LR
  P[Producer] -->|order.us.created| T((Topic Exchange: logs_topic))
  T -->|bind=order.us.*| Q1[Queue: us_orders] --> C1[Consumer1]
  T -->|bind=order.#| Q2[Queue: all_orders] --> C2[Consumer2]

Related posts:

High Availability in RabbitMQ: Clustering and Mirrored Queues Explained

RabbitMQ Performance Tuning: Optimizing Throughput and Latency

Dead Letter Queues in RabbitMQ: How to Handle Failed Messages

See also  Implementing RPC (Remote Procedure Call) with RabbitMQ
Pages: 1 2 3
Category: RabbitMQ

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Linkedin

Widhian Bramantya

Recent Posts

  • Smart Automation in PostgreSQL: Managing Time-Based Data with pg_partman and pg_cron
  • Understanding PostgreSQL WAL, Slot, Publication, LSN, and Replication Lag
  • PostgreSQL Write-Ahead Log (WAL): Durability, Performance Tuning, and Recovery Explained
  • PostgreSQL Replication Deep Dive: From High Availability to Multi-Master Clusters
  • Finding Nearby Merchants in a Ride-Hailing App Using Elasticsearch Polygon Search
  • Advanced Text Search in Elasticsearch: N-Gram, Reverse, Fuzzy, and Search-as-you-type
  • Understanding and Customizing Analyzers in Elasticsearch
  • Log Management at Scale: Integrating Elasticsearch with Beats, Logstash, and Kibana
  • Index Lifecycle Management (ILM) in Elasticsearch: Automatic Data Control Made Simple
  • Blue-Green Deployment in Elasticsearch: Safe Reindexing and Zero-Downtime Upgrades
  • Maintaining Super Large Datasets in Elasticsearch
  • Elasticsearch Best Practices for Beginners
  • Implementing the Outbox Pattern with Debezium
  • Production-Grade Debezium Connector with Kafka (Postgres Outbox Example – E-Commerce Orders)
  • Connecting Debezium with Kafka for Real-Time Streaming
  • Debezium Architecture – How It Works and Core Components
  • What is Debezium? – An Introduction to Change Data Capture
  • Offset Management and Consumer Groups in Kafka
  • Partitions, Replication, and Fault Tolerance in Kafka
  • Delivery Semantics in Kafka: At Most Once, At Least Once, Exactly Once

Recent Comments

No comments to show.

Archives

  • October 2025
  • September 2025
  • August 2025
  • November 2021
  • October 2021
  • August 2021
  • July 2021
  • June 2021
  • March 2021
  • January 2021

Categories

  • Debezium
  • Devops
  • ElasticSearch
  • Golang
  • Kafka
  • Lua
  • NATS
  • PostgreSQL
  • Programming
  • RabbitMQ
  • Redis
  • VPC
© 2026 Widhian Bramantya | Powered by Minimalist Blog WordPress Theme