RabbitMQ
Routing Keys
Message Queuing
Software Development
Server Configuration

RabbitMQ: bind multiple routing keys to a single queue

Codemia Knowledge Hub ·

Yes, one queue can have many bindings. Bind it to the same exchange once per routing key, and the exchange delivers any message whose routing key matches one of those bindings. This is mainly useful with direct and topic exchanges.

Exchanges, queues and bindings

  • Exchanges receive messages from producers and push them to queues according to routing rules.
  • Queues store messages until a consumer handles them.
  • Bindings are the rules an exchange uses to route messages to queues.

Exchange types

  1. Direct exchange: routes by the message's routing key.
  2. Topic exchange: routes on multiple criteria taken from the routing key.
  3. Fanout exchange: sends to every bound queue, ignoring the routing key.
  4. Headers exchange: routes on header attributes.
  5. Default (nameless) exchange: routes directly to the queue named in the routing key.

How multiple bindings route messages

Each binding key is a separate route by which the exchange can deliver to the queue. When a message arrives, the exchange checks which queues have a binding that matches its routing key and routes it there. A queue with several bindings can therefore collect messages from multiple producers or on varied criteria.

Example: orders and stock updates in one queue

A queue needs both "new orders" from customers and "stock updates" from inventory:

  1. Exchange: create a topic exchange, topic_logs.
  2. Queue: create a queue, critical_updates.
  3. Bindings: bind critical_updates with two routing keys:
    • order.*
    • stock.*

Messages published to topic_logs with routing key order.new or stock.update both land in critical_updates.

bash
1# Create exchange
2rabbitmqadmin declare exchange name=topic_logs type=topic
3
4# Create queue
5rabbitmqadmin declare queue name=critical_updates durable=true
6
7# Bind queue to exchange with multiple routing keys
8rabbitmqadmin declare binding source=topic_logs destination=critical_updates routing_key=order.*
9rabbitmqadmin declare binding source=topic_logs destination=critical_updates routing_key=stock.*

These use rabbitmqadmin, the CLI that ships with the management plugin; rabbitmqctl has no commands for declaring exchanges, queues or bindings. You can also declare bindings from client code.

Benefits

BenefitDescription
FlexibilityCan receive different types of messages in one queue based on varied routing keys.
Simplified ArchitectureReduces the number of queues and exchanges needed, simplifying the architecture.
Enhanced Message FilteringProvides a granular level of control over which messages are routed to particular queues.
Improved System ScalabilityHelps in scaling the system by segregating critical routes efficiently without overloading a queue.

Costs to watch

  • Performance: more bindings make routing more complex, which can affect performance.
  • Maintenance: many bindings become hard to manage, especially when routing requirements change often.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design