AMQP exchanges determine how messages are routed to queues. Different exchange types allow for different messaging patterns.

Exchange Types in AMQP

Exchange TypeDescriptionUse Case
DirectRoutes messages to a queue based on an exact routing key matchTask distribution
FanoutBroadcasts messages to all bound queuesEvent notifications, logging
TopicRoutes messages based on pattern matching (wildcards)Selective event distribution
HeadersRoutes messages based on message headersAdvanced filtering

Example: Direct Exchange Routing in RabbitMQ using Python

import pika
 
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
 
channel.exchange_declare(exchange='direct_logs', exchange_type='direct')
 
channel.basic_publish(exchange='direct_logs', routing_key='error', body='Error log message')
print(" [x] Sent 'Error log message'")
 
connection.close()