What is a headers exchange in RabbitMQ?
Learn how a RabbitMQ headers exchange routes messages by matching header attributes with x-match all or any, ignoring the routing key entirely.
Expected Interview Answer
A headers exchange routes messages based on matching the message's header attributes instead of the routing key, using a special 'x-match' argument set to 'all' (match every header) or 'any' (match at least one header).
The routing key is ignored entirely. Each binding specifies a set of header key-value pairs plus 'x-match'. When a message is published, RabbitMQ compares its headers against each binding: with x-match=all every specified header must match, while with x-match=any a single match is enough. This enables routing on multiple non-hierarchical attributes such as content type, format, or region.
- Route on multiple attributes, not a single key
- Supports 'all' (AND) and 'any' (OR) matching
- Header values can be non-string types
- Good for complex, multi-criteria routing
- Decouples routing from a rigid key structure
AI Mentor Explanation
A headers exchange is like a selector picking players by a checklist of attributes rather than a jersey number: format=test, role=spinner, fitness=fit. With 'match all', a player must tick every box to be chosen; with 'match any', ticking one is enough. The delivery ignores names and decides purely on the tagged attributes, like headers on a message.
Step-by-Step Explanation
Step 1
Declare a headers exchange
Create an exchange of type 'headers' on the channel.
Step 2
Define binding headers
Bind a queue with a headers map plus an 'x-match' argument of 'all' or 'any'.
Step 3
Publish with headers
Producer sets message headers (e.g. format=pdf, region=eu) and any routing key, which is ignored.
Step 4
Broker compares headers
RabbitMQ matches the message headers against each binding using the x-match rule.
Step 5
Deliver on match
The message is routed to every queue whose header criteria are satisfied.
What Interviewer Expects
- Routing is on headers, not the routing key
- Meaning of x-match 'all' (AND) vs 'any' (OR)
- When headers routing beats topic routing
- Awareness of the x- prefixed reserved headers
- A concrete multi-attribute example
Common Mistakes
- Thinking the routing key still matters
- Confusing x-match 'all' with 'any'
- Assuming header values must be strings
- Using headers exchange where a topic exchange is simpler
Best Answer (HR Friendly)
“A headers exchange in RabbitMQ routes messages by matching their attribute tags rather than a routing key. You tell it to match all the specified attributes or just any one of them, which makes it great for routing on several criteria at once.”
Code Example
const amqp = require('amqplib');
async function run() {
const conn = await amqp.connect('amqp://localhost');
const ch = await conn.createChannel();
const exchange = 'reports';
await ch.assertExchange(exchange, 'headers', { durable: false });
const q = await ch.assertQueue('', { exclusive: true });
await ch.bindQueue(q.queue, exchange, '', {
'x-match': 'all',
format: 'pdf',
region: 'eu'
});
// Routing key ignored; headers drive routing
ch.publish(exchange, '', Buffer.from('quarterly report'), {
headers: { format: 'pdf', region: 'eu' }
});
console.log('Routed by matching headers');
await ch.close();
await conn.close();
}
run();Follow-up Questions
- What is the difference between x-match 'all' and 'any'?
- When would you choose a headers exchange over a topic exchange?
- Are header values limited to strings?
- Why is the headers exchange sometimes slower than topic routing?
- How does RabbitMQ ignore the routing key here?
MCQ Practice
1. What does a headers exchange use to route messages?
A headers exchange ignores the routing key and routes based on matching message header attributes via the x-match rule.
2. With x-match set to 'any', when does a binding match?
x-match=any is an OR condition: the binding matches if at least one specified header matches the message.
Flash Cards
What does a headers exchange route on? — Message header attributes, not the routing key.
x-match=all vs any? — 'all' requires every specified header to match (AND); 'any' requires at least one (OR).
Is the routing key used? — No — a headers exchange ignores the routing key entirely.