← All topics
📬 Messaging & NServiceBus

RabbitMQ Fundamentals

Exchanges, queues, bindings, routing.

Queue — where messages actually sit waiting to be consumed. Exchange — receives published messages and routes them to zero or more queues based on rules; producers publish to an exchange, not directly to a queue (this indirection is exactly what enables pub/sub-style fan-out to multiple consumers, same underlying idea as NServiceBus's Publish()).

Exchange types: - Direct: routes by exact routing-key match. - Fanout: routes to every bound queue, ignoring the routing key entirely — classic broadcast/event publish. - Topic: routes by wildcard pattern match on the routing key (order..created) — flexible subscription filtering. - Headers*: routes by matching message header values instead of the routing key.

Binding — the rule connecting an exchange to a queue (with a routing key/pattern for direct/topic exchanges).

NServiceBus's RabbitMQ transport builds its Send/Publish abstraction on top of exactly these primitives — a Command typically maps to a direct-routed queue (one destination), an Event typically maps to a fanout/topic exchange (potentially many subscriber queues).

Flashcards (3)

Why do RabbitMQ producers publish to an exchange rather than directly to a queue?
tap to reveal answer
The exchange is what handles routing (to one, many, or zero queues) based on rules/bindings — this indirection is what enables one published message to reach multiple independent consumer queues (fan-out/pub-sub) without the producer knowing about any of them.
Name the four RabbitMQ exchange types and what each routes on.
tap to reveal answer
Direct: exact routing-key match. Fanout: ignores routing key, delivers to every bound queue. Topic: wildcard pattern match on routing key (e.g. order.*.created). Headers: matches on message header values instead of routing key.
How does the NServiceBus Command/Event distinction map onto RabbitMQ exchange types conceptually?
tap to reveal answer
A Command (one specific handler) maps naturally to direct routing to a single queue. An Event (zero-to-many subscribers) maps naturally to a fanout or topic exchange fanning out to each subscriber's own queue.