Mastering Direct, Topic, and Fanout Exchanges in AMQP
The article explains how AMQP exchanges—Direct, Topic, and Fanout—route messages from producers to queues using routing and binding keys, detailing their routing rules, typical use cases, and step‑by‑step configuration examples with binding keys, routing keys, and message flow illustrations.
Exchange Overview
Producer sends messages to an Exchange, which routes them to one or more Queues based on Routing Key and Binding Key. Three exchange types are supported: Direct, Topic, and Fanout.
Direct Exchange
Routing rule : matches when Binding Key equals Routing Key.
Use case : differentiate messages sent to the same Exchange.
Example : Direct Exchange bound to Queue A with binding key img.create and to Queue B with img.log. Producer sends messages with corresponding routing keys, and they are delivered to the matching queues.
Bind Direct Exchange to queues:
Binding key for Queue A: img.create Binding key for Queue B: img.log Producer sends messages:
Message A routing key: img.create Message B routing key: img.log Exchange routes messages where Binding Key = Routing Key:
Message A goes to Queue A.
Message B goes to Queue B.
Topic Exchange
Routing rule : uses wildcard matching. * matches a single word, # matches zero or more words separated by dots.
Use case : differentiate messages by pattern.
Example : Queue A bound with files.cn.hz.#, Queue B with files.cn.*.store. Producer sends messages with routing keys files.cn.hz, files.cn.hz.store, files.cn.sz.store. Routing follows wildcard rules.
Bind Topic Exchange to queues:
Queue A binding key: files.cn.hz.# Queue B binding key: files.cn.*.store Producer sends messages:
Message A routing key: files.cn.hz Message B routing key: files.cn.hz.store Message C routing key: files.cn.sz.store Exchange routes:
Message A → Queue A.
Message B → Queue A and Queue B.
Message C → Queue B.
Fanout Exchange
Routing rule : ignores routing and binding keys; broadcasts messages to all bound queues.
Use case : broadcast scenarios.
Example : Queues A, B, C bound with any key; producer sends a message with any routing key; all queues receive the message.
Bind Fanout Exchange to queues A, B, C (binding keys arbitrary).
Producer sends a message (routing key arbitrary).
Exchange delivers the message to all bound queues.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
