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

> Explore Kourier exchange types: direct, fanout, topic, and headers. Learn how to implement these standard RabbitMQ exchanges with type-safe constants and the exchangeDeclare method.

- Repository: [Guimauve Digital/kourier](https://github.com/guimauvedigital/kourier)
- Tags: deep-dive
- Published: 2026-03-02

---

**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`](https://github.com/guimauvedigital/kourier/blob/main/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.

```kotlin
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`](https://github.com/guimauvedigital/kourier/blob/main/docs/tutorials/routing.md), the implementation demonstrates declaring a direct exchange for log severity filtering:

```kotlin
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`](https://github.com/guimauvedigital/kourier/blob/main/docs/tutorials/publish-subscribe.md), declare a fanout exchange using:

```kotlin
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`](https://github.com/guimauvedigital/kourier/blob/main/docs/tutorials/topics.md) implements this pattern:

```kotlin
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:

```kotlin
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`](https://github.com/guimauvedigital/kourier/blob/main/BuiltinExchangeType.kt) as `DIRECT`, `FANOUT`, `TOPIC`, and `HEADERS`.
- **Direct** exchanges route by exact routing key matches, used in [`routing.md`](https://github.com/guimauvedigital/kourier/blob/main/routing.md).
- **Fanout** exchanges broadcast to all queues, demonstrated in [`publish-subscribe.md`](https://github.com/guimauvedigital/kourier/blob/main/publish-subscribe.md).
- **Topic** exchanges support wildcard pattern matching, shown in [`topics.md`](https://github.com/guimauvedigital/kourier/blob/main/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`](https://github.com/guimauvedigital/kourier/blob/main/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`](https://github.com/guimauvedigital/kourier/blob/main/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`](https://github.com/guimauvedigital/kourier/blob/main/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`](https://github.com/guimauvedigital/kourier/blob/main/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.