Guide

AMQP 0.9.1 exchanges explained with a post office analogy


Written by: Lovisa Johansson


Understanding how AMQP 0.9.1 exchanges work can be much simpler when you think of them like a post office. That’s exactly what we’ll do in this blog.

Just as a post office desk decides where to send a letter based on its type and destination, an exchange determines how to route a message based on its type, routing keys, binding keys, and sometimes header attributes. The queues act as mailboxes at the final destination, ready to receive the messages.

There are several types of exchanges, each with its own unique routing logic: Direct, Fanout, Topic, and Header exchanges. Let’s explore each type and see how they fit into our post office analogy.

Let’s assume that when you enter the post office, you can choose between 4 different desks, each with a different way of handling letters.

Four desks at a post office

  1. Direct exchange — the direct desk

    Direct exchanges are delivering messages to specific queues based on exact matches. In the post office, you write the name of the person (routing key) to whom you want to send the letter on the envelope. You go to the direct desk (direct exchange), and the post office then sends the letter directly to the mailbox (queue) of the person on the envelope.

Routing key: Taylor Swift. Queue: Taylor Swift.

Routing key: Taylor Swift. Queue: Taylor Swift.

  1. Fanout exchange — the fanout desk

    Fanout exchanges distribute messages widely, sending them to all bound queues regardless of content. You give the letter to the fanout desk (fanout exchange), and the post office makes copies and sends them to every mailbox (queue) in the post office, regardless of what’s written on the envelope.

Topic exchange distribute messages to all queues bound to the exchange

Topic exchange distribute messages to all queues bound to the exchange

  1. Topic exchange — the topic desk

    Topic exchanges offer flexible routing by matching messages to queues based on dynamic criteria and patterns.

    You write a category of receivers on the letter. You then walk to the topic desk (topic exchange) and leave the letter. The person behind the desk looks at the topic (like a category) on the envelope. Based on that, it decides which mailboxes (queues) to send it to. Additionally, persons working behind the topic desks are smart – they can use wildcard matches to route messages based on patterns. For instance, if you have a topic like police.**, it will match any routing key that starts with “police.” followed by one word. Similarly, police.#* would match any routing key starting with police and any number of subsequent words, like “police.stockholm”, going to all police in Stockholm.

Letter with a category “police” will go to many, but not all mailboxes

Letter with a category “police” will go to many, but not all mailboxes

  1. Header exchange — the header desk

    Header exchanges route messages based on the attributes found in message headers.

    You put a specific sticker on your envelope. Then, you walk to the header desk (header exchange). The person behind the desk looks at the specific stamp (headers) on the letter (message). Depending on what it sees, it decides where to send the letter. It’s simply sorting mail based on special labels, such as the sticker on the envelope.

Envelopes are routed depending on the label of the envelope.

Envelopes are routed depending on the label of the envelope.

This analogy shows how AMQP 0.9.1 exchanges manage message flow, ensuring they reach the right destinations using different routing rules.

Thanks for reading! We hope this post has made AMQP 0.9.1 exchanges clearer and easier to understand.