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

RabbitMQ is one of the most popular message brokers for building distributed systems. It allows applications to communicate asynchronously by sending and receiving messages through queues. At the heart of RabbitMQ is the concept of an exchange, which decides how messages flow from producers (publishers) to consumers.

This article explains RabbitMQ’s most common exchange types (Default, Direct, Fanout, and Topic) using real Go examples from the repository rabbitmq-example.

1. Default Exchange

Concept

  • The default exchange is a direct exchange with an empty name ("").
  • Messages are routed directly to the queue with the same name as the routing key.

Code Example

default/publisher/main.go

// publish directly to queue "hello"
q, _ := ch.QueueDeclare("hello", false, false, false, false, nil)
ch.Publish("", q.Name, false, false, amqp.Publishing{
    ContentType: "text/plain",
    Body:        []byte("Hello World"),
})

default/consumer_1/main.go

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

Diagram

flowchart LR
  P[Producer] -->|routing key=hello| X((Default Exchange ""))
  X --> Q[Queue: hello] --> C[Consumer]

2. Direct Exchange

Concept

  • Routes messages to queues where the binding key matches exactly the routing key.
  • Useful for logs or task routing by category.

Code Example

direct/publisher/main.go

ch.ExchangeDeclare("logs_direct", "direct", true, false, false, false, nil)
ch.Publish("logs_direct", "error", false, false, amqp.Publishing{
    ContentType: "text/plain",
    Body:        []byte("Error message"),
})

direct/consumer_1/main.go

q, _ := ch.QueueDeclare("error_logs", true, false, false, false, nil)
ch.QueueBind(q.Name, "error", "logs_direct", false, nil)

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

Diagram

flowchart LR
  P[Producer] -->|routing key=error| X((Direct Exchange: logs_direct))
  X -->|bind=error| Qe[Queue: error_logs] --> Ce[Consumer]
See also  Producers and Consumers: How Data Flows in Kafka
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