Skip to content
System Design Lab
Microservice Patterns

Request/Reply over Messaging

Get an answer back through a message broker: send a request with a reply address and a correlation id, match replies as they arrive, and decide what a missing reply means.

Intermediate 4 min read· Updated 2026-11-18

What you will learn

  • Implement request/reply with replyTo addresses and correlation ids.
  • Handle timeouts and late replies safely.
  • Avoid shared reply queues between requester instances.
  • Decide when request/reply over messaging is better than plain HTTP.

Before you start

Overview

When you post a letter that needs an answer, you write your return address on the envelope and a reference number inside ("re: your invoice 42"). The reply comes to your mailbox, and the reference tells you which letter it answers. Request/reply over messaging works exactly like that.

The requester sends a message with two headers: replyTo (the queue where it reads replies) and a correlation id. A worker processes the request and publishes the result to the replyTo queue, copying the correlation id. The requester matches incoming replies to its pending requests by that id.

Run it

A shows the happy path with two workers answering in any order. In B workers sometimes crash after the work but before replying: the requester times out and the redelivered request later produces a reply nobody is waiting for. In C two requester instances share one reply queue — switch on per-instance queues to fix it.

Loading simulation…

Rules that keep it correct

  • One reply queue per requester instance (or a shared queue plus routing by instance id). Replies must reach the instance that holds the pending request.
  • Always set a timeout and treat it as "unknown outcome", not "failed": the work may still complete.
  • Ignore or log late replies by correlation id; never apply them to a different request.
  • Make the work idempotent: redelivery after a crash runs it again (see Idempotent Consumer).

Implementation with Spring AMQP

Requester and worker
// Requester: RabbitTemplate creates a reply queue, sets replyTo + correlation id and waits.
@Service
class PriceClient {
    private final RabbitTemplate rabbit;

    PriceClient(RabbitTemplate rabbit) {
        this.rabbit = rabbit;
        this.rabbit.setReplyTimeout(2_000);
    }

    Price quote(QuoteRequest request) {
        Price price = (Price) rabbit.convertSendAndReceive("pricing", "price-requests", request);
        if (price == null) throw new PricingTimeoutException(request.id());   // timeout: outcome unknown
        return price;
    }
}

// Worker: the return value is published to the request's replyTo address automatically.
@Component
class PricingWorker {
    @RabbitListener(queues = "price-requests", concurrency = "2")
    Price handle(QuoteRequest request) {
        return engine.price(request);
    }
}

Advanced With Kafka, ReplyingKafkaTemplate does the same using a reply topic and the KafkaHeaders.CORRELATION_ID header; give each requester instance its own reply partition or topic. For long-running work prefer the HTTP flavour of the pattern: return 202 Accepted with a status URL the client polls.

Trade-offs

BenefitsCosts
DecouplingRequesters do not need to know where workers run; the broker buffers bursts.More moving parts than an HTTP call: queues, correlation, timeouts.
LoadWorkers pull work at their own pace.The requester still waits — latency includes broker hops and queueing.
FailuresRequests survive worker crashes (redelivery).Late and duplicate replies must be handled explicitly.

Production considerations

  • Use exclusive, auto-delete reply queues (or direct reply-to in RabbitMQ) so stale queues do not pile up.
  • Propagate the correlation id into logs and traces — it is the only link between request and reply.
  • Alert on unmatched replies: they reveal timeouts that are too short or routing mistakes.

Further reading

Related topics