Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 37 ยท Patterns: RPC, Work Queues

RPC with Reply Queues

Implement request-reply (RPC) over RabbitMQ with reply_to and correlation_id, exclusive callback queues, Direct Reply-To, timeouts and the trade-offs, with a runnable simulation.

Messaging is asynchronous by nature: a producer sends and forgets. Sometimes, though, you need an answer: "what is the price of product 42?", "is this card valid?". Doing that over a message broker is called RPC (remote procedure call) or the request-reply pattern. The client sends a request message that says where to reply and which request this is; the server processes it and publishes the answer to that address.

RabbitMQ is not installed on this page, so the pika code is read-only. The runnable blocks are plain Python stand-ins that model the correlation of replies. They illustrate the protocol, not the broker.

The two message properties

AMQP 0-9-1 gives every message properties for exactly this purpose:

  • reply_to: the name of the queue where the server should send the response.
  • correlation_id: an opaque string chosen by the client; the server copies it into the reply so the client can match answer to question.
sequenceDiagram
    participant C as Client
    participant R as rpc_queue
    participant S as Server
    participant Y as Reply queue (exclusive)
    C->>R: request (reply_to=Y, correlation_id=abc)
    R->>S: deliver
    S->>S: compute
    S->>Y: reply (correlation_id=abc)
    Y->>C: deliver, match abc

A complete client and server

Server: listens on a well-known queue and replies to whatever reply_to says.

Python
import json
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
channel = connection.channel()
channel.queue_declare(queue="price_rpc")
channel.basic_qos(prefetch_count=1)

PRICES = {"p-1": 9.99, "p-2": 24.50}

def on_request(ch, method, props, body):
    sku = json.loads(body)["sku"]
    reply = {"sku": sku, "price": PRICES.get(sku)}
    ch.basic_publish(
        exchange="",
        routing_key=props.reply_to,
        properties=pika.BasicProperties(correlation_id=props.correlation_id),
        body=json.dumps(reply).encode(),
    )
    ch.basic_ack(delivery_tag=method.delivery_tag)

channel.basic_consume(queue="price_rpc", on_message_callback=on_request)
channel.start_consuming()

Client: creates a private reply queue, sends a request with a fresh correlation_id, then waits.

Python
import uuid

class PriceClient:
    def __init__(self):
        self.connection = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
        self.channel = self.connection.channel()
        result = self.channel.queue_declare(queue="", exclusive=True)  # server-named, private
        self.callback_queue = result.method.queue
        self.channel.basic_consume(queue=self.callback_queue,
                                   on_message_callback=self._on_response, auto_ack=True)
        self.response = None
        self.corr_id = None

    def _on_response(self, ch, method, props, body):
        if props.correlation_id == self.corr_id:
            self.response = json.loads(body)

    def call(self, sku, timeout=5.0):
        self.response, self.corr_id = None, str(uuid.uuid4())
        self.channel.basic_publish(
            exchange="", routing_key="price_rpc",
            properties=pika.BasicProperties(reply_to=self.callback_queue,
                                            correlation_id=self.corr_id),
            body=json.dumps({"sku": sku}).encode())
        self.connection.process_data_events(time_limit=timeout)
        if self.response is None:
            raise TimeoutError("no reply within %s s" % timeout)
        return self.response

Two things are worth noticing. The reply queue is exclusive (only this connection can use it) and server-named, so there are no name clashes between clients. And the callback ignores replies with an unknown correlation id instead of crashing: a late reply to an earlier timed-out request must not be mistaken for the current answer.

Matching many concurrent requests

A real client has many requests in flight at once. It keeps a table from correlation_id to a future (or a callback), and the reply handler looks the id up.

Python Stand-in: correlation ids with out-of-order replies

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Direct Reply-To

Declaring a queue for every client has a cost. RabbitMQ offers a shortcut called Direct Reply-To: the client consumes from the pseudo-queue amq.rabbitmq.reply-to (with auto_ack=True, on the same channel it publishes from) and sets reply_to to the name that the broker generates for it. Replies sent to that name are routed straight back to the client without a real queue. It is cheaper and simpler, but replies are not persisted and are lost if the client's channel closes.

Timeouts, retries and failure

RPC over a queue brings failure modes that a function call does not have:

  • Server down: the request waits in the queue and the client waits too. Always set a timeout, and give the request an expiration (message TTL) that matches it, so the server does not answer a request nobody is waiting for any more.
  • Lost reply: if the client retries, the server may run the operation twice. Mark non-idempotent requests with a unique id and let the server deduplicate.
  • Slow server: with many clients, requests queue up. Scale servers like workers, see Work Queues and Fair Dispatch.
  • Poison requests: a request that crashes the server must not loop forever; use a dead letter exchange (Dead Letter Exchanges and Retries) and reply with an error message.
Python Stand-in: timeout and expired requests

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last request is dropped before any work is done, which is the whole point of pairing a TTL with a client timeout.

When not to use RPC over RabbitMQ

RPC couples the caller to the answer's timing, which removes much of the decoupling that makes messaging attractive. For a user-facing request that needs an immediate answer, a plain HTTP or gRPC call is usually simpler and has better tooling (status codes, tracing, deadlines). Prefer RabbitMQ RPC when you already depend on the broker, need to hide service locations or want queue-based load levelling and retries. Where possible, switch to asynchronous request/event flows: send a command, return an id immediately, and let the client receive the result later through an event (see Publish/Subscribe and Event Fanout).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which two message properties implement the RPC pattern?

    reply_to names where the answer goes; correlation_id lets the client match the reply to its request.
  2. 2. Why does the client ignore a reply whose correlation_id it does not recognize?

    Late or stray replies are normal; processing them as the current answer would return wrong data.
  3. 3. What is a benefit of Direct Reply-To?

    The client consumes from the pseudo-queue amq.rabbitmq.reply-to, so no real queue is created. Correlation ids are still needed.
  4. 4. A request has no TTL and the server was down for an hour. What risk exists when it restarts?

    Without a TTL the stale request is processed after the client gave up. Set message expiration to the client's timeout.

Summary

  • RPC over RabbitMQ uses a request queue, reply_to and correlation_id.
  • A private exclusive reply queue (or Direct Reply-To) receives replies; the client keeps a table of pending ids.
  • Always use timeouts and message TTLs, and make servers tolerate duplicate requests.
  • Prefer plain HTTP/gRPC for simple synchronous calls; prefer events when you can avoid waiting.

Next: Publish/Subscribe and Event Fanout