Kourier Exchange Types: Direct, Fanout, Topic, and Headers Implementation

Kourier supports the four standard RabbitMQ exchange types—direct, fanout, topic, and headers—implemented as type-safe constants in the BuiltinExchangeType object and utilized via the exchangeDeclare method.

Kourier is a Kotlin multiplatform AMQP client library that provides native support for RabbitMQ messaging patterns. The library defines all standard exchange types in a single location, making it straightforward to declare exchanges for precise message routing. Understanding these Kourier exchange types is essential for implementing efficient publish-subscribe and routing architectures.

Core Exchange Type Definitions

All four exchange type constants are centrally defined in amqp-core/src/commonMain/kotlin/dev/kourier/amqp/BuiltinExchangeType.kt. This object provides string constants that map directly to RabbitMQ's built-in exchange mechanisms.

object BuiltinExchangeType {
    const val DIRECT = "direct"
    const val FANOUT = "fanout"
    const val TOPIC = "topic"
    const val HEADERS = "headers"
}

These constants are passed to the exchangeDeclare method available on channel objects throughout the Kourier API.

Direct Exchanges (Exact Match Routing)

The direct exchange routes messages to queues where the binding key exactly matches the message's routing key. This pattern is ideal for filtering messages by specific criteria such as severity levels.

In docs/tutorials/routing.md, the implementation demonstrates declaring a direct exchange for log severity filtering:

channel.exchangeDeclare(
    "direct_logs",
    BuiltinExchangeType.DIRECT,
    durable = false,
    autoDelete = false,
    internal = false,
    arguments = emptyMap()
)

When publishing, messages include a routing key like "info" or "error", and only queues bound with matching keys receive the message.

Fanout Exchanges (Broadcast Pattern)

The fanout exchange broadcasts every message to all bound queues, ignoring routing keys entirely. This implements the publish-subscribe pattern where all consumers receive identical message copies.

As shown in docs/tutorials/publish-subscribe.md, declare a fanout exchange using:

channel.exchangeDeclare(
    "logs",
    BuiltinExchangeType.FANOUT,
    durable = false,
    autoDelete = false,
    internal = false,
    arguments = emptyMap()
)

This exchange type is optimal for event broadcasting scenarios where every subscriber must process every message.

Topic Exchanges (Pattern Matching)

The topic exchange enables flexible routing using dot-separated hierarchical routing keys with wildcard patterns. The * character substitutes exactly one word, while # matches zero or more words.

The tutorial in docs/tutorials/topics.md implements this pattern:

channel.exchangeDeclare(
    "topic_logs",
    BuiltinExchangeType.TOPIC,
    durable = false,
    autoDelete = false,
    internal = false,
    arguments = emptyMap()
)

Use topic exchanges when routing messages based on hierarchical categorizations, such as "stock.nyse" or "weather.us.ca".

Headers Exchanges (Attribute-Based Routing)

The headers exchange routes messages based on header attribute matching rather than routing keys. This allows complex routing decisions using multiple header criteria and the x-match argument (all or any).

While not featured in the primary tutorials, the constant is available in the same API:

channel.exchangeDeclare(
    "header_ex",
    BuiltinExchangeType.HEADERS,
    durable = false,
    autoDelete = false,
    internal = false,
    arguments = emptyMap()
)

Headers exchanges suit scenarios requiring routing based on multiple message attributes rather than simple string patterns.

Summary

  • Kourier implements four standard RabbitMQ exchange types: direct, fanout, topic, and headers.
  • Constants are defined in BuiltinExchangeType.kt as DIRECT, FANOUT, TOPIC, and HEADERS.
  • Direct exchanges route by exact routing key matches, used in routing.md.
  • Fanout exchanges broadcast to all queues, demonstrated in publish-subscribe.md.
  • Topic exchanges support wildcard pattern matching, shown in topics.md.
  • Headers exchanges enable routing via message headers for advanced filtering scenarios.

Frequently Asked Questions

What exchange type should I use for simple message broadcasting?

Use the fanout exchange type when you need to send every message to all connected queues without filtering. According to the Kourier source code in docs/tutorials/publish-subscribe.md, this pattern ignores routing keys and distributes messages equally to all bound consumers.

How does Kourier handle routing key patterns in topic exchanges?

Kourier's topic exchange implementation supports standard RabbitMQ wildcard syntax where * matches exactly one word and # matches zero or more words in a dot-separated routing key. This is implemented in the tutorials using BuiltinExchangeType.TOPIC as defined in BuiltinExchangeType.kt.

Can I use headers-based routing with Kourier?

Yes, Kourier supports headers exchanges via BuiltinExchangeType.HEADERS defined in amqp-core/src/commonMain/kotlin/dev/kourier/amqp/BuiltinExchangeType.kt. This exchange type routes messages based on header key-value matching rather than routing keys, enabling complex multi-attribute filtering.

Where are the exchange type constants defined in the Kourier repository?

All exchange type constants are centrally located in amqp-core/src/commonMain/kotlin/dev/kourier/amqp/BuiltinExchangeType.kt. This file contains the BuiltinExchangeType object with string constants for DIRECT, FANOUT, TOPIC, and HEADERS that are used throughout the library's tutorial implementations and API.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →