← Back to the chapter · Previous detail
Technical detail · AMQP Reliability: Delivery Controls
Acceptance, fallback routing and queue failure controls
These are separate configuration fragments, not a complete deployment. Declare unrouted-orders and order-failures and bind their destination queues before relying on them. Confirm handling checks broker acceptance; mandatory-return handling detects an unroutable publication. Queue TTL and length limits operate after routing. Keep a record of failures rather than treating retries as proof of recovery.
# BAD: Fire and forget
channel.basic_publish(
exchange='orders',
routing_key='orders.new',
body=order_json
)
# Returns immediately, no guarantee message was routed!
# GOOD: Wait for broker confirmation
channel.confirm_delivery() # Enable publisher confirms
try:
channel.basic_publish(
exchange='orders',
routing_key='orders.new',
body=order_json,
mandatory=True # Return message if not routed
)
# Confirm plus mandatory-return handling checks this publish
except pika.exceptions.UnroutableError:
# Message couldn't be routed - handle error!
logger.error(f"Order {order_id} could not be routed!")
# Retry, dead-letter, alert ops team
# Configure exchange with fallback for unroutable messages
channel.exchange_declare(
exchange='orders',
exchange_type='topic',
arguments={
'alternate-exchange': 'unrouted-orders' # Safety net
}
)
# Unroutable messages go to alternate exchange
# Bind to alert queue for investigation
channel.queue_bind(
queue='unrouted-alerts',
exchange='unrouted-orders'
)
# Queue with dead-letter routing for failed messages
channel.queue_declare(
queue='order-processing',
arguments={
'x-dead-letter-exchange': 'order-failures',
'x-message-ttl': 300000, # 5 min timeout
'x-max-length': 10000 # Prevent overflow
}
)