Skip to main content

RPC & reply queues

The AMQP RPC pattern works out of the box. A client declares an exclusive (or auto-delete) reply queue, publishes a request carrying reply-to and correlation-id, and the server publishes the reply back to that queue.

Exclusive / auto-delete queues are backed by an in-memory ring scoped to the declaring connection — not a durable topic. So an RPC reply queue per call costs no schema churn and vanishes automatically when the connection closes. They are auto-ack and have no durable cursor (they are transient by nature).

# client
reply = ch.queue_declare(queue="", exclusive=True).method.queue
ch.basic_consume(reply, on_reply, auto_ack=True)
ch.basic_publish(exchange="", routing_key="rpc_queue",
properties=pika.BasicProperties(reply_to=reply,
correlation_id=cid),
body=request)
# server
def on_request(ch, method, props, body):
ch.basic_publish(exchange="", routing_key=props.reply_to,
properties=pika.BasicProperties(correlation_id=props.correlation_id),
body=response)
ch.basic_ack(method.delivery_tag)

The reply queue is visible server-wide for routing (so the server's publish reaches it) but may only be consumed by its declaring connection; a foreign consume is refused with RESOURCE_LOCKED.