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]
See also  Understanding RabbitMQ Virtual Hosts (vhosts) and Their Uses
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

  • 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
  • Producers and Consumers: How Data Flows in Kafka
  • Kafka Architecture Explained: Brokers, Topics, Partitions, and Offsets
  • Getting Started with Apache Kafka: Core Concepts and Use Cases
  • Security Best Practices for RabbitMQ in Production
  • Understanding RabbitMQ Virtual Hosts (vhosts) and Their Uses
  • RabbitMQ Performance Tuning: Optimizing Throughput and Latency
  • High Availability in RabbitMQ: Clustering and Mirrored Queues Explained

Recent Comments

  1. Playing with VPC AWS (Part 2) – Widhian's Blog on Playing with VPC AWS (Part 1): VPC, Subnet, Internet Gateway, Route Table, NAT, and Security Group
  2. Basic Concept of ElasticSearch (Part 3): Translog, Flush, and Refresh – Widhian's Blog on Basic Concept of ElasticSearch (Part 1): Introduction
  3. Basic Concept of ElasticSearch (Part 2): Architectural Perspective – Widhian's Blog on Basic Concept of ElasticSearch (Part 3): Translog, Flush, and Refresh
  4. Basic Concept of ElasticSearch (Part 3): Translog, Flush, and Refresh – Widhian's Blog on Basic Concept of ElasticSearch (Part 2): Architectural Perspective
  5. Basic Concept of ElasticSearch (Part 1): Introduction – Widhian's Blog on Basic Concept of ElasticSearch (Part 2): Architectural Perspective

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
  • Programming
  • RabbitMQ
  • Redis
  • VPC
© 2025 Widhian Bramantya | Powered by Minimalist Blog WordPress Theme