0
0
RabbitMQdevops~10 mins

Topic exchange (pattern matching) in RabbitMQ - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Topic exchange (pattern matching)
Producer sends message with routing key
Topic Exchange receives message
Exchange compares routing key with binding keys
Match?
Route to queues
Consumers receive messages from queues
Messages are sent with routing keys to a topic exchange, which routes them to queues whose binding keys match the routing key pattern.
Execution Sample
RabbitMQ
1. Producer sends message with routing key: "stock.usd.nyse"
2. Topic exchange has bindings:
   - Queue A: binding key "stock.*.*"
   - Queue B: binding key "stock.usd.#"
3. Exchange routes message based on pattern match
This example shows how a message with routing key "stock.usd.nyse" is routed to queues with matching binding keys using topic exchange pattern matching.
Process Table
StepRouting KeyBinding KeyMatch ResultActionQueues Routed
1stock.usd.nysestock.*.*YesRoute messageQueue A
2stock.usd.nysestock.usd.#YesRoute messageQueue B
3stock.usd.nysestock.eur.#NoDiscard for this binding-
4stock.usd.nysestock.#YesRoute messageQueue C (if exists)
5stock.usd.nyseother.bindingNoDiscard for this binding-
6---Routing completeQueues A, B, (C if exists)
💡 All bindings checked; message routed to all matching queues.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Routing Key-stock.usd.nysestock.usd.nysestock.usd.nysestock.usd.nyse
Binding Key-stock.*.*stock.usd.#stock.eur.#-
Match Result-YesYesNo-
Queues Routed-Queue AQueue A, Queue BQueue A, Queue BQueue A, Queue B
Key Moments - 3 Insights
Why does the binding key 'stock.*.*' match 'stock.usd.nyse'?
Because '*' matches exactly one word in the routing key, so 'stock.*.*' matches 'stock.usd.nyse' where 'usd' and 'nyse' are the two words matched by '*'. See execution_table row 1.
Why does 'stock.usd.#' match the routing key?
'#' matches zero or more words, so 'stock.usd.#' matches any routing key starting with 'stock.usd', including 'stock.usd.nyse'. See execution_table row 2.
What happens if no binding keys match the routing key?
The message is discarded or dead-lettered because no queues are bound with matching patterns. See execution_table row 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which binding key does NOT match the routing key 'stock.usd.nyse'?
Astock.eur.#
Bstock.usd.#
Cstock.*.*
Dstock.#
💡 Hint
Check the 'Match Result' column in execution_table rows 1-5.
At which step does the message get routed to Queue B?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Queues Routed' column in execution_table for when Queue B appears.
If the routing key was 'stock.eur.london', which binding key would match?
Astock.usd.#
Bstock.eur.#
Cstock.*.*
Dother.binding
💡 Hint
Refer to how '#' matches zero or more words in binding keys from key_moments.
Concept Snapshot
Topic Exchange routes messages by matching routing keys to binding keys using patterns.
'*' matches exactly one word.
'#' matches zero or more words.
Messages route to all queues with matching binding keys.
Non-matching messages are discarded.
Full Transcript
In RabbitMQ, a topic exchange routes messages based on pattern matching between the message's routing key and the binding keys of queues. The routing key is a string with words separated by dots. Binding keys can include '*' to match exactly one word and '#' to match zero or more words. When a message arrives, the exchange compares the routing key to each binding key. If they match, the message is routed to the corresponding queue. If no binding keys match, the message is discarded. For example, a routing key 'stock.usd.nyse' matches binding keys like 'stock.*.*' and 'stock.usd.#'. This allows flexible routing based on patterns.