RabbitMQ Routing Keys: Beginner's Guide

Shivam DubeyShivam Dubey
3 min read

In RabbitMQ, Routing Keys play a significant role in message delivery. They are used when working with Direct and Topic exchanges to route messages to the correct queues.

This article will cover:

  • What are Routing Keys?

  • How they work in RabbitMQ.

  • Different exchange types that use Routing Keys.

  • Examples and scenarios for better understanding.

  • Visual representation using a flowchart.


πŸ§‘β€πŸ’» What is a Routing Key?

A Routing Key is a string identifier attached to a message by the producer. It acts as a label or address that RabbitMQ uses to route messages to the appropriate queue.

  • It is used only in Direct and Topic exchanges.

  • It helps RabbitMQ determine which queues should receive the message.

  • It is matched against the binding key (a key used when a queue is bound to an exchange).


🟒 How Does a Routing Key Work?

The concept of routing keys can be explained in three steps:

  1. Producer sends a message to the exchange with a specific routing key.

  2. Exchange evaluates the routing key and compares it to the binding keys of the queues.

  3. Queues that match the routing key receive the message.


πŸ“Š Flowchart: Routing Key in Action

Here’s how routing keys work in RabbitMQ using a Direct Exchange.

                +-------------------+            
                |      Producer     |            
                +-------------------+            
                          |                         
                          |                         
                 +--------------------+            
                 |   Direct Exchange  |           
                 +--------------------+            
                       /      |       \            
                ______/       |        \______     
               /              |              \     
       +---------+      +---------+      +---------+
       | Queue 1 |      | Queue 2 |      | Queue 3 |
       +---------+      +---------+      +---------+
      (error)         (info)         (warning)
  • Queue 1 is bound with the binding key error.

  • Queue 2 is bound with the binding key info.

  • Queue 3 is bound with the binding key warning.

  • A producer sends a message with the routing key error, so Queue 1 will receive the message.


🧱 Types of Exchanges That Use Routing Keys

1. Direct Exchange

  • Routes messages based on an exact match between the routing key and the binding key.

  • Ideal for logging systems where logs are separated by severity (e.g., error, info, warning).

Example:

  • Routing Key: error

  • Binding Key: error

  • Message goes to the queue bound with the error key.

2. Topic Exchange

  • Routes messages based on a pattern match using routing keys and wildcards.

  • Supports two special wildcards:

    • * (matches exactly one word)

    • # (matches zero or more words)

Example:

  • Routing Key: user.error

  • Binding Key: user.* β†’ Match (because * matches one word)

  • Binding Key: user.# β†’ Match (because # matches multiple words)


πŸ›  Example in Action

Let’s assume we have the following setup:

  • Exchange Type: Direct Exchange

  • Queues: Queue_Error, Queue_Info, Queue_Warning

  • Binding Keys: error, info, warning

Producer Sends Messages:

  • Message 1: Routing Key β†’ error

  • Message 2: Routing Key β†’ info

  • Message 3: Routing Key β†’ warning

Result:

  • Queue_Error receives Message 1.

  • Queue_Info receives Message 2.

  • Queue_Warning receives Message 3.


βœ… Key Takeaways

  • Routing Keys are only used in Direct and Topic exchanges.

  • They act as a filter for delivering messages to the correct queues.

  • Direct Exchanges use exact match routing keys, while Topic Exchanges use pattern matching.

  • Proper use of routing keys can improve message management and reduce unnecessary message processing.


0
Subscribe to my newsletter

Read articles from Shivam Dubey directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shivam Dubey
Shivam Dubey