← Back to the chapter · Next detail →
Technical detail · AMQP Reliability: Delivery Controls
Warehouse queue and consumer configuration
These fragments retain the warehouse’s exchange, queue and routing names. Declare the DLX and destination before relying on their route. The consumer’s RobotError path requests requeue; it does not count retries or automatically dead-letter repeated failures. Durable topology also needs persistent sends and acceptance evidence. pick_items must preserve the order’s repeat-safe processing contract.
# Single work queue with competing consumers
# Direct exchange routes orders to one queue
# Multiple robots consume from the same queue
channel.exchange_declare(
exchange='orders',
exchange_type='direct',
durable=True # Survive broker restart
)
channel.queue_declare(
queue='picking-queue',
durable=True,
arguments={
'x-max-length': 50000, # Buffer for 5 hours peak
'x-message-ttl': 3600000, # 1 hour max wait
'x-dead-letter-exchange': 'orders-dlx',
'x-dead-letter-routing-key': 'failed'
}
)
channel.queue_bind(
queue='picking-queue',
exchange='orders',
routing_key='new-order'
)
# Each robot gets 1 order at a time
# Bounds outstanding deliveries for every robot
channel.basic_qos(prefetch_count=1)
def robot_callback(ch, method, properties, body):
order = json.loads(body)
try:
# Process the order (30-120 seconds)
result = pick_items(order)
# Only acknowledge after successful completion
ch.basic_ack(delivery_tag=method.delivery_tag)
log.info(f"Order {order['id']} completed")
except RobotError as e:
# Reject and requeue for another robot
ch.basic_nack(delivery_tag=method.delivery_tag, requeue=True)
log.warning(f"Order {order['id']} requeued: {e}")
channel.basic_consume(
queue='picking-queue',
on_message_callback=robot_callback,
auto_ack=False # Manual acknowledgment required
)
# Dead-letter destination; retry limits require separate logic
channel.exchange_declare(exchange='orders-dlx', exchange_type='direct', durable=True)
channel.queue_declare(queue='failed-orders', durable=True)
channel.queue_bind(
queue='failed-orders',
exchange='orders-dlx',
routing_key='failed'
)
# Alert system monitors failed-orders queue
# Human operator reviews and resubmits or cancels